Decaf Compiler
Classes | Macros | Typedefs | Enumerations | Functions
iloc.h File Reference

Declarations of ILOC structures. More...

#include "common.h"
#include "token.h"
#include "ast.h"
#include "visitor.h"
#include "symbol.h"

Go to the source code of this file.

Classes

struct  Operand
 ILOC operand structure. More...
 
struct  ILOCInsn
 ILOC instruction. More...
 
struct  InsnList
 Linked list of ILOCInsn* elements. More...
 

Macros

#define WORD_SIZE   8
 Machine word size (64 bits)
 
#define MEM_SIZE   65536
 Machine memory size (64K)
 
#define MAX_VIRTUAL_REGS   2048
 Maximum number of virtual registers.
 
#define MAX_INSTRUCTIONS   2048
 Maximum number of instructions.
 
#define PARAM_BP_OFFSET   (2 * WORD_SIZE)
 Base pointer offset for parameters. More...
 
#define LOCAL_BP_OFFSET   (-WORD_SIZE)
 Base pointer offset for local variables.
 
#define STATIC_VAR_OFFSET   0x100
 Base offset/address in memory for static/global variables.
 

Typedefs

typedef enum OperandType OperandType
 Operand type.
 
typedef struct Operand Operand
 ILOC operand structure.
 
typedef enum InsnForm InsnForm
 ILOC instruction form. More...
 
typedef struct ILOCInsn ILOCInsn
 ILOC instruction. More...
 
typedef struct InsnList InsnList
 Linked list of ILOCInsn* elements.
 

Enumerations

enum  OperandType {
  EMPTY, STACK_REG, BASE_REG, RETURN_REG,
  VIRTUAL_REG, JUMP_LABEL, CALL_LABEL, INT_CONST,
  STR_CONST
}
 Operand type. More...
 
enum  InsnForm {
  ADD, SUB, MULT, DIV,
  AND, OR, LOAD_I, LOAD,
  LOAD_AI, LOAD_AO, STORE, STORE_AI,
  STORE_AO, NOP, I2I, JUMP,
  CBR, CMP_LT, CMP_LE, CMP_EQ,
  CMP_GE, CMP_GT, CMP_NE, ADD_I,
  MULT_I, NOT, NEG, PUSH,
  POP, LABEL, CALL, RETURN,
  PRINT, PHI
}
 ILOC instruction form. More...
 

Functions

Operand empty_operand ()
 Create a empty operand.
 
Operand stack_register ()
 Create a SP operand.
 
Operand base_register ()
 Create a BP operand.
 
Operand return_register ()
 Create a RET operand.
 
Operand virtual_register ()
 Create a new virtual register operand (uses next available ID)
 
Operand register_with_id (int id)
 Create a virtual/physical register operand with a specific register ID.
 
Operand anonymous_label ()
 Create a new jump label operand (uses next available ID)
 
Operand call_label (const char *label)
 Create a call label with the a specific name.
 
Operand int_const (long integer)
 Create an integer constant operand with a specific value.
 
Operand str_const (const char *string)
 Create a string constant operand with a specific value.
 
void Operand_print (Operand op, FILE *output)
 Print an operand. More...
 
ILOCInsnILOCInsn_new_3op (InsnForm form, Operand op1, Operand op2, Operand op3)
 Create a new 3-operand instruction. More...
 
ILOCInsnILOCInsn_new_2op (InsnForm form, Operand op1, Operand op2)
 Create a new 2-operand instruction. More...
 
ILOCInsnILOCInsn_new_1op (InsnForm form, Operand op1)
 Create a new 1-operand instruction. More...
 
ILOCInsnILOCInsn_new_0op (InsnForm form)
 Create a new 0-operand instruction. More...
 
void ILOCInsn_set_comment (ILOCInsn *insn, const char *comment)
 Add a comment to an instruction. More...
 
ILOCInsnILOCInsn_copy (ILOCInsn *insn)
 Create a new instruction that is a copy of an existing instruction. More...
 
void ILOCInsn_print (ILOCInsn *insn, FILE *output)
 Print an instruction. More...
 
int ILOCInsn_get_operand_count (ILOCInsn *insn)
 Count the number of operands in an instruction. More...
 
ILOCInsnILOCInsn_get_read_registers (ILOCInsn *insn)
 Get a list of registers that are read from by this instruction. More...
 
Operand ILOCInsn_get_write_register (ILOCInsn *insn)
 Get the register (if any) that is written to by this instruction. More...
 
