-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimcode.hpp
More file actions
49 lines (44 loc) · 906 Bytes
/
imcode.hpp
File metadata and controls
49 lines (44 loc) · 906 Bytes
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
#ifndef IMCODE_HPP
#define IMCODE_HPP
#include <iostream>
using MyLabel = std::string;
enum OpCode {
ADD,
SUB,
MUL,
DIV,
JEQ,
JL,
JG,
JNE,
};
static const char *opcode_name[] = {
"add", "sub", "mul", "div", "jeq", "jl", "jg", "jne"
};
struct TranslateManager {
MyLabel createTempId() {
char temp[10];
sprintf(temp, "v%d", cnt_temp_id++);
return temp;
}
MyLabel createLabel() {
char temp[10];
sprintf(temp, "l%d", cnt_label++);
return temp;
}
MyLabel immediate(int imm) {
char temp[100];
sprintf(temp, "%d", imm);
return temp;
}
void writeLabel(MyLabel label) {
std::cout << label << ": ";
}
void write(OpCode opcode, MyLabel op1, MyLabel op2, MyLabel dest) {
std::cout << opcode_name[(int) opcode] << " " << op1 << " " << op2 << " " << dest << std::endl;
}
private:
int cnt_temp_id = 0;
int cnt_label = 0;
};
#endif