1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
|
/*
* Copyright (c) 2006-2008 BitMover, Inc.
*/
#ifndef L_COMPILE_H
#define L_COMPILE_H
#include <setjmp.h>
#include "tclInt.h"
#include "tclCompile.h"
#include "Last.h"
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
/* For jump fix-ups. */
typedef struct Jmp Jmp;
struct Jmp {
int op; // jmp instruction bytecode (e.g., INST_JUMP1)
int size; // size of jmp instruction (1 or 4 bytes)
int offset; // bytecode offset of jmp instruction
Jmp *next;
};
/* Semantic stack frame. */
typedef enum {
OUTER = 0x0001, // is outer-most
SCRIPT = 0x0002, // is file scope
TOPLEV = 0x0004, // is for file top-levels
CLS_OUTER = 0x0008, // is class outer-most
CLS_TOPLEV = 0x0010, // is for class top-levels
FUNC = 0x0020, // frame is at top level of a proc
LOOP = 0x0040, // frame is for a loop
SWITCH = 0x0080, // frame is for a switch stmt
SKIP = 0x0100, // skip frame when searching enclosing scopes
SEARCH = 0x0200, // don't skip this frame
KEEPSYMS = 0x0400, // don't free symtab when scope is closed
} Frame_f;
typedef enum {
LABEL_USE = 0x01, // label is being referenced
LABEL_DEF = 0x02, // label is being defined
} Label_f;
typedef struct Label {
char *name;
int offset;
Jmp *fixups;
} Label;
typedef struct Frame {
CompileEnv *envPtr;
CompileEnv *bodyEnvPtr;
CompileEnv *prologueEnvPtr;
Proc *proc;
char *name;
Tcl_HashTable *symtab;
Tcl_HashTable *labeltab;
ClsDecl *clsdecl;
Frame_f flags;
// When a compile frame corresponds to a block in the code, we
// store the AST node of the block here.
Ast *block;
// We collect jump fix-ups for all of the jumps emitted for break and
// continue statements, so that we can stuff in the correct jump targets
// once we're done compiling the loops.
Jmp *continue_jumps;
Jmp *break_jumps;
// Jump fix-up for the jump to the prologue code at the end of a proc,
// and the bytecode offset for the jump back.
Jmp *end_jmp;
int proc_top;
// Fix-ups for return stmts which all jmp to the end.
Jmp *ret_jmps;
// List of temps allocated in this frame.
Tmp *tmps;
struct Frame *prevFrame;
} Frame;
/* Per-scope tables. Scopes are opened and close at parse time. */
typedef struct scope Scope;
struct scope {
Tcl_HashTable *structs;
Tcl_HashTable *typedefs;
Scope *prev;
};
/*
* Global L state. There is only one of these, reachable via L->global.
* The general L command line is
* tclsh [-tclsh_opt1] ... [-tclsh_optn] script_name [-script_opt1] ... [-script_optm]
*/
typedef struct {
int tclsh_argc;
Tcl_Obj *tclsh_argv;
int script_argc;
Tcl_Obj *script_argv;
int forceL; // wrap input in #lang L directive
} Lglobal;
/*
* Per-interp L state. When an interp is created, one of these is
* allocated for each interp and associated with the interp. Whenever
* the compiler is entered, it is extracted from the interp.
*/
typedef struct {
Lglobal *global; // L global state
Frame *frame; // current semantic stack frame
Scope *curr_scope;
Ast *ast_list; // list of all AST nodes
Type *type_list; // list of all type descriptors
void *ast; // ptr to AST root, set by parser
Tcl_Obj *errs;
int err; // =1 if there was any compile error
char *dir; // absolute path to dir containing L->file
char *file;
int line;
int prev_token_len;
int token_off; // offset of curr token from start of input
int prev_token_off; // offset of prev token from start of input
Tcl_Obj *script; // src of script being compiled
int script_len;
Tcl_Obj *options; // hash of command-line options
FnDecl *enclosing_func;
Frame *enclosing_func_frame;
Ast *mains_ast; // root of AST when main() last seen
Tcl_HashTable *include_table;
Tcl_Interp *interp;
Op_k idx_op; // kind of enclosing index (. -> [] {})
int tmpnum; // for creating tmp variables
char *toplev; // name of toplevel proc
jmp_buf jmp; // for syntax error longjmp bail out
int expr_level; // compile_expr() recursion depth
int call_level; // compile_expr() level of last fn call
Tcl_Obj *fn_calls; // list of all fn calls compiled
Tcl_Obj *fn_decls; // hash of all L fns
} Linterp;
/*
* Symbol table entry, for variables and functions (typedef and struct
* names have their own tables). The tclname can be different from
* the L name if we have to mangle the name as we do for L globals.
* The decl pointer is used to get line# info for error messages.
*/
typedef enum {
L_SYM_LVAR = 0x0001, // a local variable
L_SYM_GVAR = 0x0002, // a global variable
L_SYM_LSHADOW = 0x0004, // a global upvar shadow (these
// are also locals)
L_SYM_FN = 0x0008, // a function
L_SYM_FNBODY = 0x0010, // function body has been declared
} Sym_k;
struct Sym {
Sym_k kind;
char *name; // the L name
char *tclname; // the tcl name (can be same as L name)
Type *type;
int idx; // slot# for local var
int used_p; // TRUE iff var has been referenced
VarDecl *decl;
};
/*
* For our getopt. Note that this renders libc's getopt unusable,
* but the #define's are kept for compatibility with our getopt.
*/
#define getopt mygetopt
#define optind myoptind
#define optarg myoptarg
#define optopt myoptopt
extern int optind;
extern int optopt;
extern char *optarg;
typedef struct {
char *name; /* name w args ex: "url:" */
int ret; /* return value from getopt */
} longopt;
#define GETOPT_EOF -1
#define GETOPT_ERR 256
extern char *cksprintf(const char *fmt, ...);
extern char *ckstrdup(const char *str);
extern char *ckstrndup(const char *str, int len);
extern char *ckvsprintf(const char *fmt, va_list ap, int len);
extern int getopt(int ac, char **av, char *opts, longopt *lopts);
extern void getoptReset(void);
extern void L_bomb(const char *format, ...);
extern void L_compile_attributes(Tcl_Obj *hash, Expr *expr,
char *allowed[]);
extern char *L_dirname(char *path);
extern void L_err(const char *s, ...);
extern void L_errf(void *node, const char *format, ...);
extern int L_isUndef(Tcl_Obj *o);
extern void L_lex_begLhtml();
extern void L_lex_endLhtml();
extern void L_lex_begReArg(int kind);
extern void L_lex_start(void);
extern int L_parse(void); // yyparse
extern void L_scope_enter();
extern void L_scope_leave();
extern void L_set_baseType(Type *type, Type *base_type);
extern void L_set_declBaseType(VarDecl *decl, Type *base_type);
extern Tcl_Obj *L_split(Tcl_Interp *interp, Tcl_Obj *strobj,
Tcl_Obj *delimobj, Tcl_Obj *limobj, Expr_f flags);
extern Type *L_struct_lookup(char *tag, int local);
extern Type *L_struct_store(char *tag, VarDecl *members);
extern void L_synerr(const char *s); // yyerror
extern void L_synerr2(const char *s, int offset);
extern void L_trace(const char *format, ...);
extern char *L_type_str(Type_k kind);
extern void L_typeck_init();
extern int L_typeck_arrElt(Type *var, Type *array);
extern void L_typeck_assign(Expr *lhs, Type *rhs);
extern int L_typeck_compat(Type *lhs, Type *rhs);
extern int L_typeck_declType(VarDecl *decl);
extern void L_typeck_deny(Type_k deny, Expr *expr);
extern void L_typeck_expect(Type_k want, Expr *expr, char *msg);
extern void L_typeck_fncall(VarDecl *formals, Expr *call);
extern void L_typeck_main(VarDecl *decl);
extern int L_typeck_same(Type *a, Type *b);
extern Type *L_typedef_lookup(char *name);
extern void L_typedef_store(VarDecl *decl);
extern Tcl_Obj **L_undefObjPtrPtr();
extern void L_warnf(void *node, const char *format, ...);
extern Linterp *L;
extern char *L_attrs_attribute[];
extern char *L_attrs_cmdLine[];
extern char *L_attrs_pragma[];
extern Tcl_ObjType L_undefType;
extern Type *L_int;
extern Type *L_float;
extern Type *L_string;
extern Type *L_void;
extern Type *L_poly;
extern Type *L_widget;
static inline int
istype(Expr *expr, int type_flags)
{
return (expr->type && (expr->type->kind & type_flags));
}
static inline int
isarray(Expr *expr)
{
return (expr->type && (expr->type->kind == L_ARRAY));
}
static inline int
ishash(Expr *expr)
{
return (expr->type && (expr->type->kind == L_HASH));
}
static inline int
isstruct(Expr *expr)
{
return (expr->type && (expr->type->kind == L_STRUCT));
}
static inline int
isint(Expr *expr)
{
return (expr->type && (expr->type->kind == L_INT));
}
static inline int
isfloat(Expr *expr)
{
return (expr->type && (expr->type->kind == L_FLOAT));
}
static inline int
isstring(Expr *expr)
{
return (expr->type && (expr->type->kind == L_STRING));
}
static inline int
iswidget(Expr *expr)
{
return (expr->type && (expr->type->kind == L_WIDGET));
}
static inline int
isvoid(Expr *expr)
{
return (expr->type && (expr->type->kind == L_VOID));
}
static inline int
ispoly(Expr *expr)
{
return (expr->type && (expr->type->kind == L_POLY));
}
static inline int
isscalar(Expr *expr)
{
return (expr->type && (expr->type->kind & (L_INT |
L_FLOAT |
L_STRING |
L_WIDGET |
L_POLY)));
}
static inline int
isconst(Expr *expr)
{
return (expr->kind == L_EXPR_CONST);
}
static inline int
islist(Expr *expr)
{
return (expr->type && (expr->type->kind == L_LIST));
}
static inline int
isclass(Expr *expr)
{
return (expr->type && (expr->type->kind == L_CLASS));
}
static inline int
isregexp(Expr *expr)
{
return ((expr->kind == L_EXPR_RE) ||
((expr->kind == L_EXPR_BINOP) && (expr->op == L_OP_INTERP_RE)));
}
static inline int
ispolytype(Type *type)
{
return (type->kind == L_POLY);
}
static inline int
islisttype(Type *type)
{
return (type->kind == L_LIST);
}
static inline int
ishashtype(Type *type)
{
return (type->kind == L_HASH);
}
static inline int
isfntype(Type *type)
{
return (type->kind == L_FUNCTION);
}
static inline int
isinttype(Type *type)
{
return (type->kind == L_INT);
}
static inline int
isvoidtype(Type *type)
{
return (type->kind == L_VOID);
}
static inline int
isnameoftype(Type *type)
{
return (type->kind == L_NAMEOF);
}
static inline int
isclasstype(Type *type)
{
return (type->kind == L_CLASS);
}
static inline int
isarrayoftype(Type *type, Type_k kind)
{
return ((type->kind == L_ARRAY) && (type->base_type->kind & kind));
}
static inline int
ishashoftype(Type *type, Type_k base, Type_k elt)
{
return ((type->kind == L_HASH) &&
(type->base_type->kind & base) &&
(type->u.hash.idx_type->kind & elt));
}
static inline int
isaddrof(Expr *expr)
{
return ((expr->kind == L_EXPR_UNOP) && (expr->op == L_OP_ADDROF));
}
static inline int
isexpand(Expr *expr)
{
return ((expr->kind == L_EXPR_UNOP) && (expr->op == L_OP_EXPAND));
}
static inline int
iskv(Expr *expr)
{
return ((expr->kind == L_EXPR_BINOP) && (expr->op == L_OP_KV));
}
static inline int
isinterp(Expr *expr)
{
return ((expr->kind == L_EXPR_BINOP) && (expr->op == L_OP_INTERP_STRING));
}
static inline int
isid(Expr *expr, char *s)
{
return ((expr->kind == L_EXPR_ID) && !strcmp(expr->str, s));
}
static inline int
isarrayof(Expr *expr, Type_k kind)
{
return (isarray(expr) && (expr->type->base_type->kind & kind));
}
/*
* Return the flags that match the kind of variable we can
* dereference: globals, locals, class variables, and class instance
* variables.
*/
static inline int
canDeref(Sym *sym)
{
return (sym->decl->flags & (DECL_GLOBAL_VAR | DECL_LOCAL_VAR |
DECL_FN | DECL_CLASS_INST_VAR |
DECL_CLASS_VAR));
}
/*
* This checks whether the Expr node is a deep-dive operation that has
* left a deep-ptr on the run-time stack.
*/
static inline int
isdeepdive(Expr *expr)
{
return (expr->flags & (L_PUSH_PTR | L_PUSH_PTRVAL | L_PUSH_VALPTR));
}
static inline int
isClsConstructor(VarDecl *decl)
{
return (decl->flags & DECL_CLASS_CONST);
}
static inline int
isClsDestructor(VarDecl *decl)
{
return (decl->flags & DECL_CLASS_DESTR);
}
static inline int
isClsFnPublic(VarDecl *decl)
{
return ((decl->flags & (DECL_CLASS_FN | DECL_PUBLIC)) ==
(DECL_CLASS_FN | DECL_PUBLIC));
}
static inline int
isClsFnPrivate(VarDecl *decl)
{
return ((decl->flags & (DECL_CLASS_FN | DECL_PRIVATE)) ==
(DECL_CLASS_FN | DECL_PRIVATE));
}
static inline int
typeis(Type *type, char *name)
{
return (type->name && !strcmp(type->name, name));
}
static inline int
typeisf(Expr *expr, char *name)
{
return (expr->type->name && !strcmp(expr->type->name, name));
}
static inline void
emit_load_scalar(int idx)
{
/*
* The next line is a hack so we can generate disassemblable
* code even in the presence of obscure compilation errors
* that cause the value of a function name to be attempted to
* be loaded. Without this, tcl will die trying to output a
* disassembly since the local # (-1) would be invalid.
*/
if (idx == -1) idx = 0;
if (idx <= 255) {
TclEmitInstInt1(INST_LOAD_SCALAR1, idx, L->frame->envPtr);
} else {
TclEmitInstInt4(INST_LOAD_SCALAR4, idx, L->frame->envPtr);
}
}
static inline void
emit_store_scalar(int idx)
{
if (idx <= 255) {
TclEmitInstInt1(INST_STORE_SCALAR1, idx, L->frame->envPtr);
} else {
TclEmitInstInt4(INST_STORE_SCALAR4, idx, L->frame->envPtr);
}
}
static inline void
push_litf(const char *str, ...)
{
va_list ap;
int len = 64;
char *buf;
va_start(ap, str);
while (!(buf = ckvsprintf(str, ap, len))) {
va_end(ap);
va_start(ap, str);
len *= 2;
}
va_end(ap);
/*
* Subtle: register the literal in the body CompileEnv since
* all the code ends up there anyway. If we put it in the
* prologue CompileEnv, we'd have to fix-up all the literal
* numbers when we splice the prologue into the body.
*/
TclEmitPush(TclRegisterNewLiteral(L->frame->bodyEnvPtr, buf, strlen(buf)),
L->frame->envPtr);
ckfree(buf);
}
static inline void
push_lit(const char *str)
{
/* See comment above about registering in the body CompileEnv. */
TclEmitPush(TclRegisterNewLiteral(L->frame->bodyEnvPtr, str,
strlen(str)), L->frame->envPtr);
}
static inline void
emit_invoke(int size)
{
if (size < 256) {
TclEmitInstInt1(INST_INVOKE_STK1, size, L->frame->envPtr);
} else {
TclEmitInstInt4(INST_INVOKE_STK4, size, L->frame->envPtr);
}
}
static inline void
emit_invoke_expanded()
{
TclEmitOpcode(INST_INVOKE_EXPANDED, L->frame->envPtr);
}
static inline void
emit_pop()
{
TclEmitOpcode(INST_POP, L->frame->envPtr);
}
static inline int
currOffset(CompileEnv *envPtr)
{
/* Offset of the next instruction to be generated. */
return (envPtr->codeNext - envPtr->codeStart);
}
/*
* REVERSE() assumes that l is a singly linked list of type type with
* forward pointers named ptr. The last element in the list becomes
* the first and is stored back into l.
*/
#define REVERSE(type,ptr,l) \
do { \
type *a, *b, *c; \
for (a = NULL, b = l, c = (l ? ((type *)l)->ptr : NULL); \
b != NULL; \
b->ptr = a, a = b, b = c, c = (c ? c->ptr : NULL)) ; \
*(type **)&(l) = a; \
} while (0)
/*
* APPEND() starts at a, walks ptr until the end, and then attaches b
* to a. (Note that it's actually NCONC).
*/
#define APPEND(type,ptr,a,b) \
do { \
type *runner; \
for (runner = a; runner->ptr; runner = runner->ptr) ; \
runner->ptr = b; \
} while (0)
/*
* Like APPEND() but if a is NULL, set a to b.
*/
#define APPEND_OR_SET(type,ptr,a,b) \
do { \
if (a) { \
type *runner; \
for (runner = a; runner->ptr; runner = runner->ptr) ; \
runner->ptr = b; \
} else { \
a = b; \
} \
} while (0)
/*
* YYLOC_DEFAULT() is invoked by the scanner after matching a pattern
* and before executing its code. It tracks the source-file offset
* and line #.
*/
extern YYLTYPE L_lloc;
#define YYLLOC_DEFAULT(c,r,n) \
do { \
if (n) { \
(c).beg = YYRHSLOC(r,1).beg; \
(c).end = YYRHSLOC(r,n).end; \
} else { \
(c).beg = YYRHSLOC(r,0).beg; \
(c).end = YYRHSLOC(r,0).end; \
} \
(c).line = L->line; \
(c).file = L->file; \
} while (0)
#ifdef TCL_COMPILE_DEBUG
#define ASSERT(c) unless (c) \
L_bomb("Assertion failed: %s:%d: %s\n", __FILE__, __LINE__, #c)
#else
#define ASSERT(c) do {} while(0)
#endif
#endif /* L_COMPILE_H */
|