-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutine.cpp
executable file
·80 lines (79 loc) · 2.42 KB
/
routine.cpp
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
#include "routine.h"
#include "enviroment.h"
#include "common.h"
#include "reg.h"
#include "string_data.h"
void func::gencode(){
cout << name << ":" << endl;
routine::gencode();
}
void procedure::gencode(){
cout << name << ":" << endl;
routine::gencode();
}
void routine::gencode(){
if(type != FUNC_ROUTINE && type != PROC_ROUTINE){
cout << "section .data\n";
string_data::single() -> gencode();
cout << "section .text\n";
cout << "global main\n";
cout << "main:\n";
cout << "xor esi, esi" << endl;
cout << "extern printr" << endl;
cout << "extern prints" << endl;
cout << "extern print" << endl;
}
cout << "sub esp, " << header -> v_r -> getstacksize() << endl;//to get the stack variable
enviroment::single() -> insert(this -> header.get());
for(int i = 0; i < stmt_vt -> vt.size(); ++i){
stmt_vt -> vt[i] -> gencode();
}
cout << "add esp, " << header -> v_r -> getstacksize() << endl;//to release the stack variable
if(type == FUNC_ROUTINE || type == PROC_ROUTINE){
cout << "ret" << endl;
}
else{
cout << "mov eax, 1\n";
cout << "mov ebx, 0\n";
cout << "int 80h\n";
}
for(int i = 0; i < header -> r_r -> vt.size(); ++i){
header -> r_r -> vt[i] -> gencode();
}
enviroment::single() -> pop();
}
void routine::add_function_param(){
for(int i = 0; i < header -> r_r -> vt.size(); ++i){
header -> r_r -> vt[i] -> add_function_param();
}
}
void procedure::add_function_param(){
int l = param.size();
shared_ptr <base_type> tmp(new base_type(INT_TYPE));
header -> v_r -> insert_front("~nop5", tmp);
for(int i = l - 1; i >= 0; --i){
header -> v_r -> insert_front(param[i].second.first, param[i].second.second);
}
header -> v_r -> insert_front("~nop1", tmp);
header -> v_r -> insert_front("~nop2", tmp);
header -> v_r -> insert_front("~nop3", tmp);
header -> v_r -> insert_front("~nop4", tmp);
routine::add_function_param();
}
void func::add_function_param(){
int l = param.size();
shared_ptr <base_type> tmp(new base_type(INT_TYPE));
header -> v_r -> insert_front("~nop5", tmp);
for(int i = l - 1; i >= 0; --i){
header -> v_r -> insert_front(param[i].second.first, param[i].second.second);
}
header -> v_r -> insert_front(name, ret);
header -> v_r -> insert_front("~nop1", tmp);
header -> v_r -> insert_front("~nop2", tmp);
header -> v_r -> insert_front("~nop3", tmp);
header -> v_r -> insert_front("~nop4", tmp);
routine::add_function_param();
}
int routine::getsize(){
return header -> v_r -> getsize();
}