-
Notifications
You must be signed in to change notification settings - Fork 1
/
InstructionsEmitter.c
105 lines (101 loc) · 2.31 KB
/
InstructionsEmitter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "SymbolTable.h"
#include "Utilities.h"
#include "InstructionsEmitter.h"
char * getInstructionForOperation(Operations operation){
switch(operation){
case ADD:
return "add";
case MULT:
return "mult";
case DIV:
return "div";
case SUB:
return "sub";
case EQUAL:
return "cmp_EQ";
case NOTEQUAL:
return "cmp_NE";
case LT:
return "cmp_LT";
case GT:
return "cmp_GT";
case LTE:
return "cmp_LE";
case GTE:
return "cmp_GE";
case AND:
return "and";
case OR:
return "or";
default:
return "";
}
}
char * emitLoadForIdentifier(char * name, SymbolTable * table, char * reg){
Node * symbolNode = valueFor(table, name);
char * instruction[] = {
" loadAI rarp, ",
numToString(symbolNode->offset),
" -> ",
reg,
"\n"
};
return concat(instruction, 5);
}
char * emitLoadForNum(int num, char * reg){
char * instruction[] = {
" loadi ",
numToString(num),
" -> ",
reg,
"\n"
};
return concat(instruction, 5);
}
char * emitStoreForAssignment(char * exprReg, SymbolTable * table, char * name){
char * instruction [] = {
" storeAI ",
exprReg,
" -> rarp, ",
numToString(valueFor(table, name)->offset),
"\n"
};
return concat(instruction, 5);
}
char * emitBinaryInstruction(int operation, char * lreg, char * rreg, char * resultReg){
char * instruction [] = {
" ",
getInstructionForOperation(operation),
" ",
lreg,
", ",
rreg,
" -> ",
resultReg,
"\n"
};
return concat(instruction, 9);
}
char * emitCBRInstruction(char * reg, char * lab1, char * lab2){
char * instructions[] = {
" cbr ",
reg,
" -> ",
lab1,
", ",
lab2,
"\n"
};
return concat(instructions, 7);
}
char * emitJumpInstruction(char * label){
char * instruction []= {
" jumpi -> ",
label,
"\n"
};
return concat(instruction, 3);
}