Decaf Compiler
Loading...
Searching...
No Matches
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_PHYSICAL_REGS   32
 Maximum number of physical registers.
 
#define MAX_INSTRUCTIONS   2048
 Maximum number of instructions.
 
#define PARAM_BP_OFFSET   (2 * WORD_SIZE)
 Base pointer offset for parameters.
 
#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.
 
typedef struct ILOCInsn ILOCInsn
 ILOC instruction.
 
typedef struct InsnList InsnList
 Linked list of ILOCInsn* elements.
 

Enumerations

enum  OperandType {
  EMPTY , STACK_REG , BASE_REG , RETURN_REG ,
  VIRTUAL_REG , PHYSICAL_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 (void)
 Create a empty operand.
 
Operand stack_register (void)
 Create a SP operand.
 
Operand base_register (void)
 Create a BP operand.
 
Operand return_register (void)
 Create a RET operand.
 
Operand virtual_register (void)
 Create a new virtual register operand (uses next available ID)
 
Operand physical_register (int id)
 Create a physical register operand with a specific register ID.
 
Operand anonymous_label (void)
 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.
 
ILOCInsnILOCInsn_new_3op (InsnForm form, Operand op1, Operand op2, Operand op3)
 Create a new 3-operand instruction.
 
ILOCInsnILOCInsn_new_2op (InsnForm form, Operand op1, Operand op2)
 Create a new 2-operand instruction.
 
ILOCInsnILOCInsn_new_1op (InsnForm form, Operand op1)
 Create a new 1-operand instruction.
 
ILOCInsnILOCInsn_new_0op (InsnForm form)
 Create a new 0-operand instruction.
 
void ILOCInsn_set_comment (ILOCInsn *insn, const char *comment)
 Add a comment to an instruction.
 
ILOCInsnILOCInsn_copy (ILOCInsn *insn)
 Create a new instruction that is a copy of an existing instruction.
 
void ILOCInsn_print (ILOCInsn *insn, FILE *output)
 Print an instruction.
 
int ILOCInsn_get_operand_count (ILOCInsn *insn)
 Count the number of operands in an instruction.
 
ILOCInsnILOCInsn_get_read_registers (ILOCInsn *insn)
 Get a list of registers that are read from by this instruction.
 
Operand ILOCInsn_get_write_register (ILOCInsn *insn)
 Get the register (if any) that is written to by this instruction.
 
void ILOCInsn_free (ILOCInsn *insn)
 Deallocate an instruction structure.
 
InsnListInsnList_new (void)
 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.
 
NodeVisitorAllocateSymbolsVisitor_new (void)
 Create a new AST visitor that allocates addresses for all variable symbols.
 
void reg_attr_print (void *reg, FILE *output)
 Print a register attribute.
 
void insnlist_attr_print (InsnList *list, FILE *output)
 Print an instruction list attribute.
 
void ASTNode_copy_code (ASTNode *dest, ASTNode *src)
 Copy code attribute from one AST node to another.
 
void ASTNode_emit_insn (ASTNode *dest, ILOCInsn *insn)
 Add/append an instruction to the code attribute (instruction list) for an AST node.
 
void ASTNode_add_comment (ASTNode *dest, const char *comment)
 Add a comment to the most recently-emitted instruction for an AST node.
 
void ASTNode_set_temp_reg (ASTNode *node, Operand reg)
 Set a temporary result register for an AST node.
 
Operand ASTNode_get_temp_reg (ASTNode *node)
 Retrieve a temporary result register from an AST node.
 
int run_simulator (InsnList *program, bool print_trace)
 Run ILOC simulator on an ILOC program.
 

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.

Every ILOC instruction has a base type or "form" (it is also sometimes called a "class" but we avoid this term because it implies objects). The form is usually encoded as a number (e.g., opcode or icode) but because ILOC is intended to be a higher-level intermediate assembly language we do not specify the ABI down to that level.

Enumeration Type Documentation

◆ InsnForm

enum InsnForm

ILOC instruction form.

Every ILOC instruction has a base type or "form" (it is also sometimes called a "class" but we avoid this term because it implies objects). The form is usually encoded as a number (e.g., opcode or icode) but because ILOC is intended to be a higher-level intermediate assembly language we do not specify the ABI down to that level.

Enumerator
ADD 

Integer addition (r1 + r2 => r3)

SUB 

Integer subtraction (r1 - r2 => r3)

MULT 

Integer multiplication (r1 * r2 => r3)

DIV 

Integer division (r1 / r2 => r3) w/ truncation of answer if fractional.

AND 

Boolean AND (r1 && r2 => r3)

OR 

Boolean OR (r1 || r2 => r3)

LOAD_I 

Load/set a register to an immediate value.

LOAD 

Load from memory using address (reg) addressing.

LOAD_AI 

Load from memory using address + immediate (reg/imm) addressing.

LOAD_AO 

Load from memory using address + offset (reg/reg) addressing.

STORE 

Store to memory using address (reg) addressing.

STORE_AI 

Store to memory using address + immediate (reg/imm) addressing.

STORE_AO 

Store to memory using address + offset (reg/reg) addressing.

NOP 

Do nothing (sometimes useful for testing)

I2I 

Copy one integer register to another.

JUMP 

Unconditional control branch to a target label.

CBR 

Conditional control branch to one of two target labels.

CMP_LT 

Compare integers and save the boolean result (r1 < r2 => r3)

CMP_LE 

Compare integers and save the boolean result (r1 <= r2 => r3)

CMP_EQ 

Compare integers and save the boolean result (r1 == r2 => r3)

CMP_GE 

Compare integers and save the boolean result (r1 >= r2 => r3)

CMP_GT 

Compare integers and save the boolean result (r1 > r2 => r3)

CMP_NE 

Compare integers and save the boolean result (r1 != r2 => r3)

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

PHYSICAL_REG 

Numbered 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 ( void  )

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 because C doesn't allow us to return an array of operands – 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