Subversion Repository Public Repository

excompiler

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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "asmcode.h"

#define WORD_SIZE 4

enum Opcode {
  C_PUSH_AX, C_PUSH_BX, C_PUSH_PBX,
  C_POP_AX, C_POP_BX, C_POP_CX,
  C_ADD, C_SUB, C_MUL,
  C_SETE, C_SETNE, C_SETL, C_SETG, C_SETLE, C_SETGE, C_AND_AX,
  C_LEA, C_MOV_PBX_CX, C_CALL,
  C_CMP_BX_CX, C_CMP_BX_0, C_JZ, C_JMP
};

int AsmCodeGen2::add(Opcode op)
{
}

int AsmCodeGen2::add(Opcode op, std::string param)
{
}

int AsmCodeGen2::add(std::string line)
{
}

void AsmCodeGen2::push(int n)
{
  add("push dword " + to_string(n)); // todo
}

void AsmCodeGen2::pop(ValueSize size)
{
  add("add esp, " + to_string(WORD_SIZE));
}

void AsmCodeGen2::binop(Opcode op)
{
  add(C_POP_CX);
  add(C_POP_BX);
  add(op, C_BX, C_CX);
  add(C_PUSH_BX);
}

void AsmCodeGen2::add(ValueSize l, ValueSize r)
{
  binop(C_ADD);
}

void AsmCodeGen2::sub(ValueSize l, ValueSize r)
{
  binop(C_SUB);
}

void AsmCodeGen2::mul()
{
  binop(C_MUL);
}

void AsmCodeGen2::div()
{
  add(C_POP_BX);
  add(C_POP_AX);
  add("cdq");
  add("idiv ebx");
  add(C_PUSH_AX);
}

void AsmCodeGen2::cmpop(Opcode op)
{
  add(C_POP_CX);
  add(C_POP_BX);
  add(C_CMP_BX_CX);
  add(op);
  add(C_AND_AX);
  add(C_PUSH, C_AX);
}

void AsmCodeGen2::cmp(ValueSize size, Keyword op)
{
  switch (op) {
  case LT:
    cmpop(C_SETL);
    break;
  case GT:
    cmpop(C_SETG);
    break;
  case LE:
    cmpop(C_SETLE);
    break;
  case GE:
    cmpop(C_SETGE);
    break;
  case EQ:
    cmpop(C_SETE);
    break;
  case NE:
    cmpop(C_SETNE);
    break;
  }
}

void AsmCodeGen2::index(ValueSize size)
{
  add(C_POP_CX);
  add(C_POP_BX);
  add(C_LEA, "[ebx + " + to_string(WORD_SIZE) + " * ecx]");
  add(C_PUSH, R_AX);
}

void AsmCodeGen2::enter(int stackSize)
{
  add("push ebp");
  add("mov ebp, esp");
  if (stackSize > 0) {
    add("sub esp, " + to_string(stackSize));
  }
}

void AsmCodeGen2::call(int paramSize)
{
  add(C_POP, R_CX);
  add(C_CALL);
  if (paramSize > 0) {
    add("add esp, " + to_string(paramSize));
  }
  add(C_PUSH, R_AX);
}

void AsmCodeGen2::ret()
{
  add(C_POP_AX);
  add("mov esp, ebp");
  add("pop ebp");
  add("ret");
}

void AsmCodeGen2::mov(ValueSize size)
{
  add(C_POP_CX);
  add(C_POP_BX);
  add(C_MOV_PBX_CX);
}

static std::string to_string(Addr *addr)
{
  if (addr == NULL)
    return "";
  switch (addr->type) {
  case O_GLOBAL:
  case O_CODE:
    return "[" + addr->name + "]";
  case O_PARAMS:
    return "[ebp + " + to_string(addr->offset + WORD_SIZE * 2) + "]";
  case O_LOCAL:
    return "[ebp - " + to_string(addr->offset) + "]";
  }
  return "";
}

int AsmCodeGen2::lea(Addr *addr)
{
  int ret = add(C_LEA, to_string(addr));
  add(C_PUSH, R_AX);
  return ret;
}

void AsmCodeGen2::ld(ValueSize size)
{
  add(C_POP, R_BX);
  add(C_PUSH_PBX);
}

void AsmCodeGen2::cast(ValueSize from, ValueSize to)
{
}

int AsmCodeGen2::jz(ValueSize size)
{
  add(C_POP_BX);
  add(C_CMP_BX_0);	
  return add(C_JZ);
}

int AsmCodeGen2::jmp()
{
  return add(C_JMP);
}

void AsmCodeGen2::print(std::string format)
{
  if (format == "c") {
    add("call _printChar");
  } else if (format == "s") {
    add("call _printString");
    pop(S_PTR);
  } else if (format == "d") {
    add("call _printNum");
    pop(S_INT);
  } else if (format == "p") {
    cast(S_PTR, S_INT);
    add("call _printNum");
    pop(S_INT);
  }
}

void AsmCodeGen2::println()
{
  add("push byte 10");
  add("call _printChar");
}

void AsmCodeGen2::setJump(int instruction, int addr)
{
  lines[instruction] += " _lbl" + to_string(addr);
}

void AsmCodeGen2::setAddr(int instruction, Addr *addr)
{
  lines[instruction] += to_string(addr);
}

void AsmCodeGen2::start()
{
  lines.push_back("global _start");
  lines.push_back("");
  lines.push_back("extern _GetStdHandle@4");
  lines.push_back("extern _WriteConsoleA@20");
  lines.push_back("extern _ExitProcess@4");
}

void AsmCodeGen2::data()
{
  lines.push_back("");
  lines.push_back("section .bss");
  add("_numCharsWritten: resd 1");
}

void AsmCodeGen2::global(std::string name, int size)
{
  add(name + ": resb " + to_string(size));
}

void AsmCodeGen2::code()
{
  lines.push_back("");
  lines.push_back("section .text");
}

void AsmCodeGen2::function(std::string name)
{
  lines.push_back("");
  add(name + ":");
}

void AsmCodeGen2::main()
{
  lines.push_back("");
  add("_start:");
}

void AsmCodeGen2::finish()
{
  add("push dword 0"); // todo: get from last var
  add("call _ExitProcess@4");
  lines.push_back("");
  add("_printChar:");
  add("push dword -11");
  add("call _GetStdHandle@4");
  add("lea  ebx, [esp + 4]");
  add("push dword 0");
  add("push _numCharsWritten");
  add("push dword 1");
  add("push ebx");
  add("push eax");
  add("call _WriteConsoleA@20");
  add("ret 4");
}

static int lblCount = 1;

int AsmCodeGen2::end()
{
  int l = lblCount++;
  add("_lbl" + to_string(l) + ":");
  return l;
}

void AsmCodeGen::print(const char *output)
{
  FILE *out = output == NULL ? stdout : fopen(output, "w+t");
  for (unsigned i = 0; i < lines.size(); i++) {
    fprintf(out, "%s\n", lines[i].c_str());
  }
  if (output) {
    fclose(out);
  }
}

Commits for excompiler/trunk/src/asmcode2.cpp

Diff revisions: vs.
Revision Author Commited Message
6 osobolev picture osobolev Sat 07 Dec, 2013 20:59:37 +0000

Experimental optimizer