Decaf Compiler
iloc.h
Go to the documentation of this file.
1 
9 #ifndef __ILOC_H
10 #define __ILOC_H
11 
12 #include "common.h"
13 #include "token.h"
14 #include "ast.h"
15 #include "visitor.h"
16 #include "symbol.h"
17 
21 #define WORD_SIZE 8
22 
26 #define MEM_SIZE 65536
27 
31 #define MAX_VIRTUAL_REGS 2048
32 
36 #define MAX_INSTRUCTIONS 2048
37 
43 #define PARAM_BP_OFFSET (2 * WORD_SIZE)
44 
48 #define LOCAL_BP_OFFSET (-WORD_SIZE)
49 
53 #define STATIC_VAR_OFFSET 0x100
54 
58 typedef enum OperandType
59 {
69 } OperandType;
70 
74 typedef struct Operand
75 {
80 
81  union {
82  int id;
83  long imm;
84  char str[MAX_LINE_LEN];
85  };
86 
87 } Operand;
88 
93 
98 
103 
108 
113 
117 Operand register_with_id (int id);
118 
123 
127 Operand call_label (const char* label);
128 
132 Operand int_const (long integer);
133 
137 Operand str_const (const char* string);
138 
145 void Operand_print (Operand op, FILE* output);
146 
156 typedef enum InsnForm
157 {
158  /* Forms documented in EAC2 Appendix A */
159 
160  ADD,
161  SUB,
163  DIV,
164  AND,
165  OR,
173  NOP,
174  I2I,
176  CBR,
184  /* New or modified forms */
185 
188  NOT,
189  NEG,
191  POP,
198 } InsnForm;
199 
221 typedef struct ILOCInsn
222 {
227 
235 
242 
246  struct ILOCInsn* next;
247 
248 } ILOCInsn;
249 
260 
270 
279 
287 
294 void ILOCInsn_set_comment (ILOCInsn* insn, const char* comment);
295 
303 
310 void ILOCInsn_print (ILOCInsn* insn, FILE* output);
311 
319 
331 
339 
345 void ILOCInsn_free (ILOCInsn* insn);
346 
348 
349 
355 void InsnList_print (InsnList* list, FILE* output);
356 
363 
370 void reg_attr_print (void* reg, FILE* output);
371 
378 void insnlist_attr_print (InsnList* list, FILE* output);
379 
386 void ASTNode_copy_code (ASTNode* dest, ASTNode* src);
387 
394 void ASTNode_emit_insn (ASTNode* dest, ILOCInsn* insn);
395 
404 void ASTNode_add_comment (ASTNode* dest, const char* comment);
405 
412 void ASTNode_set_temp_reg (ASTNode* node, Operand reg);
413 
423 
433 int run_simulator (InsnList* program, bool print_trace);
434 
435 #endif
ASTNode_get_temp_reg
Operand ASTNode_get_temp_reg(ASTNode *node)
Retrieve a temporary result register from an AST node.
Definition: iloc.c:461
stack_register
Operand stack_register()
Create a SP operand.
Definition: iloc.c:13
CMP_GE
@ CMP_GE
Compare integers and save the boolean result (r1 >= r2 => r3)
Definition: iloc.h:180
InsnList
Linked list of ILOCInsn* elements.
Definition: iloc.h:347
visitor.h
AST visitors.
Operand::type
OperandType type
Operand type (discriminator/tag for anonymous union)
Definition: iloc.h:79
register_with_id
Operand register_with_id(int id)
Create a virtual/physical register operand with a specific register ID.
Definition: iloc.c:38
call_label
Operand call_label(const char *label)
Create a call label with the a specific name.
Definition: iloc.c:51
ILOCInsn
struct ILOCInsn ILOCInsn
ILOC instruction.
JUMP_LABEL
@ JUMP_LABEL
Numbered jump label.
Definition: iloc.h:65
ILOCInsn::op
Operand op[3]
Operands.
Definition: iloc.h:234
BASE_REG
@ BASE_REG
Base pointer register (BP)
Definition: iloc.h:62
ADD
@ ADD
Integer addition (r1 + r2 => r3)
Definition: iloc.h:160
ILOCInsn_set_comment
void ILOCInsn_set_comment(ILOCInsn *insn, const char *comment)
Add a comment to an instruction.
Definition: iloc.c:125
ASTNode
Main AST node structure.
Definition: ast.h:470
CBR
@ CBR
Conditional control branch to one of two target labels.
Definition: iloc.h:176
STORE_AO
@ STORE_AO
Store to memory using address + offset (reg/reg) addressing.
Definition: iloc.h:172
int_const
Operand int_const(long integer)
Create an integer constant operand with a specific value.
Definition: iloc.c:59
MULT_I
@ MULT_I
Multiply an immediate/constant value with a register value (r1 * c1 => r2)
Definition: iloc.h:187
reg_attr_print
void reg_attr_print(void *reg, FILE *output)
Print a register attribute.
Definition: iloc.c:397
ASTNode_set_temp_reg
void ASTNode_set_temp_reg(ASTNode *node, Operand reg)
Set a temporary result register for an AST node.
Definition: iloc.c:456
CALL_LABEL
@ CALL_LABEL
Textual call label.
Definition: iloc.h:66
STORE_AI
@ STORE_AI
Store to memory using address + immediate (reg/imm) addressing.
Definition: iloc.h:171
PRINT
@ PRINT
Print a constant or register value.
Definition: iloc.h:195
PUSH
@ PUSH
Push register value onto system stack (decrement SP and store from r1)
Definition: iloc.h:190
ast.h
AST nodes and attributes.
run_simulator
int run_simulator(InsnList *program, bool print_trace)
Run ILOC simulator on an ILOC program.
Definition: iloc.c:920
NodeVisitor
Node visitor structure.
Definition: visitor.h:27
virtual_register
Operand virtual_register()
Create a new virtual register operand (uses next available ID)
Definition: iloc.c:31
LOAD
@ LOAD
Load from memory using address (reg) addressing.
Definition: iloc.h:167
ILOCInsn_copy
ILOCInsn * ILOCInsn_copy(ILOCInsn *insn)
Create a new instruction that is a copy of an existing instruction.
Definition: iloc.c:130
STACK_REG
@ STACK_REG
Stack pointer register (SP)
Definition: iloc.h:61
Operand
struct Operand Operand
ILOC operand structure.
Operand
ILOC operand structure.
Definition: iloc.h:74
RETURN
@ RETURN
Return from a function (pop return address and jump)
Definition: iloc.h:194
AllocateSymbolsVisitor_new
NodeVisitor * AllocateSymbolsVisitor_new()
Create a new AST visitor that allocates addresses for all variable symbols.
Definition: iloc.c:375
ASTNode_copy_code
void ASTNode_copy_code(ASTNode *dest, ASTNode *src)
Copy code attribute from one AST node to another.
Definition: iloc.c:412
VIRTUAL_REG
@ VIRTUAL_REG
Numbered virtual (or physical) register.
Definition: iloc.h:64
INT_CONST
@ INT_CONST
Integer constant/literal.
Definition: iloc.h:67
InsnList_print
void InsnList_print(InsnList *list, FILE *output)
Print an instruction list with proper indentation and comments.
Definition: iloc.c:279
empty_operand
Operand empty_operand()
Create a empty operand.
Definition: iloc.c:7
str_const
Operand str_const(const char *string)
Create a string constant operand with a specific value.
Definition: iloc.c:65
ILOCInsn_print
void ILOCInsn_print(ILOCInsn *insn, FILE *output)
Print an instruction.
Definition: iloc.c:146
insnlist_attr_print
void insnlist_attr_print(InsnList *list, FILE *output)
Print an instruction list attribute.
Definition: iloc.c:403
RETURN_REG
@ RETURN_REG
Return value register (RET)
Definition: iloc.h:63
ILOCInsn_get_operand_count
int ILOCInsn_get_operand_count(ILOCInsn *insn)
Count the number of operands in an instruction.
Definition: iloc.c:199
InsnForm
InsnForm
ILOC instruction form.
Definition: iloc.h:156
NOP
@ NOP
Do nothing (sometimes useful for testing)
Definition: iloc.h:173
LOAD_AO
@ LOAD_AO
Load from memory using address + offset (reg/reg) addressing.
Definition: iloc.h:169
CMP_NE
@ CMP_NE
Compare integers and save the boolean result (r1 != r2 => r3)
Definition: iloc.h:182
Operand_print
void Operand_print(Operand op, FILE *output)
Print an operand.
Definition: iloc.c:73
STORE
@ STORE
Store to memory using address (reg) addressing.
Definition: iloc.h:170
ILOCInsn_new_3op
ILOCInsn * ILOCInsn_new_3op(InsnForm form, Operand op1, Operand op2, Operand op3)
Create a new 3-operand instruction.
Definition: iloc.c:97
DECL_LIST_TYPE
#define DECL_LIST_TYPE(NAME, ELEMTYPE)
Declare a singly-linked list structure of the given type.
Definition: common.h:132
STR_CONST
@ STR_CONST
String constant/literal.
Definition: iloc.h:68
Operand::imm
long imm
Integer constant/literal.
Definition: iloc.h:83
NOT
@ NOT
Boolean NOT (!r1 => r2)
Definition: iloc.h:188
ASTNode_emit_insn
void ASTNode_emit_insn(ASTNode *dest, ILOCInsn *insn)
Add/append an instruction to the code attribute (instruction list) for an AST node.
Definition: iloc.c:433
EMPTY
@ EMPTY
No operand.
Definition: iloc.h:60
I2I
@ I2I
Copy one integer register to another.
Definition: iloc.h:174
ILOCInsn_free
void ILOCInsn_free(ILOCInsn *insn)
Deallocate an instruction structure.
Definition: iloc.c:272
CMP_LT
@ CMP_LT
Compare integers and save the boolean result (r1 < r2 => r3)
Definition: iloc.h:177
Operand::id
int id
Virtual/physical register or jump label ID.
Definition: iloc.h:82
ASTNode_add_comment
void ASTNode_add_comment(ASTNode *dest, const char *comment)
Add a comment to the most recently-emitted instruction for an AST node.
Definition: iloc.c:443
Operand::str
char str[256]
String constant/literal.
Definition: iloc.h:84
CMP_LE
@ CMP_LE
Compare integers and save the boolean result (r1 <= r2 => r3)
Definition: iloc.h:178
OR
@ OR
Boolean OR (r1 || r2 => r3)
Definition: iloc.h:165
ILOCInsn_get_read_registers
ILOCInsn * ILOCInsn_get_read_registers(ILOCInsn *insn)
Get a list of registers that are read from by this instruction.
Definition: iloc.c:210
symbol.h
Symbols, symbol tables, and static analysis errors.
CMP_EQ
@ CMP_EQ
Compare integers and save the boolean result (r1 == r2 => r3)
Definition: iloc.h:179
ILOCInsn::next
struct ILOCInsn * next
Next instruction (if stored in a list)
Definition: iloc.h:246
base_register
Operand base_register()
Create a BP operand.
Definition: iloc.c:19
LABEL
@ LABEL
Jump label w/ integer ID (no effect)
Definition: iloc.h:192
MAX_LINE_LEN
#define MAX_LINE_LEN
Maximum length (in characters) of any single line of input.
Definition: common.h:42
return_register
Operand return_register()
Create a RET operand.
Definition: iloc.c:25
CMP_GT
@ CMP_GT
Compare integers and save the boolean result (r1 > r2 => r3)
Definition: iloc.h:181
ILOCInsn_get_write_register
Operand ILOCInsn_get_write_register(ILOCInsn *insn)
Get the register (if any) that is written to by this instruction.
Definition: iloc.c:248
DIV
@ DIV
Integer division (r1 / r2 => r3) w/ truncation of answer if fractional.
Definition: iloc.h:163
ILOCInsn_new_1op
ILOCInsn * ILOCInsn_new_1op(InsnForm form, Operand op1)
Create a new 1-operand instruction.
Definition: iloc.c:115
LOAD_AI
@ LOAD_AI
Load from memory using address + immediate (reg/imm) addressing.
Definition: iloc.h:168
common.h
Includes, constants, declarations, and macros used across the compiler.
AND
@ AND
Boolean AND (r1 && r2 => r3)
Definition: iloc.h:164
SUB
@ SUB
Integer subtraction (r1 - r2 => r3)
Definition: iloc.h:161
MULT
@ MULT
Integer multiplication (r1 * r2 => r3)
Definition: iloc.h:162
ILOCInsn_new_0op
ILOCInsn * ILOCInsn_new_0op(InsnForm form)
Create a new 0-operand instruction.
Definition: iloc.c:120
anonymous_label
Operand anonymous_label()
Create a new jump label operand (uses next available ID)
Definition: iloc.c:44
PHI
@ PHI
Combine two registers in SSA form.
Definition: iloc.h:196
LOAD_I
@ LOAD_I
Load/set a register to an immediate value.
Definition: iloc.h:166
ILOCInsn
ILOC instruction.
Definition: iloc.h:221
OperandType
OperandType
Operand type.
Definition: iloc.h:58
CALL
@ CALL
Call a function (push return address and jump)
Definition: iloc.h:193
ILOCInsn_new_2op
ILOCInsn * ILOCInsn_new_2op(InsnForm form, Operand op1, Operand op2)
Create a new 2-operand instruction.
Definition: iloc.c:110
ADD_I
@ ADD_I
Add an immediate/constant value to a register value (r1 + c1 => r2)
Definition: iloc.h:186
NEG
@ NEG
Integer negation (-r1 => r2)
Definition: iloc.h:189
ILOCInsn::comment
char comment[256]
Comment associated with this instruction.
Definition: iloc.h:241
JUMP
@ JUMP
Unconditional control branch to a target label.
Definition: iloc.h:175
token.h
Tokens and regular expressions.
ILOCInsn::form
InsnForm form
Type ("form") of instruction.
Definition: iloc.h:226
POP
@ POP
Pop register value from system stack (load into r2 and increment SP)
Definition: iloc.h:191