| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#ifndef TCJS_H |
| 14 |
#define TCJS_H |
| 15 |
|
| 16 |
#include <assert.h> |
| 17 |
|
| 18 |
|
| 19 |
#include "tcjsdrv.h" |
| 20 |
|
| 21 |
|
| 22 |
#include "vmop.h" |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 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 |
|
| 46 |
void init(const char *txt, size_t txtlen) |
| 47 |
{ |
| 48 |
|
| 49 |
buf = (char *)t3malloc(txtlen + 1); |
| 50 |
if (buf == 0) |
| 51 |
err_throw(TCERR_CODEGEN_NO_MEM); |
| 52 |
|
| 53 |
|
| 54 |
memcpy(buf, txt, txtlen); |
| 55 |
|
| 56 |
|
| 57 |
buf[txtlen] = '\0'; |
| 58 |
|
| 59 |
|
| 60 |
len = txtlen; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
void assume_buffer(char *buf, size_t len) |
| 65 |
{ |
| 66 |
this->buf = buf; |
| 67 |
this->len = len; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
char *buf; |
| 72 |
|
| 73 |
|
| 74 |
size_t len; |
| 75 |
|
| 76 |
|
| 77 |
js_expr_ele *nxt; |
| 78 |
}; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
class js_expr_buf |
| 85 |
{ |
| 86 |
public: |
| 87 |
js_expr_buf() |
| 88 |
{ |
| 89 |
|
| 90 |
buf = init_buf; |
| 91 |
alo = init_buf_size; |
| 92 |
|
| 93 |
|
| 94 |
buf[0] = '\0'; |
| 95 |
used = 0; |
| 96 |
} |
| 97 |
|
| 98 |
~js_expr_buf() |
| 99 |
{ |
| 100 |
|
| 101 |
if (buf != init_buf) |
| 102 |
t3free(buf); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
void append(const char *txt, size_t len) |
| 107 |
{ |
| 108 |
|
| 109 |
if (used + len + 1 > alo) |
| 110 |
{ |
| 111 |
|
| 112 |
size_t new_alo = used + len + alo/2; |
| 113 |
|
| 114 |
|
| 115 |
char *new_buf = (char *)t3malloc(new_alo); |
| 116 |
|
| 117 |
|
| 118 |
if (new_buf == 0) |
| 119 |
err_throw(TCERR_CODEGEN_NO_MEM); |
| 120 |
|
| 121 |
|
| 122 |
memcpy(new_buf, buf, used + 1); |
| 123 |
|
| 124 |
|
| 125 |
if (buf != init_buf) |
| 126 |
t3free(buf); |
| 127 |
|
| 128 |
|
| 129 |
buf = new_buf; |
| 130 |
alo = new_alo; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
memcpy(buf + used, txt, len); |
| 135 |
|
| 136 |
|
| 137 |
used += len; |
| 138 |
|
| 139 |
|
| 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 |
|
| 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 |
|
| 179 |
const char *getbuf() const { return buf; } |
| 180 |
size_t getlen() const { return used; } |
| 181 |
|
| 182 |
|
| 183 |
char *yield_buffer() |
| 184 |
{ |
| 185 |
char *ret; |
| 186 |
|
| 187 |
|
| 188 |
if (buf == init_buf) |
| 189 |
{ |
| 190 |
|
| 191 |
char *new_buf = (char *)t3malloc(used + 1); |
| 192 |
if (new_buf == 0) |
| 193 |
err_throw(TCERR_CODEGEN_NO_MEM); |
| 194 |
|
| 195 |
|
| 196 |
memcpy(new_buf, buf, used + 1); |
| 197 |
|
| 198 |
|
| 199 |
ret = new_buf; |
| 200 |
} |
| 201 |
else |
| 202 |
{ |
| 203 |
|
| 204 |
ret = buf; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
buf = init_buf; |
| 209 |
|
| 210 |
|
| 211 |
buf[0] = '\0'; |
| 212 |
used = 0; |
| 213 |
alo = init_buf_size; |
| 214 |
} |
| 215 |
|
| 216 |
protected: |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
static const size_t init_buf_size = 512; |
| 223 |
char init_buf[init_buf_size]; |
| 224 |
|
| 225 |
|
| 226 |
char *buf; |
| 227 |
|
| 228 |
|
| 229 |
size_t alo; |
| 230 |
size_t used; |
| 231 |
}; |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
class CTcGenTarg |
| 240 |
{ |
| 241 |
public: |
| 242 |
|
| 243 |
CTcGenTarg(); |
| 244 |
|
| 245 |
|
| 246 |
~CTcGenTarg(); |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
void js_expr(const char *str, ...); |
| 284 |
|
| 285 |
|
| 286 |
tctarg_prop_id_t new_prop_id() { return next_prop_++; } |
| 287 |
|
| 288 |
|
| 289 |
tctarg_obj_id_t new_obj_id() { return next_obj_++; } |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
void note_str(size_t) { } |
| 297 |
void note_list(size_t) { } |
| 298 |
|
| 299 |
|
| 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 |
|
| 306 |
|
| 307 |
|
| 308 |
void add_line_rec(class CTcTokFileDesc *, long) { } |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 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 |
|
| 319 |
const char *get_fnset_name(int idx) const; |
| 320 |
|
| 321 |
|
| 322 |
int get_fnset_cnt() const { return fnset_cnt_; } |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
int find_or_add_meta(const char *nm, size_t len, |
| 332 |
class CTcSymMetaclass *sym); |
| 333 |
|
| 334 |
|
| 335 |
class CTcSymMetaclass *get_meta_sym(int meta_idx); |
| 336 |
void set_meta_sym(int meta_idx, class CTcSymMetaclass *sym); |
| 337 |
|
| 338 |
|
| 339 |
int get_meta_cnt() const { return meta_cnt_; } |
| 340 |
|
| 341 |
|
| 342 |
const char *get_meta_name(int idx) const; |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
void notify_replace_object(ulong stream_ofs); |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
void parsing_done(); |
| 357 |
|
| 358 |
|
| 359 |
void build_synthesized_code(); |
| 360 |
|
| 361 |
|
| 362 |
void gen_code_for_dict(class CTcDictEntry *dict); |
| 363 |
|
| 364 |
|
| 365 |
void gen_code_for_gramprod(class CTcGramProdEntry *prod); |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
void write_to_object_file(class CVmFile *object_fp, |
| 373 |
class CTcMake *make_obj); |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
int load_object_file(CVmFile *fp, const textchar_t *fname); |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
void write_to_image(class CVmFile *image_fp, uchar data_xor_mask, |
| 387 |
const char tool_data[4]); |
| 388 |
|
| 389 |
|
| 390 |
|
| 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 |
|
| 399 |
|
| 400 |
|
| 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 |
|
| 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 |
|
| 413 |
void pop_js_expr(js_expr_buf &buf) |
| 414 |
{ |
| 415 |
copy_js_expr(buf, 1); |
| 416 |
pop_js_expr(); |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
void copy_js_expr(js_expr_buf &dst, int n); |
| 421 |
|
| 422 |
protected: |
| 423 |
|
| 424 |
js_expr_ele *expr_stack; |
| 425 |
int expr_stack_depth; |
| 426 |
|
| 427 |
|
| 428 |
tctarg_prop_id_t next_prop_; |
| 429 |
tctarg_obj_id_t next_obj_; |
| 430 |
|
| 431 |
|
| 432 |
int meta_cnt_; |
| 433 |
|
| 434 |
|
| 435 |
int fnset_cnt_; |
| 436 |
}; |
| 437 |
|
| 438 |
|
| 439 |
#endif |