void ILOCInsn_free (ILOCInsn *insn)
 Deallocate an instruction structure. More...
 
InsnListInsnList_new ()
 Allocate and initialize a new, empty list.
 
void InsnList_add (InsnList *list, ILOCInsn *item)
 Add an item to the end of a list.
 
int InsnList_size (InsnList *list)
 Look up the size of a list.
 
bool InsnList_is_empty (InsnList *list)
 Test a list to see if it is empty.
 
void InsnList_free (InsnList *list)
 Deallocate a list and any contained items.
 
void InsnList_print (InsnList *list, FILE *output)
 Print an instruction list with proper indentation and comments. More...
 
NodeVisitorAllocateSymbolsVisitor_new ()
 Create a new AST visitor that allocates addresses for all variable symbols. More...
 
void reg_attr_print (void *reg, FILE *output)
 Print a register attribute. More...
 
void insnlist_attr_print (InsnList *list, FILE *output)
 Print an instruction list attribute. More...
 
void ASTNode_copy_code (ASTNode *dest, ASTNode *src)
 Copy code attribute from one AST node to another. More...
 
void ASTNode_emit_insn (ASTNode *dest, ILOCInsn *insn)
 Add/append an instruction to the code attribute (instruction list) for an AST node. More...
 
void ASTNode_add_comment (ASTNode *dest, const char *comment)
 Add a comment to the most recently-emitted instruction for an AST node. More...
 
void ASTNode_set_temp_reg (ASTNode *node, Operand reg)
 Set a temporary result register for an AST node. More...
 
Operand ASTNode_get_temp_reg (ASTNode *node)
 Retrieve a temporary result register from an AST node. More...
 
int run_simulator (InsnList *program, bool print_trace)
 Run ILOC simulator on an ILOC program. More...
 

Detailed Description

Declarations of ILOC structures.

This module provides declarations of all structures and framework functions for ILOC programs. It is necessary for Projects 4 (code generation) and 5 (register allocation).

Macro Definition Documentation

◆ PARAM_BP_OFFSET

#define PARAM_BP_OFFSET   (2 * WORD_SIZE)

Base pointer offset for parameters.

Skips base pointer and return address

Typedef Documentation

◆ ILOCInsn

typedef struct ILOCInsn ILOCInsn

ILOC instruction.

There are four normal constructors, based on the number of operands needed:

There is also a copy constructor (ILOCInsn_copy). Instructions should be deallocated using ILOCInsn_free.

Members:

◆ InsnForm

typedef enum InsnForm InsnForm

ILOC instruction form.

Most of these are as documented in EAC2 (Appendix A). New or modified forms are documented here.

Enumeration Type Documentation

◆ InsnForm

enum InsnForm

ILOC instruction form.

Most of these are as documented in EAC2 (Appendix A). New or modified forms are documented here.

Enumerator
ADD_I 

Add an immediate/constant value to a register value (r1 + c1 => r2)

MULT_I 

Multiply an immediate/constant value with a register value (r1 * c1 => r2)

NOT 

Boolean NOT (!r1 => r2)

NEG 

Integer negation (-r1 => r2)

PUSH 

Push register value onto system stack (decrement SP and store from r1)

POP 

Pop register value from system stack (load into r2 and increment SP)

LABEL 

Jump label w/ integer ID (no effect)

CALL 

Call a function (push return address and jump)

RETURN 

Return from a function (pop return address and jump)

PRINT 

Print a constant or register value.

PHI 

Combine two registers in SSA form.

◆ OperandType

Operand type.

Enumerator
EMPTY 

No operand.

STACK_REG 

Stack pointer register (SP)

BASE_REG 

Base pointer register (BP)

RETURN_REG 

Return value register (RET)

VIRTUAL_REG 

Numbered virtual (or physical) register.

JUMP_LABEL 

Numbered jump label.

CALL_LABEL 

Textual call label.

INT_CONST 

Integer constant/literal.

STR_CONST 

String constant/literal.

Function Documentation

◆ AllocateSymbolsVisitor_new()

NodeVisitor* AllocateSymbolsVisitor_new ( )

Create a new AST visitor that allocates addresses for all variable symbols.

Returns
Pointer to visitor 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.

If there is no code attribute or if it's an empty list, nothing is done.

Parameters
destPointer to destination AST node
commentComment to add

◆ ASTNode_copy_code()

void ASTNode_copy_code ( ASTNode dest,
ASTNode src 
)

Copy code attribute from one AST node to another.

Parameters
destPointer to destination AST node
srcPointer to source AST node

