History

Alter stub generation code in limbo compiler to target DisVM instead of Inferno OS

arr committed 9 years ago

Commit ef24d071 parent cae8cd31 file · +70 −67Browse files

Unified · Split

limbo/compiler/stubs.b+70−67
@@ -86,10 +86,19 @@ modcode(globals: ref Decl) · 85 unmodified lines
8686 print("#include \"%s.h\"\n", buf);
8787 }
8888 else{
89− print("#include <lib9.h>\n");
90− print("#include <isa.h>\n");
91− print("#include <interp.h>\n");
89+ print("#include <memory.h>\n");
90+ print("#include <disvm.h>\n");
91+ print("#include <builtin_module.h>\n");
9292 print("#include \"%smod.h\"\n", emitcode);
93+ print("using disvm::vm_t;\n");
94+ print("using disvm::runtime::type_descriptor_t;\n");
95+ print("using disvm::runtime::vm_registers_t;\n");
96+ print("using disvm::runtime::vm_frame_base_alloc_t;\n");
97+ print("using disvm::runtime::byte_t;\n");
98+ print("using disvm::runtime::word_t;\n");
99+ print("using disvm::runtime::big_t;\n");
100+ print("using disvm::runtime::real_t;\n");
101+ print("using disvm::runtime::pointer_t;\n");
93102 }
94103 print("\n");
95104
@@ -106,7 +115,7 @@ modcode(globals: ref Decl) · 10 unmodified lines
106115 for(id := d.ty.ids; id != nil; id = id.next){
107116 if(id.store == Dtype && id.ty.kind == Tadt){
108117 id.ty = usetype(id.ty);
109− print("Type*\tT_%s;\n", id.sym.name);
118+ print("std::shared_ptr<const type_descriptor_t> T_%s;\n", id.sym.name);
110119 }
111120 }
112121
@@ -116,7 +125,7 @@ modcode(globals: ref Decl) · 3 unmodified lines
116125 if(emitdyn){
117126 for(id = d.ty.ids; id != nil; id = id.next)
118127 if(id.store == Dtype && id.ty.kind == Tadt)
119− print("uchar %s_map[] = %s_%s_map;\n",
128+ print("byte_t %s_map[] = %s_%s_map;\n",
120129 id.sym.name, emitcode, id.sym.name);
121130 }
122131
@@ -126,8 +135,10 @@ modcode(globals: ref Decl) · 3 unmodified lines
126135 if(emitdyn){
127136 for(id = d.ty.ids; id != nil; id = id.next)
128137 if(id.store == Dtype && id.ty.kind == Tadt){
129− print("\n%s_%s*\n%salloc%s(void)\n{\n\tHeap *h;\n\n\th = heap(T_%s);\n\treturn H2D(%s_%s*, h);\n}\n", emitcode, id.sym.name, emitcode, id.sym.name, id.sym.name, emitcode, id.sym.name);
130− print("\nvoid\n%sfree%s(Heap *h, int swept)\n{\n\t%s_%s *d;\n\n\td = H2D(%s_%s*, h);\n\tfreeheap(h, swept);\n}\n", emitcode, id.sym.name, emitcode, id.sym.name, emitcode, id.sym.name);
138+ print("\n%s_%s*\n%salloc%s(void)\n{\n vm_alloc_t *a = vm_alloc_t::allocate(T_%s);\n return ::new(a->get_allocation())%s_%s{};\n}\n",
139+ emitcode, id.sym.name, emitcode, id.sym.name, id.sym.name, emitcode, id.sym.name);
140+ print("\nvoid\n%sfree%s(vm_alloc_t *a)\n{\n // Customized deallocation\n //%s_%s *t = a->get_allocation<%s_%s>();\n //t->~%s_%s();\n\n // Default deallocation\n disvm::runtime::dec_ref_count_and_free(a);\n}\n",
141+ emitcode, id.sym.name, emitcode, id.sym.name, emitcode, id.sym.name, emitcode, id.sym.name);
131142 }
132143 }
133144
@@ -138,16 +149,16 @@ modcode(globals: ref Decl) · 4 unmodified lines
138149 print("\nvoid\n%sinit(void)\n{\n", emitcode);
139150 else{
140151 print("\nvoid\n%smodinit(void)\n{\n", emitcode);
141− print("\tbuiltinmod(\"$%s\", %smodtab, %smodlen);\n", emitcode, emitcode, emitcode);
152+ print(" disvm::runtime::builtin::register_module_exports(%s_PATH, %smodtab, %smodlen);\n", emitcode, emitcode, emitcode);
142153 }
143154 for(id = d.ty.ids; id != nil; id = id.next)
144155 if(id.store == Dtype && id.ty.kind == Tadt){
145156 if(emitdyn)
146− print("\tT_%s = dtype(%sfree%s, %s_%s_size, %s_map, sizeof(%s_map));\n",
147− id.sym.name, emitcode, id.sym.name, emitcode, id.sym.name, id.sym.name, id.sym.name);
157+ print(" T_%s = type_descriptor_t::create(%s_%s_size, sizeof(%s_map), %s_map);\n",
158+ id.sym.name, emitcode, id.sym.name, emitcode, id.sym.name);
148159 else
149− print("\tT_%s = dtype(freeheap, sizeof(%s), %smap, sizeof(%smap));\n",
150− id.sym.name, id.sym.name, id.sym.name, id.sym.name);
160+ print(" T_%s = type_descriptor_t::create(sizeof(%s_%s), sizeof(%s_%s_map), %s_%s_map);\n",
161+ id.sym.name, id.dot.sym.name, id.sym.name, id.dot.sym.name, id.sym.name, id.dot.sym.name, id.sym.name);
151162 }
152163 print("}\n");
153164
@@ -155,24 +166,20 @@ modcode(globals: ref Decl) · 1 unmodified line
155166 # end function
156167 #
157168 if(emitdyn){
158− print("\nvoid\n%send(void)\n{\n", emitcode);
159− for(id = d.ty.ids; id != nil; id = id.next)
160− if(id.store == Dtype && id.ty.kind == Tadt)
161− print("\tfreetype(T_%s);\n", id.sym.name);
162− print("}\n");
169+ print("\nvoid\n%send(void)\n{\n}\n", emitcode);
163170 }
164171
165172 #
166173 # stub functions
167174 #
168175 for(id = d.ty.tof.ids; id != nil; id = id.next){
169− print("\nvoid\n%s_%s(void *fp)\n{\n\tF_%s_%s *f = fp;\n",
176+ print("\nvoid\n%s_%s(vm_registers_t &r, vm_t &vm)\n{\n auto &fp = r.stack.peek_frame()->base<F_%s_%s>();\n",
170177 id.dot.sym.name, id.sym.name,
171178 id.dot.sym.name, id.sym.name);
172179 if(id.ty.tof != tnone && tattr[id.ty.tof.kind].isptr){
173− print("\tvoid *r;\n");
174− print("\n\tr = *f->ret;\n\t*f->ret = H;\n\tdestroy(r);\n");
180+ print("\n //disvm::runtime::dec_ref_count_and_free(vm_alloc_t::from_allocation(*fp.ret));\n //*fp.ret = nullptr;\n");
175181 }
182+ print(" throw vm_system_exception{ \"Function not implemented\" };\n");
176183 print("}\n");
177184 }
178185
@@ -182,14 +189,13 @@ modcode(globals: ref Decl) · 3 unmodified lines
182189
183190 modtab(globals: ref Decl)
184191 {
185− print("typedef struct{char *name; long sig; void (*fn)(void*); int size; int np; uchar map[16];} Runtab;\n");
186192 for(d := globals; d != nil; d = d.next){
187193 if(d.store == Dtype && d.ty.kind == Tmodule && d.sym.name == emittab){
188194 n := 0;
189− print("Runtab %smodtab[]={\n", d.sym.name);
195+ print("disvm::runtime::builtin::vm_runtab_t %smodtab[]={\n", d.sym.name);
190196 for(id := d.ty.tof.ids; id != nil; id = id.next){
191197 n++;
192− print("\t\"");
198+ print(" \"");
193199 if(id.dot != d)
194200 print("%s.", id.dot.sym.name);
195201 print("%s\",0x%ux,%s_%s,", id.sym.name, sign(id),
@@ -202,8 +208,8 @@ modtab(globals: ref Decl) · 6 unmodified lines
202208 }
203209 print("\n");
204210 }
205− print("\t0\n};\n");
206− print("#define %smodlen %d\n", d.sym.name, n);
211+ print(" 0\n};\n\n");
212+ print("const %smodlen = %d\n", d.sym.name, n);
207213 }
208214 }
209215 }
@@ -221,14 +227,13 @@ modstub(globals: ref Decl) · 11 unmodified lines
221227 s := id.dot.sym.name + "_" + id.sym.name;
222228 if(emitdyn && id.dot.dot != nil)
223229 s = id.dot.dot.sym.name + "_" + s;
224− print("void %s(void*);\ntypedef struct F_%s F_%s;\nstruct F_%s\n{\n",
225− s, s, s, s);
226− print(" WORD regs[NREG-1];\n");
230+ print("void %s(vm_registers_t &, vm_t &);\nstruct F_%s : public vm_frame_base_alloc_t\n{\n",
231+ s, s);
227232 if(id.ty.tof != tnone)
228− print(" %s* ret;\n", ctypeconv(id.ty.tof));
233+ print(" %s* ret;\n", ctypeconv(id.ty.tof));
229234 else
230− print(" WORD noret;\n");
231− print(" uchar temps[%d];\n", MaxTemp-NREG*IBY2WD);
235+ print(" word_t noret;\n");
236+ print(" byte_t temps[%d];\n", MaxTemp-NREG*IBY2WD);
232237 offset := MaxTemp;
233238 for(m := id.ty.ids; m != nil; m = m.next){
234239 p := "";
@@ -245,13 +250,13 @@ modstub(globals: ref Decl) · 10 unmodified lines
245250 if(offset != m.offset)
246251 yyerror("module stub must not contain data objects");
247252 # fatal("modstub bad offset");
248− print(" %s %s;\n", ctypeconv(t), p);
253+ print(" %s %s;\n", ctypeconv(t), p);
249254 arg++;
250255 offset += t.size;
251256 #ZZZ need to align?
252257 }
253258 if(id.ty.varargs != byte 0)
254− print(" WORD vargs;\n");
259+ print(" byte_t vargs;\n");
255260 print("};\n");
256261 }
257262 for(id = d.ty.ids; id != nil; id = id.next)
@@ -262,10 +267,9 @@ modstub(globals: ref Decl) · 4 unmodified lines
262267
263268 chanstub(in: string, id: ref Decl)
264269 {
265− print("typedef %s %s_%s;\n", ctypeconv(id.ty.tof), in, id.sym.name);
270+ print("using %s_%s = %s;\n", in, id.sym.name, ctypeconv(id.ty.tof));
266271 desc := mktdesc(id.ty.tof);
267− print("#define %s_%s_size %d\n", in, id.sym.name, desc.size);
268− print("#define %s_%s_map %s\n", in, id.sym.name, mapconv(desc));
272+ print("const byte_t %s_%s_map = %s;\n", in, id.sym.name, mapconv(desc));
269273 }
270274
271275 #
@@ -287,13 +291,13 @@ adtstub(globals: ref Decl) · 15 unmodified lines
287291 s := dotprint(d.ty.decl, '_');
288292 case d.ty.kind{
289293 Tadt =>
290− print("typedef struct %s %s;\n", s, s);
294+ ;
291295 Tint or
292296 Tbyte or
293297 Treal or
294298 Tbig or
295299 Tfix =>
296− print("typedef %s %s;\n", ctypeconv(t), s);
300+ print("using %s = %s;\n", ctypeconv(t), s);
297301 }
298302 }
299303 }
@@ -319,12 +323,12 @@ adtstub(globals: ref Decl) · 19 unmodified lines
319323 (offset, nil) = stubalign(offset, tt.align, nil);
320324 if(offset != id.offset)
321325 fatal("adtstub bad offset");
322− print(" %s %s;\n", ctypeconv(tt), id.sym.name);
326+ print(" %s %s;\n", ctypeconv(tt), id.sym.name);
323327 offset += tt.size;
324328 }
325329 }
326330 if(t.ids == nil){
327− print(" char dummy[1];\n");
331+ print(" char dummy[1];\n");
328332 offset = 1;
329333 }
330334 (offset, nil)= stubalign(offset, t.align, nil);
@@ -345,8 +349,7 @@ adtstub(globals: ref Decl) · 14 unmodified lines
345349 desc := mktdesc(t);
346350 if(offset != desc.size && t.ids != nil)
347351 fatal("adtstub: bad desc size");
348− print("#define %s_size %d\n", s, offset);
349− print("#define %s_map %s\n", s, mapconv(desc));
352+ print("const byte_t %s_map[] = %s;\n", s, mapconv(desc));
350353 #ZZZ
351354 if(0)
352355 print("struct %s_check {int s[2*(sizeof(%s)==%s_size)-1];};\n", s, s, s);
@@ -367,9 +370,9 @@ stubalign(offset: int, a: int, s: string): (int, string) · 14 unmodified lines
367370 return (offset, s);
368371 x = a - x;
369372 if(s != nil)
370− s += sprint("uchar\t_pad%d[%d]; ", offset, x);
373+ s += sprint("byte_t _pad%d[%d]; ", offset, x);
371374 else
372− print("\tuchar\t_pad%d[%d];\n", offset, x);
375+ print(" byte_t _pad%d[%d];\n", offset, x);
373376 offset += x;
374377 if((offset & (a-1)) || x >= a)
375378 fatal("compiler stub misalign");
@@ -381,16 +384,16 @@ constub(id: ref Decl) · 5 unmodified lines
381384 s := id.dot.sym.name + "_" + id.sym.name;
382385 case id.ty.kind{
383386 Tbyte =>
384− print("#define %s %d\n", s, int id.init.c.val & 16rff);
387+ print("const byte_t %s = %d;\n", s, int id.init.c.val & 16rff);
385388 Tint or
386389 Tfix =>
387− print("#define %s %d\n", s, int id.init.c.val);
390+ print("const word_t %s = %#x;\n", s, int id.init.c.val);
388391 Tbig =>
389− print("#define %s %bd\n", s, id.init.c.val);
392+ print("const big_t %s %bd;\n", s, id.init.c.val);
390393 Treal =>
391− print("#define %s %g\n", s, id.init.c.rval);
394+ print("const real_t %s = %.16g;\n", s, id.init.c.rval);
392395 Tstring =>
393− print("#define %s \"%s\"\n", s, id.init.decl.sym.name);
396+ print("const char *%s = \"%s\";\n", s, id.init.decl.sym.name);
394397 }
395398 }
396399
@@ -422,20 +425,20 @@ ckindname := array[Tend] of · 25 unmodified lines
422425 Tnone => "void",
423426 Tadt => "struct",
424427 Tadtpick => "?adtpick?",
425− Tarray => "Array*",
426− Tbig => "LONG",
427− Tbyte => "BYTE",
428− Tchan => "Channel*",
429− Treal => "REAL",
428+ Tarray => "vm_array_t",
429+ Tbig => "big_t",
430+ Tbyte => "byte_t",
431+ Tchan => "vm_channel_t",
432+ Treal => "real_t",
430433 Tfn => "?fn?",
431− Tint => "WORD",
432− Tlist => "List*",
433− Tmodule => "Modlink*",
434+ Tint => "word_t",
435+ Tlist => "vm_list_t",
436+ Tmodule => "vm_module_ref_t",
434437 Tref => "?ref?",
435− Tstring => "String*",
438+ Tstring => "vm_string_t",
436439 Ttuple => "?tuple?",
437440 Texception => "?exception",
438− Tfix => "WORD",
441+ Tfix => "word_t",
439442 Tpoly => "void*",
440443
441444 Tainit => "?ainit?",
@@ -463,29 +466,29 @@ ctypeconv(t: ref Type): string · 21 unmodified lines
463466 Terror =>
464467 return "type error";
465468 Tref =>
466− s = ctypeconv(t.tof);
467− s += "*";
468− Tarray or
469− Tlist or
469+ return "/* " + ctypeconv(t.tof) + " */ pointer_t";
470470 Tint or
471471 Tbig or
472− Tstring or
473472 Treal or
474473 Tbyte or
475474 Tnone or
476475 Tany or
477− Tchan or
478− Tmodule or
479476 Tfix or
480477 Tpoly =>
481478 return ckindname[t.kind];
479+ Tarray or
480+ Tlist or
481+ Tstring or
482+ Tchan or
483+ Tmodule =>
484+ return "/* " + ckindname[t.kind] + " */ pointer_t";
482485 Tadtpick =>
483486 return ctypeconv(t.decl.dot.ty);
484487 Tadt or
485488 Ttuple =>
486489 if(t.decl.sym != anontupsym)
487490 return dotprint(t.decl, '_');
488− s += "struct{ ";
491+ s += "struct { ";
489492 offset := 0;
490493 for(id := t.ids; id != nil; id = id.next){
491494 tt := id.ty;