From 609fbac09a1a131bfb4dbd2c5b18bbed47d36362 Mon Sep 17 00:00:00 2001 From: Klaas-Jan Stol Date: Fri, 1 Jun 2012 11:40:36 +0100 Subject: [PATCH] assign statement's lhs is of type m1_object. this is preparation for adding another parameter to gencode_obj --- m1_ast.c | 2 +- m1_ast.h | 2 +- m1_eval.c | 4 +++- m1_gencode.c | 15 ++++++++++----- m1_semcheck.c | 5 ++++- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/m1_ast.c b/m1_ast.c index 1af4e17..4cd31e4 100644 --- a/m1_ast.c +++ b/m1_ast.c @@ -286,7 +286,7 @@ expr_set_assign(M1_compiler *comp, m1_expression *node, m1_expression *lhs, int a += b => a = a + b */ node->expr.a = (m1_assignment *)m1_malloc(sizeof(m1_assignment)); - node->expr.a->lhs = lhs; + node->expr.a->lhs = lhs->expr.t; switch (assignop) { case OP_ASSIGN: /* normal case */ diff --git a/m1_ast.h b/m1_ast.h index fe13016..5276f16 100644 --- a/m1_ast.h +++ b/m1_ast.h @@ -41,7 +41,7 @@ typedef struct m1_struct { typedef struct m1_assignment { - struct m1_expression *lhs; + struct m1_object *lhs; struct m1_expression *rhs; } m1_assignment; diff --git a/m1_eval.c b/m1_eval.c index f59f1c0..24536ab 100644 --- a/m1_eval.c +++ b/m1_eval.c @@ -12,6 +12,8 @@ Sample AST walking code to print out the equivalent M1 code. #define OUT stderr +static void eval_assign(m1_assignment *a); +static void eval_obj(m1_object *o); static void eval_expr(m1_expression *e); static void @@ -27,7 +29,7 @@ eval_int(int value) { static void eval_assign(m1_assignment *a) { - eval_expr(a->lhs); + eval_obj(a->lhs); fprintf(OUT, " = "); eval_expr(a->rhs); fprintf(OUT, ";"); diff --git a/m1_gencode.c b/m1_gencode.c index 4a25d40..7709da9 100644 --- a/m1_gencode.c +++ b/m1_gencode.c @@ -38,7 +38,7 @@ in gencode_number(). static m1_reg gencode_expr(M1_compiler *comp, m1_expression *e); static void gencode_block(M1_compiler *comp, m1_expression *block); - +static m1_reg gencode_obj(M1_compiler *comp, m1_object *obj, m1_object **parent); static const char type_chars[4] = {'i', 'n', 's', 'p'}; static const char reg_chars[4] = {'I', 'N', 'S', 'P'}; @@ -110,7 +110,7 @@ gencode_int(M1_compiler *comp, m1_literal *lit) { set_imm Ix, y, z */ - m1_reg reg; + m1_reg reg; assert(comp != NULL); assert(lit != NULL); @@ -156,13 +156,14 @@ gencode_string(M1_compiler *comp, m1_literal *lit) { static m1_reg gencode_assign(M1_compiler *comp, NOTNULL(m1_assignment *a)) { m1_reg lhs, rhs; + m1_object *parent; debug("gencode_assign start...\n"); assert(a != NULL); rhs = gencode_expr(comp, a->rhs); - lhs = gencode_expr(comp, a->lhs); + lhs = gencode_obj(comp, a->lhs, &parent); /* copy the value held in register for rhs to the register of lhs */ assert((lhs.type >= 0) && (lhs.type < 4)); @@ -303,8 +304,12 @@ gencode_obj(M1_compiler *comp, m1_object *obj, m1_object **parent) { { int offset = 0; m1_reg offsetreg = gencode_expr(comp, obj->obj.index); - - // fprintf(OUT, "\tderef\t%d, , I%d\n", reg.no); + if (obj->is_target == 1) { + // fprintf(OUT, "\tderef\t%d, , I%d\n", reg.no); + } + else { + + } break; } default: diff --git a/m1_semcheck.c b/m1_semcheck.c index c507563..afb3f34 100644 --- a/m1_semcheck.c +++ b/m1_semcheck.c @@ -9,6 +9,9 @@ static m1_type check_expr(M1_compiler *comp, m1_expression *e); static void check_block(M1_compiler *comp, m1_expression *expr); +static m1_type check_obj(M1_compiler *comp, m1_object *obj); + + static void type_error(M1_compiler *comp, unsigned line, char *msg) { @@ -24,7 +27,7 @@ warning(M1_compiler *comp, unsigned line, char *msg) { static void check_assign(M1_compiler *comp, m1_assignment *a) { - m1_type ltype = check_expr(comp, a->lhs); + m1_type ltype = check_obj(comp, a->lhs); m1_type rtype = check_expr(comp, a->rhs); if (ltype != rtype) {