◆ 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.

Parameters
destPointer to destination AST node
insnInstruction to add

◆ ASTNode_get_temp_reg()

Operand ASTNode_get_temp_reg ( ASTNode node)

Retrieve a temporary result register from an AST node.

Aborts with an error message if the given node has no "reg" attribute.

Parameters
nodeAST node
Returns
Register operand

◆ ASTNode_set_temp_reg()

void ASTNode_set_temp_reg ( ASTNode node,
Operand  reg 
)

Set a temporary result register for an AST node.

Parameters
nodeAST node
regRegister as an operand

◆ ILOCInsn_copy()

ILOCInsn* ILOCInsn_copy ( ILOCInsn insn)

Create a new instruction that is a copy of an existing instruction.

Parameters
insnInstruction to copy
Returns
Pointer to a new instruction with all the data copied from the given instruction

◆ ILOCInsn_free()

void ILOCInsn_free ( ILOCInsn insn)

Deallocate an instruction structure.

Parameters
insnInstruction to deallocate

◆ ILOCInsn_get_operand_count()

int ILOCInsn_get_operand_count ( ILOCInsn insn)

Count the number of operands in an instruction.

Parameters
insnInstruction to count operands of
Returns
Number of non-empty operands in the instruction

◆ ILOCInsn_get_read_registers()

ILOCInsn* ILOCInsn_get_read_registers ( ILOCInsn insn)

Get a list of registers that are read from by this instruction.

This function returns the registers inside of a new "fake" instruction – don't forget to deallocate that instruction when you're done with it.

Parameters
insnInstruction to examine
Returns
Fake NOP instruction with the relevant registers as operands

◆ ILOCInsn_get_write_register()

Operand ILOCInsn_get_write_register ( ILOCInsn insn)

Get the register (if any) that is written to by this instruction.

Parameters
insnInstruction to examine
Returns
Operand that is written (or an EMPTY operand if there is none)

◆ ILOCInsn_new_0op()

ILOCInsn* ILOCInsn_new_0op ( InsnForm  form)

Create a new 0-operand instruction.

Parameters
formInstruction type/form
Returns
Pointer to new instruction

◆ ILOCInsn_new_1op()

ILOCInsn* ILOCInsn_new_1op ( InsnForm  form,
Operand  op1 
)

Create a new 1-operand instruction.

Parameters
formInstruction type/form
op1First operand
Returns
Pointer to new instruction

◆ ILOCInsn_new_2op()

ILOCInsn* ILOCInsn_new_2op ( InsnForm  form,
Operand  op1,
Operand  op2 
)

Create a new 2-operand instruction.

Parameters
formInstruction type/form
op1First operand
op2Second operand
Returns
Pointer to new instruction

◆ ILOCInsn_new_3op()

ILOCInsn* ILOCInsn_new_3op ( InsnForm  form,
Operand  op1,
Operand  op2,
Operand  op3 
)

Create a new 3-operand instruction.

Parameters
formInstruction type/form
op1First operand
op2Second operand
op3Third operand
Returns
Pointer to new instruction

◆ ILOCInsn_print()

void ILOCInsn_print ( ILOCInsn insn,
FILE *  output 
)

Print an instruction.

Parameters
insnInstruction to print
outputFile stream to print to

◆ ILOCInsn_set_comment()

void ILOCInsn_set_comment ( ILOCInsn insn,
const char *  comment 
)

Add a comment to an instruction.

Parameters
insnInstruction to add a comment to
commentComment to add

◆ insnlist_attr_print()

void insnlist_attr_print ( InsnList list,
FILE *  output 
)

Print an instruction list attribute.

Parameters
listPointer to instruction list to print
outputFile stream to print to

◆ InsnList_print()

void InsnList_print ( InsnList list,
FILE *  output 
)

Print an instruction list with proper indentation and comments.

Parameters
listList of instructions to print
outputFile stream to print to

◆ Operand_print()

void Operand_print ( Operand  op,
FILE *  output 
)

Print an operand.

Parameters
opOperand to print
outputFile stream to print to

◆ reg_attr_print()

void reg_attr_print ( void *  reg,
FILE *  output 
)

Print a register attribute.

Parameters
regPointer to register attribute to print
outputFile stream to print to

◆ run_simulator()

int run_simulator ( InsnList program,
bool  print_trace 
)

Run ILOC simulator on an ILOC program.

If tracing is enabled, the simulator will print the machine state before executing each instruction.

Parameters
programList of ILOC instructions
print_traceEnable/disable debug tracing