Skip to content

Commit 4290481

Browse files
committed
More optimizations to the native compiler, produces smaller editor.
1 parent bd5c00e commit 4290481

File tree

2 files changed

+56
-21
lines changed

2 files changed

+56
-21
lines changed

src/editor.bas

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ PROC InputFilename
6161
pos. 6, 0
6262
poke @CH, 12: ' Force ENTER
6363
input #0, FileName$
64+
key = 0
6465
exit
6566
elif key >= 30 and key <= 124 or key = 126
6667
put key
@@ -79,7 +80,7 @@ PROC SaveCompiledFile
7980
pos. 0, 0
8081
? "œ Name?";
8182
exec InputFileName
82-
if key <> 155
83+
if key
8384
' Don't save
8485
exit
8586
endif
@@ -115,7 +116,7 @@ PROC AskSaveFile
115116
pos. 0, 0
116117
? "œ Save?";
117118
exec InputFileName
118-
if key <> 155
119+
if key
119120
' Don't save
120121
exit
121122
endif
@@ -164,6 +165,7 @@ ENDPROC
164165
' Compile (and run) file
165166
PROC CompileFile
166167
' Compile main file
168+
exec SaveLine
167169
poke MemEnd, $9B
168170
pos. 1,0
169171
? "œ Parsing: ";
@@ -772,10 +774,10 @@ do
772774
pos. 0, 0
773775
? "œ Load?";
774776
exec InputFileName
775-
if key = 155
776-
exec LoadFile
777-
else
777+
if key
778778
exec ShowInfo
779+
else
780+
exec LoadFile
779781
endif
780782
'
781783
'--------- Control-Z (undo) -----

src/native.cc

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,19 @@ class parse {
7979
std::string str;
8080
size_t pos;
8181
size_t max_pos;
82-
std::vector<codew> code;
82+
std::map<std::string, std::vector<codew>> procs;
8383
std::map<std::string, int> vars;
8484
std::map<std::string, int> labels;
8585
std::vector<jump> jumps;
8686
int label_num;
87+
bool finalized;
88+
std::vector<codew> *code;
8789

88-
parse(): lvl(0), maxlvl(0), pos(0), max_pos(0), label_num(0) {}
90+
parse():
91+
lvl(0), maxlvl(0), pos(0),
92+
max_pos(0), label_num(0),
93+
finalized(false),
94+
code(&procs[std::string()]) { }
8995

9096
std::string new_label()
9197
{
@@ -134,20 +140,20 @@ class parse {
134140

135141
saved_pos save()
136142
{
137-
return saved_pos{pos, code.size()};
143+
return saved_pos{pos, code->size()};
138144
}
139145

140146
void restore(saved_pos s)
141147
{
142148
if( pos > max_pos ) max_pos = pos;
143149
pos = s.pos;
144-
code.resize(s.opos);
150+
code->resize(s.opos);
145151
}
146152

147153
codew remove_last()
148154
{
149-
codew ret = code.back();
150-
code.pop_back();
155+
codew ret = code->back();
156+
code->pop_back();
151157
return ret;
152158
}
153159
void debug(const std::string &c)
@@ -251,29 +257,52 @@ class parse {
251257
bool emit_word(std::string s)
252258
{
253259
codew c{ codew::word, s};
254-
code.push_back(c);
260+
code->push_back(c);
255261
return true;
256262
}
257263
bool emit_label(std::string s)
258264
{
259265
codew c{ codew::label, s};
260-
code.push_back(c);
266+
code->push_back(c);
261267
return true;
262268
}
263269
bool comment(std::string s)
264270
{
265271
codew c{ codew::comment, s};
266-
code.push_back(c);
272+
code->push_back(c);
267273
return true;
268274
}
269275
bool emit(std::string s)
270276
{
271277
codew c{ codew::byte, s};
272278
if( s.substr(0,4) == "TOK_" )
273279
c.type = codew::tok;
274-
code.push_back(c);
280+
code->push_back(c);
275281
return true;
276282
}
283+
void push_proc(std::string l)
284+
{
285+
code = &procs[l];
286+
}
287+
void pop_proc(std::string l)
288+
{
289+
code = &procs[std::string()];
290+
}
291+
std::vector<codew> &full_code()
292+
{
293+
std::vector<codew> &p = procs[std::string()];
294+
if( !finalized )
295+
{
296+
finalized = true;
297+
// Correctly terminate main code
298+
if( !p.size() || p.back().type != codew::tok || p.back().value != "TOK_END" )
299+
p.push_back({codew::tok, "TOK_END"});
300+
for(auto &c: procs)
301+
if( !c.first.empty() )
302+
p.insert(std::end(p), std::begin(c.second), std::end(c.second));
303+
}
304+
return p;
305+
}
277306
};
278307

279308
static VarType get_vartype(parse::codew cw)
@@ -482,7 +511,6 @@ static bool SMB_E_PUSH_LT(parse &s)
482511
case LT_WHILE_2:
483512
case LT_FOR_2:
484513
case LT_IF:
485-
case LT_PROC_1:
486514
case LT_DATA:
487515
s.emit_word(l);
488516
break;
@@ -492,6 +520,11 @@ static bool SMB_E_PUSH_LT(parse &s)
492520
case LT_PROC_2:
493521
case LT_LAST_JUMP:
494522
break;
523+
case LT_PROC_1:
524+
// Optimize by switching codep
525+
s.remove_last();
526+
s.push_proc(l);
527+
break;
495528
}
496529
return true;
497530
}
@@ -587,7 +620,7 @@ static bool SMB_E_POP_PROC_1(parse &s)
587620
auto l = s.pop_loop(LT_PROC_1);
588621
if( l.empty() )
589622
return false;
590-
s.emit_label(l);
623+
s.pop_proc(l);
591624
return true;
592625
}
593626

@@ -1117,13 +1150,13 @@ int main(int argc, char **argv)
11171150

11181151
s.emit("TOK_END");
11191152
// Optimize
1120-
peephole pp(s.code);
1153+
peephole pp(s.full_code());
11211154
// Statistics
11221155
if( show_stats )
1123-
opstat op(s.code);
1156+
opstat op(s.full_code());
11241157

11251158
// Write global symbols
1126-
for(auto &c: s.code)
1159+
for(auto &c: s.full_code())
11271160
{
11281161
if( c.type == parse::codew::word && c.value[0] >= 'A' && c.value[0] <= '_' )
11291162
ofile << "\t.global " << c.value << "\n";
@@ -1147,7 +1180,7 @@ int main(int argc, char **argv)
11471180
";-----------------------------\n"
11481181
"; Bytecode\n"
11491182
"bytecode_start:\n";
1150-
for(auto c: s.code)
1183+
for(auto c: s.full_code())
11511184
{
11521185
switch(c.type)
11531186
{

0 commit comments

Comments
 (0)