-
Notifications
You must be signed in to change notification settings - Fork 2
/
code_generator.cpp
79 lines (66 loc) · 1.64 KB
/
code_generator.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
#include ".\code_generator.h"
#include "generator_operand.h"
#include "state.h"
#include <iostream>
#include "base_operand.h"
#include "base_parameter.h"
#include "common.h"
#include "registers.h"
#include "source_parser.h"
#include "exceptions.h"
#include "configuration.h"
#include "junk_manager.h"
code_generator::code_generator(void)
{
}
code_generator::~code_generator(void)
{
}
void code_generator::generate()
{
int min = configuration::instance().intvalue(config::min_operand);
int max = configuration::instance().intvalue(config::max_operand);
int i=0;
int number=0;
number = min + rand() % (max-min);
base_operand * baseop=0;
registers::instance().init();
state& mState=state::instance();
std::fstream *output = source_parser::instance().output_macro();
while(i<number)
{
try
{
if(!(rand()%configuration::instance().intvalue(config::junk_template)))
*output << junk_manager::instance().random_junk() << std::endl;
baseop=generator_operand::instance().generate();
*output << baseop->tostr();
//std::cout << "G " <<baseop->tostr() << std::endl;
if(baseop->isprotected())
mState.add_op(baseop);
else
delete this;
}
catch(p_exception *exp)
{
switch(exp->id())
{
case excp::NO_REGISTER_FREE :
mState.flush_list();
registers::instance().init();
break;
case excp::STACK_OVERFLOW:
mState.flush_list();
registers::instance().init();
break;
case excp::ILLEGAL_OPERAND :
//std::cout << ";Illegal Operand!\n";
*output << ";ILLEGAL_OPERAND!"<<std::endl;
break;
default :std::cout << "Exception caught" << exp->id() << "\n";
}
}
i++;
}
mState.flush_list();
}