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 
153 typedef enum InsnForm
154 {
155  ADD, SUB, MULT, DIV, AND, OR,
156  LOAD_I, LOAD, LOAD_AI, LOAD_AO,
157  STORE, STORE_AI, STORE_AO,
158  NOP, I2I, JUMP, CBR,
159  CMP_LT, CMP_LE, CMP_EQ, CMP_GE, CMP_GT, CMP_NE,
160 
163  NOT,
164  NEG,
166  POP,
172 } InsnForm;
173 
195 typedef struct ILOCInsn
196 {
201 
209 
216 
220  struct ILOCInsn* next;
221 
222 } ILOCInsn;
223 
234 
244 
253 
261 
268 void ILOCInsn_set_comment (ILOCInsn* insn, const char* comment);
269 
277 
284 void ILOCInsn_print (ILOCInsn* insn, FILE* output);
285 
293 
304 
312 
318 void ILOCInsn_free (ILOCInsn* insn);
319 
321 
322 
328 void InsnList_print (InsnList* list, FILE* output);
329 
336 
343 void reg_attr_print (void* reg, FILE* output);
344 
351 void insnlist_attr_print (InsnList* list, FILE* output);
352 
359 void ASTNode_copy_code (ASTNode* dest, ASTNode* src);
360 
367 void ASTNode_emit_insn (ASTNode* dest, ILOCInsn* insn);
368 
377 void ASTNode_add_comment (ASTNode* dest, const char* comment);
378 
385 void ASTNode_set_temp_reg (ASTNode* node, Operand reg);
386 
396 
406 int run_simulator (InsnList* program, bool print_trace);
407 
408 #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
InsnList
Linked list of ILOCInsn* elements.
Definition: iloc.h:320
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
JUMP_LABEL
Numbered jump label.
Definition: iloc.h:65
ILOCInsn::op
Operand op[3]
Operands.
Definition: iloc.h:208
BASE_REG
Base pointer register (BP)
Definition: iloc.h:62
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:449
int_const
Operand int_const(long integer)
Create an integer constant operand with a specific value.
Definition: iloc.c:59
MULT_I
Multiply an immediate/constant value with a register value (r1 * c1 => r2)
Definition: iloc.h:162
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
Textual call label.
Definition: iloc.h:66
PRINT
Print a constant or register value.
Definition: iloc.h:170
PUSH
Push register value onto system stack (decrement SP and store from r1)
Definition: iloc.h:165
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
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 pointer register (SP)
Definition: iloc.h:61
Operand
ILOC operand structure.
Definition: iloc.h:74
RETURN
Return from a function (pop return address and jump)
Definition: iloc.h:169
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
Numbered virtual (or physical) register.
Definition: iloc.h:64
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 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:153
Operand_print
void Operand_print(Operand op, FILE *output)
Print an operand.
Definition: iloc.c:73
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:122
STR_CONST
String constant/literal.
Definition: iloc.h:68
Operand::imm
long imm
Integer constant/literal.
Definition: iloc.h:83
NOT
Boolean NOT (!r1 => r2)
Definition: iloc.h:163
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
No operand.
Definition: iloc.h:60
ILOCInsn_free
void ILOCInsn_free(ILOCInsn *insn)
Deallocate an instruction structure.
Definition: iloc.c:272
Operand::id
int id
Virtual/physical register or jump label ID.
Definition: iloc.h:82
Operand
struct Operand Operand
ILOC operand structure.
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
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.
ILOCInsn::next
struct ILOCInsn * next
Next instruction (if stored in a list)
Definition: iloc.h:220
base_register
Operand base_register()
Create a BP operand.
Definition: iloc.c:19
LABEL
Jump label w/ integer ID (no effect)
Definition: iloc.h:167
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
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
ILOCInsn_new_1op
ILOCInsn * ILOCInsn_new_1op(InsnForm form, Operand op1)
Create a new 1-operand instruction.
Definition: iloc.c:115
common.h
Includes, constants, declarations, and macros used across the compiler.
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
Combine two registers in SSA form.
Definition: iloc.h:171
ILOCInsn
ILOC instruction.
Definition: iloc.h:195
OperandType
OperandType
Operand type.
Definition: iloc.h:58
CALL
Call a function (push return address and jump)
Definition: iloc.h:168
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 an immediate/constant value to a register value (r1 + c1 => r2)
Definition: iloc.h:161
NEG
Integer negation (-r1 => r2)
Definition: iloc.h:164
ILOCInsn::comment
char comment[256]
Comment associated with this instruction.
Definition: iloc.h:215
token.h
Tokens and regular expressions.
ILOCInsn::form
InsnForm form
Type ("form") of instruction.
Definition: iloc.h:200
ILOCInsn
struct ILOCInsn ILOCInsn
ILOC instruction.
POP
Pop register value from system stack (load into r2 and increment SP)
Definition: iloc.h:166