-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegen.c
276 lines (234 loc) · 6.53 KB
/
codegen.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* Compilers Project Group 2
// Aditya Raisinghani 2011A7PS042P
// Utkarsh Verma 2011A7PS137P
// BITS Pilani, Pilani Campus
// Second semester 2014
*/
// Contains functions for code generation
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "tokens.h"
#include "lexer.h"
#include "parserDef.h"
#include "first_follow_gen.h"
#include "parser.h"
#include "symbol_table.h"
#include "type_extractor.h"
#include "codegen.h" //include in all others.
#define REGNUM 8 //no of registers = 8
int asmlineno = 0;
ASMList acode;
int regCount = 0;
int regBcount = 0;
void codegenInit(ASMList asmlist)
{
char *modeltext = ".MODEL SMALL"
char *stacktext = ".STACK 100h"
asmlist[asmlineno] = malloc(sizeof(char)*LINESIZE);
strcpy(asmlist[asmlineno],modeltext); //line0
asmlist[++asmlineno] = malloc(sizeof(char)*LINESIZE);
strcpy(asmlist[asmlineno],stacktext); //line1
}
void codegenData(ASMList asmlist, IRTable headIR)
{
char *datainit = ".DATA"
asmlist[++asmlineno] = malloc(sizeof(char)*LINESIZE);
strcpy(asmlist[asmlineno],datainit);
IRTable findIR = headIR;
while(findIR != NULL)
{
if(findIR->op == MOV) //from enumerated ops list
{
char *datatext = malloc(sizeof(char)*LINESIZE);
//// cat findIR->result.temp_name;
//check if int or real or str
if(findIR->arg1.typename == INTEGER )
{
///char *datatext_type = " DW "; //fixed for now.
///char *datatext_val = findIR->arg1.value; //try strcpy if this fails
sprintf(datatext, "%s DW %d", findIR->result.temp_name, findIR->arg1.value);
}
/* FLOAT handling in ASM?
else if(findIR->arg1.typename == FLOAT )
{
char *datatext_type = " DW "; //fixed for now.
char *datatext_val = findIR->arg1.floatValue;
}
*/
else if(findIR->arg1.typename == STRINGENUM) //replace with enum name
{
char *datatext_type = " DW ";
//char *quote = "' ";
//char *datatext_val;
///strcpy(datatext_val,quote);
///strcat(datatext_val, findIR->arg1.strValue);
///strcat(datatext_val, quote);
sprintf(datatext, "%s DW '%s' ", findIR->result.temp_name, findIR->arg1.strValue);
}
///strcat(datatext, datatext_type);
///strcat(datatext, datatext_val);
asmlist([++asmlineno]) = malloc(sizeof(char)*LINESIZE);
strcpy(asmlist[asmlineno], datatext); //line added
findIR = findIR->nextEntry; //move to next entry
}
}
}
///Uses a round robin approach for register allocation:
///will return an occupied reg after all are occupied, in RR fashion.
char* findFreeReg()
{
char *rname;
switch(regCount)
{
case 0:
rname = "ax";
regCount++;
break;
case 1:
rname = "bx";
regCount++;
break;
case 2:
rname = "cx";
regCount++;
break;
case 3:
rname = "dx";
regCount = 0;
break;
default:
rname = NULL;
}
return rname;
}
char* findFreeRegBIG()
{
char *rBname;
switch(regBcount)
{
case 0:
rBname = "ds, "; //note the , reqd in dest.
regBcount++;
break;
case 1:
rBname = "es, ";
regBcount = 0;
break;
//add more CS,SS if reqd.
default:
rBname = NULL;
}
return rBname;
}
char* insertBwStr(char* t1, char* t2, char* t3)
{
char *res;
strcpy(res,t1);
strcat(res,t2);
strcat(res,t3);
return res;
}
void insertASMline(ASMList asmlist, char *str)
{
asmlist([++asmlineno]) = malloc(sizeof(char)*LINESIZE);
strcpy(asmlist[asmlineno], str);
}
void codegenOps(ASMList asmlist, IRTable headIR)
{
char *opsinit = ".CODE"
asmlist[++asmlineno] = malloc(sizeof(char)*LINESIZE);
strcat(asmlist[asmlineno],opsinit);
IRTable findIR = headIR;
while(findIR != NULL)
{
//for MOV op,need to load the val to a reg
if(findIR->op == MOV) //from enumerated ops list
{
char *reg = findFreeReg();
char *mov_offset = insertBwStr("mov ",reg, ", @data"); //if this doesnt work, then put t1 and t3 as sep strings and pass.
//asmlist([++asmlineno]) = malloc(sizeof(char)*LINESIZE);
//strcpy(asmlist[asmlineno], mov_offset);
insertASMline(asmlist, mov_offset);
char *regB = findFreeRegBIG();
char *mov_ds = insertBwStr("mov ",regB, reg);
insertASMline(asmlist, mov_ds);
char *mov_dsoffset = insertBwStr("mov ",regB,"OFFSET ");
strcat(mov_dsoffset, findIR->result.temp_name);
insertASMline(asmlist, mov_dsoffset);
findIR = findIR->nextEntry;
}
else if(findIR->op == ADD ) // || MUL ,SUB,DIV others
{
//! MODIFY. mismatch possbile btw loaded reg and this. think .
//! this may fail if the regs reqd are overwritten.
//! bascially if the math op is just after the mov ops, then fine
char *reg = findFreeReg();
char *math_text = insertBwStr("mov ",reg, ", " );
strcat(math_text, findIR->arg1.temp_name);
insertASMline(asmlist, math_text);
char *math_text2 = insertBwStr("add ",reg, ", ");
strcat(math_text2 , findIR->arg2.temp_name);
insertASMline(asmlist, math_text2);
char *math_text3 = insertBwStr("mov ", findIR->result.temp_name, ", ");
strcat(math_text3, reg);
insertASMline(asmlist , math_text3);
insertASMline(asmlist, math_text);
findIR = findIR->nextEntry;
}
/* mak add work for all other 3 ops. use automation.
else if(findIR->op == SUB ) // || MUL ,SUB,DIV others
{
//! MODIFY. mismatch possbile btw loaded reg and this. think .
char *math_text= "sub ";
strcat(math_text, findIR->result.temp_name);
strcat(math_text, findIR->arg1.temp_name);
strcat(math_text, findIR->arg2.temp_name);
insertASMline(asmlist, math_text);
findIR = findIR->nextEntry;
}
else if(findIR->op == MUL ) // || MUL ,SUB,DIV others
{
//! MODIFY. mismatch possbile btw loaded reg and this. think .
char *math_text= "mul ";
strcat(math_text, findIR->result.temp_name);
strcat(math_text, findIR->arg1.temp_name);
strcat(math_text, findIR->arg2.temp_name);
insertASMline(asmlist, math_text);
findIR = findIR->nextEntry;
}
else if(findIR->op == DIV ) // || MUL ,SUB,DIV others
{
//! MODIFY. mismatch possbile btw loaded reg and this. think .
char *math_text= "div ";
strcat(math_text, findIR->result.temp_name);
strcat(math_text, findIR->arg1.temp_name);
strcat(math_text, findIR->arg2.temp_name);
insertASMline(asmlist, math_text) ;
findIR = findIR->nextEntry;
}
*/
}
void writeCodetoFile()
{
FILE* outfile = fopen("code.asm","rw");
int c = 0;
if( outfile != NULL)
{
while(c<= asmlineno)
{
fprintf(outfile, "%s\n", asmlist[c]);
}
}
}
void mainCodeGen()
{
acode = malloc(siezeof(codeLine)*ASMLINES);
if(acode != NULL)
{
codegenInit(acode);
codegenData(acode, headIR);
codegenOps( acode, headIR);
writeCodetoFile();
}
}