Input a code block,
optimize a code block,
output a code block
move r0 r1
?
!
is an operator?
int main() block::code cblock while (block::input(std::cin, cblock)) block::optimize(cblock) block::output(std::cout, cblock)
typedef std::vector<std::string> code; bool input(std::istream & is, code & blk) if !read_nonblank_line(is, str) return false blk.clear() do blk.push_back(str) while getline(is, str) and !is_blank_line(str) return true void optimize(code & blk) { } void output(std::ostream & os, const code & blk) for i = blk.begin(); i != blk.end(); i++ os << *i << "\n"
move a r0 move r0 b
is not the same as
move a r0 ; move r0 b
r0
is involved in a race condition.
move r1 b move r0 a add r2 r0
is not the same as
move r1 b ; add r2 r0 move r0 a
typedef std::vector<statement_set> code;
s
has no conflicts with any statement in the set.
bool statement_set::compatible(statement s)
static bool lift_statements(block::code & blk) bool changed = false; block::code new_blk new_blk.push_back(blk[0]) for (unsigned i = 1; i < blk.size(); i++) const statement_set & old_ss = blk[i] statement_set new_ss; for (unsigned j = 0; j < old_ss.size(); j++) const statement & s = old_ss[j] unsigned home = new_blk.size() while ((home > 0) && new_blk[home - 1].compatible(s)) home-- if (home != new_blk.size()) new_blk[home].add(s) changed = true else ss.add(s) if (!ss.empty()) new_blk.push_back(ss) if (changed) std::swap(blk, new_blk) return changed
static void normalize(block::code & blk) while (normalize_opers(blk) || normalize_size(blk)) { } split_sets(blk);
void optimize(code & blk) lift_statements(blk); normalize(blk);
bool normalize_opers(code & blk) bool changed = false for (i = 0; i < blk.size() - 1; i++) statement_set & ss = blk[i] for (j = 0; j < ss.size(); j++) const statement & s = ss[j] if ( (s.opr() != "move") and (ss.opr_cnt(s.opr()) > 2) and blk[i + 1].compatible(s)) blk[i + 1].add(s); ss.rem(s) changed = true return changed
200 | 300 | 400 | 500 | 600 | 700 | |
---|---|---|---|---|---|---|
0 | 1 | 1 | ||||
10 | 1 | |||||
20 | 1 | |||||
30 | 1 | |||||
40 | ||||||
50 | 1 | 2 | 1 | 1 | ||
60 | 1 | 1 | 3 | 1 | ||
70 | 1 | 1 | ||||
80 | 1 | 1 | ||||
90 | 1 | |||||
Totals | 3 | 4 | 8 | 3 | 0 | 3 |
100 | 200 | 300 | |
---|---|---|---|
0 | 2 | 2 | |
10 | 1 | 2 | 1 |
20 | 1 | ||
30 | 1 | ||
40 | 3 | 1 | |
50 | 2 | ||
60 | 2 | ||
70 | |||
80 | 2 | ||
90 | 1 | ||
Totals | 12 | 7 | 2 |
total | pages per procedure | |||||
---|---|---|---|---|---|---|
procs | 1 | 2 | 3 | 4 | 5 | 6 |
7 | 4 | 3 | ||||
7 | 5 | 1 | 1 | |||
10 | 7 | 2 | 1 | |||
10 | 8 | 1 | 1 | |||
10 | 9 | 1 | ||||
11 | 9 | 1 | 1 | |||
14 | 11 | 2 | 1 | |||
14 | 13 | 1 | ||||
14 | 7 | 5 | 2 | |||
15 | 13 | 1 | 1 | |||
17 | 14 | 2 | 1 | |||
19 | 17 | 2 | ||||
26 | 26 | |||||
27 | 23 | 3 | 1 | |||
28 | 22 | 5 | 1 | |||
36 | 35 | 1 | ||||
IsInstructionValid(vString insvec) { string ins char szInstruction[MAX_STRING] vector<string>::const_iterator it char *szRegisters[8] = {"r0", . . ., "r7"} for (it=insvec.begin(); it !=insvec.end() ; ++it) ins += (*it) strcpy(szInstruction, ins.c_str()) for (int i = 0; i < 8; ++i) if (ins.find(szRegisters[i]) != -1) bregisterFound = true break
When trek(6) reads in keyboard input, a bounds check was not performed correctly. If more than 100 characters are entered, a buffer overrun occurs.
inputfile read_inputfile(istream is, unsigned count, unsigned cnt1) inputfile ss; if (!readline(is, line)) line = ""; ss = new command_row[count + 1]; cnt = cnt1; else ss = read_inputfile(is, count + 1, ++cnt1); ss[count] = line return ss
vector<string> read(istream is) vector<string> input string line while (readline(is, line) input.push_back(line) return input
input.size()
.
const unsigned DEST = 1; const unsigned SRC = 2; if (is_move_opcode()) if (is_register(simple_st[DEST]) && is_register(simple_st[SRC])) return false; else if (!is_register(simple_st[DEST]) && !is_register(simple_st[SRC])) return false; else if (!is_register(simple_st[DEST]) || !is_register(simple_st[SRC])) return false
or
for (i = 0; i < EIGHT; i++) if (vstr[1] == registers[i]) flagd = true; for (i = 0; i < EIGHT; i++) if (vstr[2] == registers[i]) flags = true; flag = flagd || flags; return flag;
bool alpha_test(vector < string > vect) if (vect[0] == "move") if (vect[1] == "r0" || vect[1] == "r1" || vect[1] == "r2" || vect[1] == "r3" || vect[1] == "r4" || vect[1] == "r5" || vect[1] == "r6" || vect[1] == "r7") { if (vect[2] == " r0" || vect[2] == "r1" || vect[2] == " r2" || vect[2] == "r3" || vect[2] == "r4"||vect[2] == "r5" || vect[2] == "r6" || vect[2] == "r7") { } if (vect[2] == "r0" || vect[2] == "r1" || vect[2] == "r2" || vect[2] == "r3" || vect[2] == "r4" || vect[2] == "r5" || vect[2] == "r6" || vect[2] == "r7" ) { } bool move_test(vector < string > vect ) if (vect[0]!="move") for (unsigned int k = 1; k < vect.size();k++) if (vect[k] == "r0" || vect[k] == "r1" || vect[k] == "r2" || vect[k] == "r3" || vect[k] == "r4" || vect[k] == "r5" || vect[k] == "r6" || vect[k] == "r7") { }
vector<string> getInputRows(){ vector<string> vsInput char cInput string sInput while cin.get(cInput) if (cInput != '\n') sInput += cInput else vsInput.push_back(sInput) sInput = "" if (sInput! = "") vsInput.push_back(sInput) return vsInput
This page last modified on 25 October 2002.