switch -O2 to -O1 for zetacom compilation
[xoc.git] / zeta / compile.h
blob9ad005bd7493f930ebce122a8b637d398f3d4eca
1 // Data structures used only in the zeta compiler (not at run time).
3 typedef struct Expr Expr;
4 typedef struct Fnarg Fnarg;
5 typedef struct Fn Fn;
6 typedef struct Frame Frame;
7 typedef struct Include Include;
8 typedef struct Pos Pos;
9 typedef struct Scope Scope;
10 typedef struct Stmt Stmt;
11 typedef struct Structelem Structelem;
12 typedef struct Typecheck Typecheck;
13 typedef struct Var Var;
14 typedef struct Yystype Yystype;
15 typedef struct Typex Typex;
17 LIST(Expr)
18 LIST(Fn)
19 LIST(Fnarg)
20 LIST(Include)
21 LIST(Stmt)
22 LIST(Var)
23 LIST(Typex)
24 LIST(Structelem)
26 //////////////////////////////////////////////////////////////////////////
28 // Input, lexing, parsing
30 // Position of text in input (byte offsets).
31 struct Pos {
32 int start;
33 int end;
35 int pos2fileline(Pos, char**);
37 // %P file:line
38 // %#P actual text at given location
39 // %lP line "file"
40 int posfmt(Fmt*);
42 // Error messages - error and warn are in the GNU C library, sigh.
43 #define error zeta_error
44 #define warn zeta_warn
46 void error(Pos, char*, ...);
47 void warn(Pos, char*, ...);
48 extern int nerrors; // counts calls to error
51 // Convert zeta file name into zm, zo, prefix
52 Name zeta2zm(Name);
53 Name zeta2zo(Name);
54 Name zeta2prefix(Name);
56 typedef enum TypexOp {
57 TypexType = 1,
58 TypexOne,
59 TypexListOne,
60 TypexTwo,
61 TypexVar,
62 TypexList,
63 TypexName,
64 TypexDot,
65 TypexEmpty,
66 } TypexOp;
67 struct Typex
69 Pos pos;
70 TypexOp op;
71 struct /*union, but safer*/ {
72 Type *(*one)(Type*);
73 Type *(*two)(Type*, Type*);
74 Type *(*listone)(TypeL*, Type*);
75 Type *(*list)(TypeL*);
76 } mk;
77 Name name;
78 Typex *left;
79 Typex *right;
80 TypexL *typexl;
81 Type *type;
84 Typex *mkTypex(TypexOp);
85 Typex *mkTypex1(Type*);
87 // Binary operators.
88 typedef enum BinOp {
89 BinEqEq = 1,
90 BinNotEq,
92 BinAddInt,
93 BinSubInt,
94 BinMulInt,
95 BinDivInt,
96 BinModInt,
97 BinLshInt,
98 BinRshInt,
99 BinOrInt,
100 BinAndInt,
101 BinXorInt,
102 BinLtInt,
103 BinGtInt,
104 BinLeInt,
105 BinGeInt,
107 BinAddFlt,
108 BinSubFlt,
109 BinMulFlt,
110 BinDivFlt,
111 BinModFlt,
113 BinAddStr,
114 BinLtStr,
115 BinGtStr,
116 BinLeStr,
117 BinGeStr,
118 BinIndexStr,
120 BinConsList,
121 BinIndexList,
122 BinAssocList,
123 BinAddList,
125 BinIndexArr,
127 BinIndexMap,
128 BinIndexFmap,
130 BinIndexTuple,
132 BinJoinAst,
133 BinSameAst,
135 MaxBinOp,
136 } BinOp;
138 // Unary operators
139 typedef enum UnOp {
140 UnNegInt = 1,
141 UnPosInt,
142 UnTwidInt,
143 UnLenList,
144 UnLenArr,
145 UnLenStr,
146 UnBoolAny,
147 UnStringBool,
148 UnStringInt,
149 UnStringFlt,
150 UnStringStr,
151 UnArrayList,
152 UnListArr,
153 UnListAst,
154 UnArrayInt,
155 UnSplitAst,
156 UnCopyAst,
157 UnLastkidAst,
158 UnMergedAst,
159 UnLispAst,
160 UnLongAst,
161 UnStringAst,
162 UnIntFlt,
163 UnIntString,
164 UnIntBool,
165 UnNegFlt,
166 UnFloatStr,
167 UnParentAst,
168 UnSlotAst,
169 UnSlottedAst,
170 UnKeysMap,
171 UnPtr,
172 UnRmcopyAst,
173 MaxUnOp,
174 } UnOp;
176 struct ParsePattern;
178 // Expressions
179 typedef enum ExprOp
181 ExprName,
182 ExprDeclType,
183 ExprDeclAssign,
184 ExprAssign,
185 ExprInteger,
186 ExprString,
187 ExprUnary,
188 ExprBinary,
189 ExprBinaryEq,
190 ExprCall,
191 ExprDot,
192 ExprPreInc,
193 ExprPreDec,
194 ExprPostInc,
195 ExprPostDec,
196 ExprNot,
197 ExprMkList,
198 ExprMkArray,
199 ExprTuple,
200 ExprMatch,
201 ExprAndAnd,
202 ExprOrOr,
203 ExprFloat,
204 ExprBool,
205 ExprNil,
206 ExprSlice,
207 ExprFn,
208 ExprMap,
209 ExprMkMap,
210 ExprMkFmap,
211 ExprMkStruct,
212 ExprSubtype,
213 ExprTag,
214 ExprParse,
215 ExprBacktick,
216 ExprBacktick2,
217 ExprDots,
218 } ExprOp;
219 struct Expr
221 ExprOp op;
222 BinOp binop;
223 UnOp unop;
224 Name name;
225 NameL *namel;
226 Pos pos;
227 Expr *e1;
228 Expr *e2;
229 Expr *e3;
230 ExprL *el;
231 int64 i;
232 double f;
233 Type *type;
234 Typex *typex;
235 Var *var;
236 int l1;
237 int l2;
238 Fn *fn;
239 Type *realtype;
240 Module *module;
241 Name str;
242 struct ParsePattern *pattern;
244 Expr* mkExpr(ExprOp op);
246 struct GTop;
248 // Statements
249 typedef enum StmtOp
251 StmtExpr = 1,
252 StmtPrint,
253 StmtBlock,
254 StmtWhile,
255 StmtDoWhile,
256 StmtBreak,
257 StmtContinue,
258 StmtIf,
259 StmtFn,
260 StmtReturn,
261 StmtImport,
262 StmtAssert,
263 StmtFor,
264 StmtGoto,
265 StmtLabel,
266 StmtForIn,
267 StmtTypedef,
268 StmtSwitch,
269 StmtCase,
270 StmtStruct,
271 StmtGrammar,
272 StmtAttrDecl,
273 StmtType,
274 } StmtOp;
275 struct Stmt
277 StmtOp op;
278 Pos pos;
279 Expr *e1;
280 Expr *e2;
281 Expr *e3;
282 Stmt *s1;
283 Stmt *s2;
284 StmtL *stmtl;
285 int lcontinue;
286 int lbreak;
287 int label;
288 int lfalse;
289 int broke;
290 int continued;
291 Fn *fn;
292 Name name;
293 Typex *typex;
294 StructelemL *sel;
295 int structinit;
296 Name xflag;
297 NameL *namel;
298 struct GTop *gtop;
300 Stmt* mkStmt(StmtOp op);
302 // Struct element
303 struct Structelem
305 Pos pos;
306 int tag;
307 Name name;
308 Typex *typex;
309 StructelemL *sel;
311 Structelem *mkStructelem(void);
313 struct GTop;
314 struct GStmt;
315 struct GStmtL;
316 struct GRule;
317 struct GRuleL;
318 struct GTerm;
319 struct GTermL;
320 struct ParsePattern;
321 struct PatToken;
322 struct PatTokenL;
324 // Parse stack type.
325 struct Yystype
327 int64 i;
328 double f;
329 Name name;
330 int len;
331 Expr *expr;
332 ExprL *exprl;
333 Stmt *stmt;
334 Typex *typex;
335 TypexL *typexl;
336 StmtL *stmtl;
337 FnargL *fnargl;
338 Fnarg *fnarg;
339 Structelem *structelem;
340 StructelemL *structeleml;
341 NameL *namel;
342 struct GTop *gtop;
343 struct GStmt *gstmt;
344 struct GStmtL *gstmtl;
345 struct GRule *grule;
346 struct GRuleL *grulel;
347 struct GTerm *gterm;
348 struct GTermL *gterml;
349 struct ParsePattern *parsepattern;
350 struct PatToken *pattoken;
351 struct PatTokenL *pattokenl;
352 int flags;
355 // Functions
356 struct Fn
358 Name name;
359 FnargL *args;
360 Type *returntype;
361 Typex *xreturntype;
362 StmtL *body;
363 Pos pos;
364 Frame *argframe;
365 Frame *localframe;
366 GType *type;
367 Var *var;
368 Fn *outer;
369 int nescape;
370 VarL *stolen;
371 int extensible;
372 int extend;
373 int attribute;
374 Var *vdefault;
375 Expr *extender;
377 Fn* mkFn(Name, FnargL*, Typex*, StmtL*);
379 // Function argument
380 struct Fnarg
382 Pos pos;
383 Name name;
384 Typex *typex;
385 Var *var;
387 Fnarg* mkFnarg(Name, Typex*);
390 // Input reader.
391 void pushinputfile(char*);
392 void pushinputstring(char*);
393 int yylex(Yystype*, Pos*);
394 Name fullpath(char*);
396 extern int ingrammar;
397 extern int intermname;
398 extern int inbacktick;
400 // List of input files read.
401 struct Include
403 Name file;
404 uint32 mtime;
405 uint32 size;
407 extern IncludeL* includel;
409 // Parse constants.
410 char* cescapestring(char *s, int len, int q);
411 char* cunescapestring(char *s, int len);
412 int parsechar(char *s, uint64 *value, char **end);
413 int parsefloat(char *s, long double *value);
414 int parseinteger(char *s, uint64 *value);
416 // Parser
417 int yyparse(void);
418 GType* parsetype(char*);
419 void topstmt(Stmt*);
420 extern Pos yylastpos; // Pos of most recent yacc action
423 //////////////////////////////////////////////////////////////////////
425 // Type checking; more generally, semantic analysis
427 // A variable: has a name, a type, and a cname (used in C).
428 struct Var
430 Name name;
431 Name cname;
432 GType *type;
433 Scope *scope; // scope containing variable
434 Pos pos;
435 int escape;
436 Var *redirect; // use this one instead
437 Module *module; // when type == typemodule()
438 Type *realtype; // when type == typetype()
439 int flags;
442 // Variables currently visible.
443 struct Scope
445 Scope *outer;
446 VarL *varl;
447 Frame *frame;
450 // Variables in an allocation frame (e.g.,
451 // all the top-level variables, or all the function
452 // arguments, or all the stack-allocated variables
453 // in this function.)
454 struct Frame
456 Name prefix;
457 VarL *varl;
458 Fn *fn; // function containing frame
461 // Type-checking state.
462 struct Typecheck
464 int reachable; // next statement is reachable
465 Scope *scope; // current variable scope
466 StmtL *jumpl; // all gotos and labels
467 int nlabel; // labels used so far
468 StmtL *breakl; // stack of break-able statements
469 StmtL *continuel; // stack of continue-able statements
470 Fn *fn; // current function
471 FnL *fnl; // stack of fns containing fn.
474 // Expression contexts
475 enum
477 Value = 1<<0, // Expr being used for its value
478 LValue = 1<<1, // Expr being used for its lvalue
479 BindValue = 1<<2, // Expr must be a name
482 // Start/end new variable scope.
483 Scope* pushscope(Typecheck*);
484 void popscope(Typecheck*, Scope*);
486 // Copy an existing scope.
487 Scope* copyscope(Scope*);
489 // S1 and s2 were copied from scope
490 // and then changed. Merge common changes into scope.
491 void mergescopes(Scope *s1, Scope *s2, Scope *scope);
494 // Look for, declare a variable in a scope.
495 Var* lookupvar(Scope *scope, Name name);
496 VarL* lookupvarl(Scope *scope, Name name);
497 Var* declarevar(Pos pos, Scope *scope, Name name, Type *type);
498 Var* declarevarmany(Pos pos, Scope *scope, Name name, Type *type);
499 Var* mlookupvar(Module *m, Name name);
500 Var* lookupattribute(Scope*, Name, Type*);
501 Var* _declarevar(Pos, Scope*, Name, Type*, int, Name);
503 Name attributename(Name, Type*);
504 Type* attributeargtype(Type*);
506 // Start/end new variable frame.
507 // Also starts/end new scope for the new frame.
508 Frame* pushframe(Typecheck*);
509 void popframe(Typecheck*, Frame*);
511 // Start/end new function to type check.
512 void pushfn(Typecheck*, Fn*);
513 void popfn(Typecheck*, Fn*);
515 // Type check a statement or expression.
516 void typecheckinit(void);
517 void typecheckstmt(Typecheck*, Stmt*);
518 int typecheckexpr(Typecheck*, Expr*, int);
519 void typecheckmatch(Typecheck*, Expr*, Type*);
520 void typecheckcond(Typecheck*, Expr*, Scope*, Scope*);
521 int typecheckfn(Typecheck*, Fn*);
522 void typecheckgram(Typecheck*, struct GTop*);
523 int typecheckpexpr(Typecheck*, Expr*, int);
524 int typecheckgexpr(Typecheck*, Expr*, int);
526 Type *typechecktype(Typecheck*, Typex*);
528 int typecheckpmatch(Typecheck*, Expr*, Type*);
530 Type *typedot(Pos, Type*, Name);
531 Type *typegramdot(Type*, Name);
533 void declaretype(Scope*, Expr*, Type*);
534 void redeclare(Scope*, Expr*, Type*);
536 Expr* tobool(Typecheck*, Expr*);
537 void noassign(Expr*, int);
538 void nosideeffect(Expr*, int);
540 // List of all imported modules
541 extern ModuleL* importl;
543 // Top-level scope in module.
544 extern Scope* topscope;
547 ///////////////////////////////////////////////////////////////////
549 // C output
551 typedef struct Cbuf Cbuf;
552 struct Cbuf
554 Fmt *fmt; // default output target for cgenstmt etc.
555 Fmt runfmt; // output inside run() function
556 Fmt topfmt; // output at top level
557 Fn *fn; // function being compiled
560 // %N ZN(N("string"))
561 int namefmt(Fmt*);
563 void cbprint(Cbuf*, Pos, char*, ...);
564 void cgenstmt(Cbuf*, Stmt*);
565 void cgentypes(Cbuf*);
566 void cgentypes0(Cbuf*);
567 void cgenimportdecls(Cbuf*);
568 void cgenstructdecls(Cbuf*);
569 void cgengraminit(Cbuf*);
570 Name cgenexpr(Cbuf*, Expr*);
571 Name cbtmp(Cbuf*);
572 Name cgenpexpr(Cbuf*, Expr*);
573 void cgenassign(Cbuf *cb, Expr *dst, Name src);
574 void cgenpmatch(Cbuf *cb, Expr *expr, Name val, int lfail);
575 Name csanitize(Name);
577 void writemodule(Name);
578 extern int linenumbers;
580 Name findinclude(Pos, Name);