char name[32];
if (a->mode == ITEM_INDEX)
{
- snprintf(name, sizeof(name), "(*_T%d)", a->reg);
+ snprintf(name, sizeof(name), "(*_T%d + _T%lld)", a->reg, a->imm.i);
}
else if (a->mode == ITEM_FIELD)
{
{
error(p, "negative array index");
}
+ index->imm.i = (index->imm.i * size_of(array->type->base));
}
else
{
printf(" __CHECK_RANGE(%s < %d);\n", temp(index), array->type->size);
+ int dest_reg = declare_temp(p, index->type, 0);
+ printf(" = %s * %ld;\n", temp(index), size_of(array->type->base));
+ index->reg = dest_reg;
+ }
+
+ if (array->mode == ITEM_INDEX)
+ {
+ int dest_reg = declare_temp(p, index->type, 0);
+ printf(" = _T%lld + %s;\n", array->imm.i, temp(index));
+ index->reg = dest_reg;
}
/* emit the operation to calculate offset */
array->type = array->type->base;
array->mode = ITEM_INDEX;
-
-// int dest_reg = declare_temp(p, array->type, 1);
-// printf(" = &%s[%s];\n", temp(array), temp(index));
-// array->reg = dest_reg;
+ array->imm.i = index->reg;
}
Field* get_field(Parser* p, Type* type, char* name)
void dump_symbol(Symbol* a);
void dump_type(Type* a);
+// src/align.c
+long align_item(long offset, long size);
+long size_of(Type* type);
+
/* Backend Code Generation
*****************************************************************************/
extern Type BoolType, IntType, RealType, StringType;
--- /dev/null
+#include "cerise.h"
+
+long align_item(long offset, long size)
+{
+ offset--;
+ if (size >= 8u)
+ {
+ offset += (8 - (offset & (8 - 1)));
+ }
+ else if (size >= 4u)
+ {
+ offset += (4 - (offset & (4 - 1)));
+ }
+ else if (size >= 2u)
+ {
+ offset += (2 - (offset & (2 - 1)));
+ }
+ return offset;
+}
+
+long size_of(Type* type)
+{
+ long size = 1;
+ for (; type->form == FORM_ARRAY; type = type->base)
+ {
+ size = (size * type->size);
+ }
+ return (size * type->size);
+}
}
}
-static long align_item(long offset, long size)
-{
- offset--;
- if (size >= 8u)
- {
- offset += (8 - (offset & (8 - 1)));
- }
- else if (size >= 4u)
- {
- offset += (4 - (offset & (4 - 1)));
- }
- else if (size >= 2u)
- {
- offset += (2 - (offset & (2 - 1)));
- }
- return offset;
-}
-
-static long size_of(Type* type)
-{
- long size = 1;
- for (; type->form == FORM_ARRAY; type = type->base)
- {
- size = (size * type->size);
- }
- return (size * type->size);
-}
-
Field* add_field(Parser* p, Type* type, char* name, bool export)
{
Field* prev = NULL;
--- /dev/null
+type symbol = string
+
+type expr =
+ | Var of symbol
+ | Num of int
+ | Plus of expr * expr
+ | Assign of symbol * expr
+
+type mem =
+ Var of symbol
+ | Temp of symbol
+ | Addr of symbol
+
+type operand =
+ Const of int
+ | Mem of mem
+
+type cond =
+ Bool of operand
+ | Not of operand
+ | Eq of operand * operand
+ | Leq of operand * operand
+ | Le of operand * operand
+
+type rhs =
+ Plus of operand * operand
+ | Times of operand * operand
+ | Id of operand
+
+type instr=
+ Read of symbol
+ | Write of symbol
+ | Lab of symbol
+ | Assign of symbol * rhs
+ | AssignRI of operand * operand * operand
+ | AssignLI of operand * operand * operand
+ | BranchComp of cond * label
+ | Halt
+ | Nop
+
+type tree =
+ Oneline of instr
+ | Seq of tree * tree
\ No newline at end of file
# f.dim.h = 0;
# f.label[0] = 42;
- g[1] = 42;
+ c = 4;
+ g[c] = 42;
+# e[0][9] = 42;
end