Debian Codegraph @

terps/tads/tads3/tcjs.h

raw symbols SCIP
1 /* Copyright (c) 2009 by Michael J. Roberts. All Rights Reserved. */
2 /*
3 Name
4 tcjs.h - tads 3 compiler - javascript code generator
5 Function
6
7 Notes
8
9 Modified
10 02/17/09 MJRoberts - Creation
11 */
12
13 #ifndef TCJS_H
14 #define TCJS_H
15
16 #include <assert.h>
17
18 /* include the Javascript code generator final parse node classes */
19 #include "tcjsdrv.h"
20
21 // $$$ deprecated - build scaffolding only
22 #include "vmop.h"
23
24
25 /* ------------------------------------------------------------------------ */
26 /*
27 * JS expression stack element
28 */
29 class js_expr_ele
30 {
31 public:
32 js_expr_ele(class js_expr_buf &expr, js_expr_ele *tos);
33
34 ~js_expr_ele()
35 {
36 t3free(buf);
37 }
38
39 const char *getbuf() const { return buf; }
40 size_t getlen() const { return len; }
41
42 js_expr_ele *getnxt() const { return nxt; }
43
44 protected:
45 /* store a string */
46 void init(const char *txt, size_t txtlen)
47 {
48 /* allocate our buffer */
49 buf = (char *)t3malloc(txtlen + 1);
50 if (buf == 0)
51 err_throw(TCERR_CODEGEN_NO_MEM);
52
53 /* copy the data */
54 memcpy(buf, txt, txtlen);
55
56 /* null-terminate it */
57 buf[txtlen] = '\0';
58
59 /* remember the length */
60 len = txtlen;
61 }
62
63 /* take ownership of a buffer */
64 void assume_buffer(char *buf, size_t len)
65 {
66 this->buf = buf;
67 this->len = len;
68 }
69
70 /* our buffer */
71 char *buf;
72
73 /* length of our string */
74 size_t len;
75
76 /* next deeper stack element */
77 js_expr_ele *nxt;
78 };
79
80
81 /*
82 * Javascript expression generation buffer
83 */
84 class js_expr_buf
85 {
86 public:
87 js_expr_buf()
88 {
89 /* start with the initial buffer */
90 buf = init_buf;
91 alo = init_buf_size;
92
93 /* there's nothing in the buffer yet */
94 buf[0] = '\0';
95 used = 0;
96 }
97
98 ~js_expr_buf()
99 {
100 /* if the buffer is allocated, free it */
101 if (buf != init_buf)
102 t3free(buf);
103 }
104
105 /* append data */
106 void append(const char *txt, size_t len)
107 {
108 /* ensure we have enough space */
109 if (used + len + 1 > alo)
110 {
111 /* expand the buffer to make room, and then some */
112 size_t new_alo = used + len + alo/2;
113
114 /* allocate a new buffer */
115 char *new_buf = (char *)t3malloc(new_alo);
116
117 /* throw an error on failure */
118 if (new_buf == 0)
119 err_throw(TCERR_CODEGEN_NO_MEM);
120
121 /* copy the existing data (including the trailing null) */
122 memcpy(new_buf, buf, used + 1);
123
124 /* if we had an allocated buffer, delete it */
125 if (buf != init_buf)
126 t3free(buf);
127
128 /* switch to the new buffer */
129 buf = new_buf;
130 alo = new_alo;
131 }
132
133 /* copy the text */
134 memcpy(buf + used, txt, len);
135
136 /* adjust the length counter */
137 used += len;
138
139 /* keep the buffer null-terminated */
140 buf[used] = '\0';
141 }
142
143 void append(char c) { append(&c, 1); }
144 void append(const char *txt) { append(txt, txt != 0 ? strlen(txt) : 0); }
145 void append(const js_expr_buf *j) { append(j->buf, j->used); }
146
147 /* some operator overloading for convenient notation */
148 js_expr_buf *const operator +=(const char *txt)
149 {
150 append(txt);
151 return this;
152 }
153 js_expr_buf *const operator += (char c)
154 {
155 append(&c, 1);
156 return this;
157 }
158 js_expr_buf *const operator += (const js_expr_buf &j)
159 {
160 append(j.buf, j.used);
161 return this;
162 }
163 js_expr_buf *const operator += (const js_expr_buf *j)
164 {
165 append(j->buf, j->used);
166 return this;
167 }
168 js_expr_buf *const operator += (const js_expr_ele *ele)
169 {
170 append(ele->getbuf(), ele->getlen());
171 return this;
172 }
173 operator const char *()
174 {
175 return buf;
176 }
177
178 /* get the buffer information */
179 const char *getbuf() const { return buf; }
180 size_t getlen() const { return used; }
181
182 /* yield ownership of our buffer */
183 char *yield_buffer()
184 {
185 char *ret;
186
187 /* if the buffer was not allocated, make an allocated copy */
188 if (buf == init_buf)
189 {
190 /* make a new copy */
191 char *new_buf = (char *)t3malloc(used + 1);
192 if (new_buf == 0)
193 err_throw(TCERR_CODEGEN_NO_MEM);
194
195 /* copy the data */
196 memcpy(new_buf, buf, used + 1);
197
198 /* return the new buffer */
199 ret = new_buf;
200 }
201 else
202 {
203 /* our buffer is already allocated, so just return it */
204 ret = buf;
205 }
206
207 /* revert to our initial built-in buffer */
208 buf = init_buf;
209
210 /* clear the buffer */
211 buf[0] = '\0';
212 used = 0;
213 alo = init_buf_size;
214 }
215
216 protected:
217
218 /*
219 * initial buffer - this is allocated as part of the initial structure
220 * to save malloc overhead
221 */
222 static const size_t init_buf_size = 512;
223 char init_buf[init_buf_size];
224
225 /* buffer */
226 char *buf;
227
228 /* buffer space allocated/used */
229 size_t alo;
230 size_t used;
231 };
232
233
234 /* ------------------------------------------------------------------------ */
235 /*
236 * T3-specific code generator helper class. This class provides a set
237 * of static functions that are useful for T3 code generation.
238 */
239 class CTcGenTarg
240 {
241 public:
242 /* initialize the code generator */
243 CTcGenTarg();
244
245 /* destroy the code generator object */
246 ~CTcGenTarg();
247
248 /*
249 * Push a javascript expression. The string can be a printf-style
250 * format string, with varargs following. First, the printf formatting
251 * is applied to generate the control string. Then, we expand '$'
252 * expressions:
253 *
254 * $1 = the top-of-stack code expression
255 *. $2 = the 2nd item on the stack
256 *. $n = the nth item on the stack
257 *
258 * ${disc n} = substitute an empty string, but mark the nth stack item
259 * for discarding at the end of the process (see below) as though it
260 * had been mentioned in a regular $n expression
261 *
262 * ${args n m} = the 'm' stack elements starting at $n, concatenated
263 * together into an argument list (with commas as separators)
264 *
265 * ${,args n m} = same as ${args}, but start the list with a comma if
266 * it's non empty, otherwise generate nothing
267 *
268 * ${args[] n m varargs} = same as ${args}, but enclose the list in
269 * square brackets to make it an array at run-time. If varargs is
270 * true, we already have a varargs list on the stack.
271 *
272 * $$ = a single literal '$'
273 *
274 * After expanding the '$' expressions, we pop the stack expressions
275 * used. We pop every stack item up to and including the highest
276 * numbered item used in a '$' expression - even if an item is never
277 * used, it will be discarded if it's on the stack after an item that
278 * was mentioned.
279 *
280 * Finally, the expanded expression string that results from the above
281 * substitutions is pushed onto the expression stack.
282 */
283 void js_expr(const char *str, ...);
284
285 /* allocate a new global property ID */
286 tctarg_prop_id_t new_prop_id() { return next_prop_++; }
287
288 /* allocate a new global object ID */
289 tctarg_obj_id_t new_obj_id() { return next_obj_++; }
290
291 /*
292 * Note constant data sizes. This doesn't affect us, since we don't
293 * have to worry about dividing anything up into pages the way the T3
294 * code generator does.
295 */
296 void note_str(size_t) { }
297 void note_list(size_t) { }
298
299 /* get the maximum string/list/bytecode lengths */
300 size_t get_max_str_len() const { return 0; }
301 size_t get_max_list_cnt() const { return 0; }
302 size_t get_max_bytecode_len() const { return 0; }
303
304 /*
305 * Add a debug line record. We don't keep debugging information, so we
306 * can just ignore this.
307 */
308 void add_line_rec(class CTcTokFileDesc *, long) { }
309
310 /*
311 * Add a function set to the dependency table - returns the index of
312 * the function set in the table
313 */
314 int add_fnset(const char *fnset_extern_name, size_t len);
315 int add_fnset(const char *fnset_extern_name)
316 { return add_fnset(fnset_extern_name, strlen(fnset_extern_name)); }
317
318 /* get the name of a function set given its index */
319 const char *get_fnset_name(int idx) const;
320
321 /* get the number of defined function sets */
322 int get_fnset_cnt() const { return fnset_cnt_; }
323
324 /*
325 * Find a metaclass entry, adding it if it's not already there. If the
326 * metaclass is already defined, and it has an associated symbol, we
327 * will not change the associated symbol - this will let the caller
328 * detect that the metaclass has been previously defined for a
329 * different symbol, which is usually an error.
330 */
331 int find_or_add_meta(const char *nm, size_t len,
332 class CTcSymMetaclass *sym);
333
334 /* get/set the symbol for a given metaclass */
335 class CTcSymMetaclass *get_meta_sym(int meta_idx);
336 void set_meta_sym(int meta_idx, class CTcSymMetaclass *sym);
337
338 /* get the number of metaclasses */
339 int get_meta_cnt() const { return meta_cnt_; }
340
341 /* get the external (universally unique) name for the given metaclass */
342 const char *get_meta_name(int idx) const;
343
344 /*
345 * Notify the code generator that we're replacing an object (via the
346 * "replace" statement) at the given stream offset. We'll mark the
347 * data in the stream as deleted so that we don't write it to the image
348 * file.
349 */
350 void notify_replace_object(ulong stream_ofs);
351
352 /*
353 * Notify the code generator that parsing is finished. This should be
354 * called after parsing and before code generation begins.
355 */
356 void parsing_done();
357
358 /* generate synthesized code during linking */
359 void build_synthesized_code();
360
361 /* generate code for a dictionary object */
362 void gen_code_for_dict(class CTcDictEntry *dict);
363
364 /* generate code for a grammar production object */
365 void gen_code_for_gramprod(class CTcGramProdEntry *prod);
366
367 /*
368 * Write to an object file. The compiler calls this after all parsing
369 * and code generation are completed to write an object file, which can
370 * then be linked with other object files to create an image file.
371 */
372 void write_to_object_file(class CVmFile *object_fp,
373 class CTcMake *make_obj);
374
375 /*
376 * Load an object file. Returns zero on success, non-zero on error.
377 */
378 int load_object_file(CVmFile *fp, const textchar_t *fname);
379
380 /*
381 * Write the image file. The compiler calls this after all parsing and
382 * code generation are completed to write an image file. We must apply
383 * all fixups, assign the code and constant pool layouts, and write the
384 * data to the image file.
385 */
386 void write_to_image(class CVmFile *image_fp, uchar data_xor_mask,
387 const char tool_data[4]);
388
389 /*
390 * $$$ deprecated - temporary build scaffolding only
391 */
392 void write_op(int op) { assert(FALSE); }
393 void write_callprop(int argc, int varargs, int propno) { assert(FALSE); }
394 void note_push(int n = 1) { assert(FALSE); }
395 void note_pop(int n = 1) { assert(FALSE); }
396
397 /*
398 * Debugger mode/Speculative debugger evaluation? We don't (currently)
399 * have any debugger integration possible in the js version, so this
400 * isn't relevant.
401 */
402 int is_eval_for_debug() const { return FALSE; }
403 int is_speculative() const { return FALSE; }
404 int get_debug_stack_level() const { return 0; }
405
406 /* push/pop expressions to/from the js stack */
407 void push_js_expr(js_expr_buf &buf);
408 void pop_js_expr() { pop_js_exprs(1); }
409 void pop_js_exprs(int n);
410 int get_sp_depth() const { return expr_stack_depth; }
411
412 /* pop the js expression stack, appending the top of stack to a buffer */
413 void pop_js_expr(js_expr_buf &buf)
414 {
415 copy_js_expr(buf, 1);
416 pop_js_expr();
417 }
418
419 /* copy the contents of stack[n] to a buffer */
420 void copy_js_expr(js_expr_buf &dst, int n);
421
422 protected:
423 /* expression stack head */
424 js_expr_ele *expr_stack;
425 int expr_stack_depth;
426
427 /* property/object ID allocators */
428 tctarg_prop_id_t next_prop_;
429 tctarg_obj_id_t next_obj_;
430
431 /* metaclass tracking */
432 int meta_cnt_;
433
434 /* function set tracking */
435 int fnset_cnt_;
436 };
437
438
439 #endif /* TCJS_H */