limbo.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. #include "lib9.h"
  2. #include "bio.h"
  3. #include "isa.h"
  4. #include "mathi.h"
  5. /* internal dis ops */
  6. #define IEXC MAXDIS
  7. #define IEXC0 (MAXDIS+1)
  8. #define INOOP (MAXDIS+2)
  9. /* temporary */
  10. #define LDT 1
  11. #ifndef Extern
  12. #define Extern extern
  13. #endif
  14. #define YYMAXDEPTH 200
  15. typedef struct Addr Addr;
  16. typedef struct Case Case;
  17. typedef struct Decl Decl;
  18. typedef struct Desc Desc;
  19. typedef struct Dlist Dlist;
  20. typedef struct Except Except;
  21. typedef struct File File;
  22. typedef struct Fline Fline;
  23. typedef struct Inst Inst;
  24. typedef struct Label Label;
  25. typedef struct Line Line;
  26. typedef struct Node Node;
  27. typedef struct Ok Ok;
  28. typedef struct Src Src;
  29. typedef struct Sym Sym;
  30. typedef struct Szal Szal;
  31. typedef struct Tattr Tattr;
  32. typedef struct Teq Teq;
  33. typedef struct Tpair Tpair;
  34. typedef struct Type Type;
  35. typedef struct Typelist Typelist;
  36. typedef double Real;
  37. typedef vlong Long;
  38. enum
  39. {
  40. STemp = NREG * IBY2WD,
  41. RTemp = STemp+IBY2WD,
  42. DTemp = RTemp+IBY2WD,
  43. MaxTemp = DTemp+IBY2WD,
  44. MaxReg = 1<<16,
  45. MaxAlign = IBY2LG,
  46. StrSize = 256,
  47. NumSize = 32, /* max length of printed */
  48. MaxIncPath = 32, /* max directories in include path */
  49. MaxScope = 64, /* max nested {} */
  50. MaxInclude = 32, /* max nested include "" */
  51. ScopeBuiltin = 0,
  52. ScopeNils = 1,
  53. ScopeGlobal = 2
  54. };
  55. /*
  56. * return tuple from expression type checking
  57. */
  58. struct Ok
  59. {
  60. int ok;
  61. int allok;
  62. };
  63. /*
  64. * return tuple from type sizing
  65. */
  66. struct Szal
  67. {
  68. int size;
  69. int align;
  70. };
  71. /*
  72. * return tuple for file/line numbering
  73. */
  74. struct Fline
  75. {
  76. File *file;
  77. int line;
  78. };
  79. struct File
  80. {
  81. char *name;
  82. int abs; /* absolute line of start of the part of file */
  83. int off; /* offset to line in the file */
  84. int in; /* absolute line where included */
  85. char *act; /* name of real file with #line fake file */
  86. int actoff; /* offset from fake line to real line */
  87. int sbl; /* symbol file number */
  88. };
  89. struct Line
  90. {
  91. int line;
  92. int pos; /* character within the line */
  93. };
  94. struct Src
  95. {
  96. Line start;
  97. Line stop;
  98. };
  99. enum
  100. {
  101. Aimm, /* immediate */
  102. Amp, /* global */
  103. Ampind, /* global indirect */
  104. Afp, /* activation frame */
  105. Afpind, /* frame indirect */
  106. Apc, /* branch */
  107. Adesc, /* type descriptor immediate */
  108. Aoff, /* offset in module description table */
  109. Anoff, /* above encoded as -ve */
  110. Aerr, /* error */
  111. Anone, /* no operand */
  112. Aldt, /* linkage descriptor table immediate */
  113. Aend
  114. };
  115. struct Addr
  116. {
  117. long reg;
  118. long offset;
  119. Decl *decl;
  120. };
  121. struct Inst
  122. {
  123. Src src;
  124. ushort op;
  125. long pc;
  126. uchar reach; /* could a control path reach this instruction? */
  127. uchar sm; /* operand addressing modes */
  128. uchar mm;
  129. uchar dm;
  130. Addr s; /* operands */
  131. Addr m;
  132. Addr d;
  133. Inst *branch; /* branch destination */
  134. Inst *next;
  135. int block; /* blocks nested inside */
  136. };
  137. struct Case
  138. {
  139. int nlab;
  140. int nsnd;
  141. long offset; /* offset in mp */
  142. Label *labs;
  143. Node *wild; /* if nothing matches */
  144. Inst *iwild;
  145. };
  146. struct Label
  147. {
  148. Node *node;
  149. char isptr; /* true if the labelled alt channel is a pointer */
  150. Node *start; /* value in range [start, stop) => code */
  151. Node *stop;
  152. Inst *inst;
  153. };
  154. enum
  155. {
  156. Dtype,
  157. Dfn,
  158. Dglobal,
  159. Darg,
  160. Dlocal,
  161. Dconst,
  162. Dfield,
  163. Dtag, /* pick tags */
  164. Dimport, /* imported identifier */
  165. Dunbound, /* unbound identified */
  166. Dundef,
  167. Dwundef, /* undefined, but don't whine */
  168. Dend
  169. };
  170. struct Decl
  171. {
  172. Src src; /* where declaration */
  173. Sym *sym;
  174. uchar store; /* storage class */
  175. uchar nid; /* block grouping for locals */
  176. schar caninline; /* inline function */
  177. uchar das; /* declared with := */
  178. Decl *dot; /* parent adt or module */
  179. Type *ty;
  180. int refs; /* number of references */
  181. long offset;
  182. int tag; /* union tag */
  183. uchar scope; /* in which it was declared */
  184. uchar handler; /* fn has exception handler in body */
  185. Decl *next; /* list in same scope, field or argument list, etc. */
  186. Decl *old; /* declaration of the symbol in enclosing scope */
  187. Node *eimport; /* expr from which imported */
  188. Decl *importid; /* identifier imported */
  189. Decl *timport; /* stack of identifiers importing a type */
  190. Node *init; /* data initialization */
  191. int tref; /* 1 => is a tmp; >=2 => tmp in use */
  192. char cycle; /* can create a cycle */
  193. char cyc; /* so labelled in source */
  194. char cycerr; /* delivered an error message for cycle? */
  195. char implicit; /* implicit first argument in an adt? */
  196. Decl *iface; /* used external declarations in a module */
  197. Decl *locals; /* locals for a function */
  198. Decl *link; /* pointer to parent function or function argument or local share or parent type dec */
  199. Inst *pc; /* start of function */
  200. /* Inst *endpc; */ /* limit of function - unused */
  201. Desc *desc; /* heap descriptor */
  202. };
  203. struct Desc
  204. {
  205. int id; /* dis type identifier */
  206. uchar used; /* actually used in output? */
  207. uchar *map; /* byte map of pointers */
  208. long size; /* length of the object */
  209. long nmap; /* length of good bytes in map */
  210. Desc *next;
  211. };
  212. struct Dlist
  213. {
  214. Decl *d;
  215. Dlist *next;
  216. };
  217. struct Except
  218. {
  219. Inst *p1; /* first pc covered */
  220. Inst *p2; /* last pc not covered */
  221. Case *c; /* exception case instructions */
  222. Decl *d; /* exception definition if any */
  223. Node *zn; /* list of nodes to zero in handler */
  224. Desc *desc; /* descriptor map for above */
  225. int ne; /* number of exceptions (ie not strings) in case */
  226. Except *next;
  227. };
  228. struct Sym
  229. {
  230. ushort token;
  231. char *name;
  232. int len;
  233. int hash;
  234. Sym *next;
  235. Decl *decl;
  236. Decl *unbound; /* place holder for unbound symbols */
  237. };
  238. /*
  239. * ops for nodes
  240. */
  241. enum
  242. {
  243. Oadd = 1,
  244. Oaddas,
  245. Oadr,
  246. Oadtdecl,
  247. Oalt,
  248. Oand,
  249. Oandand,
  250. Oandas,
  251. Oarray,
  252. Oas,
  253. Obreak,
  254. Ocall,
  255. Ocase,
  256. Ocast,
  257. Ochan,
  258. Ocomma,
  259. Ocomp,
  260. Ocondecl,
  261. Ocons,
  262. Oconst,
  263. Ocont,
  264. Odas,
  265. Odec,
  266. Odiv,
  267. Odivas,
  268. Odo,
  269. Odot,
  270. Oelem,
  271. Oeq,
  272. Oexcept,
  273. Oexdecl,
  274. Oexit,
  275. Oexp,
  276. Oexpas,
  277. Oexstmt,
  278. Ofielddecl,
  279. Ofnptr,
  280. Ofor,
  281. Ofunc,
  282. Ogeq,
  283. Ogt,
  284. Ohd,
  285. Oif,
  286. Oimport,
  287. Oinc,
  288. Oind,
  289. Oindex,
  290. Oinds,
  291. Oindx,
  292. Oinv,
  293. Ojmp,
  294. Olabel,
  295. Olen,
  296. Oleq,
  297. Oload,
  298. Olsh,
  299. Olshas,
  300. Olt,
  301. Omdot,
  302. Omod,
  303. Omodas,
  304. Omoddecl,
  305. Omul,
  306. Omulas,
  307. Oname,
  308. Oneg,
  309. Oneq,
  310. Onot,
  311. Onothing,
  312. Oor,
  313. Ooras,
  314. Ooror,
  315. Opick,
  316. Opickdecl,
  317. Opredec,
  318. Opreinc,
  319. Oraise,
  320. Orange,
  321. Orcv,
  322. Oref,
  323. Oret,
  324. Orsh,
  325. Orshas,
  326. Oscope,
  327. Oself,
  328. Oseq,
  329. Oslice,
  330. Osnd,
  331. Ospawn,
  332. Osub,
  333. Osubas,
  334. Otagof,
  335. Otl,
  336. Otuple,
  337. Otype,
  338. Otypedecl,
  339. Oused,
  340. Ovardecl,
  341. Ovardecli,
  342. Owild,
  343. Oxor,
  344. Oxoras,
  345. Oend
  346. };
  347. /*
  348. * moves
  349. */
  350. enum
  351. {
  352. Mas,
  353. Mcons,
  354. Mhd,
  355. Mtl,
  356. Mend
  357. };
  358. /*
  359. * addressability
  360. */
  361. enum
  362. {
  363. Rreg, /* v(fp) */
  364. Rmreg, /* v(mp) */
  365. Roff, /* $v */
  366. Rnoff, /* $v encoded as -ve */
  367. Rdesc, /* $v */
  368. Rdescp, /* $v */
  369. Rconst, /* $v */
  370. Ralways, /* preceeding are always addressable */
  371. Radr, /* v(v(fp)) */
  372. Rmadr, /* v(v(mp)) */
  373. Rcant, /* following are not quite addressable */
  374. Rpc, /* branch address */
  375. Rmpc, /* cross module branch address */
  376. Rareg, /* $v(fp) */
  377. Ramreg, /* $v(mp) */
  378. Raadr, /* $v(v(fp)) */
  379. Ramadr, /* $v(v(mp)) */
  380. Rldt, /* $v */
  381. Rend
  382. };
  383. #define PARENS 1
  384. #define TEMP 2
  385. #define FNPTRA 4 /* argument */
  386. #define FNPTR2 8 /* 2nd parameter */
  387. #define FNPTRN 16 /* use -ve offset */
  388. #define FNPTR (FNPTRA|FNPTR2|FNPTRN)
  389. struct Node
  390. {
  391. Src src;
  392. uchar op;
  393. uchar addable;
  394. uchar flags;
  395. uchar temps;
  396. Node *left;
  397. Node *right;
  398. Type *ty;
  399. Decl *decl;
  400. Long val; /* for Oconst */
  401. Real rval; /* for Oconst */
  402. };
  403. enum
  404. {
  405. /*
  406. * types visible to limbo
  407. */
  408. Tnone = 0,
  409. Tadt,
  410. Tadtpick, /* pick case of an adt */
  411. Tarray,
  412. Tbig, /* 64 bit int */
  413. Tbyte, /* 8 bit unsigned int */
  414. Tchan,
  415. Treal,
  416. Tfn,
  417. Tint, /* 32 bit int */
  418. Tlist,
  419. Tmodule,
  420. Tref,
  421. Tstring,
  422. Ttuple,
  423. Texception,
  424. Tfix,
  425. Tpoly,
  426. /*
  427. * internal use types
  428. */
  429. Tainit, /* array initializers */
  430. Talt, /* alt channels */
  431. Tany, /* type of nil */
  432. Tarrow, /* unresolved ty->id types */
  433. Tcase, /* case labels */
  434. Tcasel, /* case big labels */
  435. Tcasec, /* case string labels */
  436. Tdot, /* unresolved ty.id types */
  437. Terror,
  438. Tgoto, /* goto labels */
  439. Tid, /* id with unknown type */
  440. Tiface, /* module interface */
  441. Texcept, /* exception handler tables */
  442. Tinst, /* instantiated adt */
  443. Tend
  444. };
  445. enum
  446. {
  447. OKbind = 1 << 0, /* type decls are bound */
  448. OKverify = 1 << 1, /* type looks ok */
  449. OKsized = 1 << 2, /* started figuring size */
  450. OKref = 1 << 3, /* recorded use of type */
  451. OKclass = 1 << 4, /* equivalence class found */
  452. OKcyc = 1 << 5, /* checked for cycles */
  453. OKcycsize = 1 << 6, /* checked for cycles and size */
  454. OKmodref = 1 << 7, /* started checking for a module handle */
  455. OKmask = 0xff,
  456. /*
  457. * recursive marks
  458. */
  459. TReq = 1 << 0,
  460. TRcom = 1 << 1,
  461. TRcyc = 1 << 2,
  462. TRvis = 1 << 3,
  463. };
  464. /* type flags */
  465. #define FULLARGS 1 /* all hidden args added */
  466. #define INST 2 /* instantiated adt */
  467. #define CYCLIC 4 /* cyclic type */
  468. #define POLY 8 /* polymorphic types inside */
  469. #define NOPOLY 16 /* no polymorphic types inside */
  470. struct Type
  471. {
  472. Src src;
  473. uchar kind;
  474. uchar varargs; /* if a function, ends with vargs? */
  475. uchar ok; /* set when type is verified */
  476. uchar linkall; /* put all iface fns in external linkage? */
  477. uchar rec; /* in the middle of recursive type */
  478. uchar cons; /* exception constant */
  479. uchar align; /* alignment in bytes */
  480. uchar flags;
  481. int sbl; /* slot in .sbl adt table */
  482. long sig; /* signature for dynamic type check */
  483. long size; /* storage required, in bytes */
  484. Decl *decl;
  485. Type *tof;
  486. Decl *ids;
  487. Decl *tags; /* tagged fields in an adt */
  488. Decl *polys; /* polymorphic fields in fn or adt */
  489. Case *cse; /* case or goto labels */
  490. Type *teq; /* temporary equiv class for equiv checking */
  491. Type *tcom; /* temporary equiv class for compat checking */
  492. Teq *eq; /* real equiv class */
  493. Node *val; /* for Tfix, Tfn, Tadt only */
  494. union {
  495. Node *eraises; /* for Tfn only */
  496. Typelist *tlist; /* for Tinst only */
  497. Tpair *tmap; /* for Tadt only */
  498. } u;
  499. };
  500. /*
  501. * type equivalence classes
  502. */
  503. struct Teq
  504. {
  505. int id; /* for signing */
  506. Type *ty; /* an instance of the class */
  507. Teq *eq; /* used to link eq sets */
  508. };
  509. struct Tattr
  510. {
  511. char isptr;
  512. char refable;
  513. char conable;
  514. char big;
  515. char vis; /* type visible to users */
  516. };
  517. enum {
  518. Sother,
  519. Sloop,
  520. Sscope
  521. };
  522. struct Tpair
  523. {
  524. Type *t1;
  525. Type *t2;
  526. Tpair *nxt;
  527. };
  528. struct Typelist
  529. {
  530. Type *t;
  531. Typelist *nxt;
  532. };
  533. Extern Decl **adts;
  534. Extern Sym *anontupsym; /* name assigned to all anonymouse tuples */
  535. Extern int arrayz;
  536. Extern int asmsym; /* generate symbols in assembly language? */
  537. Extern Biobuf *bins[MaxInclude];
  538. Extern int blocks;
  539. Extern Biobuf *bout; /* output file */
  540. Extern Biobuf *bsym; /* symbol output file; nil => no sym out */
  541. Extern double canonnan; /* standard nan */
  542. Extern uchar casttab[Tend][Tend]; /* instruction to cast from [1] to [2] */
  543. Extern long constval;
  544. Extern Decl *curfn;
  545. Extern char debug[256];
  546. Extern Desc *descriptors; /* list of all possible descriptors */
  547. Extern int dontcompile; /* dis header flag */
  548. Extern int dowarn;
  549. Extern char *emitcode; /* emit stub routines for system module functions */
  550. Extern int emitdyn; /* emit stub routines as above but for dynamic modules */
  551. Extern int emitstub; /* emit type and call frames for system modules */
  552. Extern char *emittab; /* emit table of runtime functions for this module */
  553. Extern int errors;
  554. Extern char escmap[256];
  555. Extern Inst *firstinst;
  556. Extern long fixss; /* set extent from command line */
  557. Extern Decl *fndecls;
  558. Extern Decl **fns;
  559. Extern int gendis; /* generate dis or asm? */
  560. Extern Decl *impdecl; /* id of implementation module or union if many */
  561. Extern Dlist *impdecls; /* id(s) of implementation module(s) */
  562. /* Extern Sym *impmod; */ /* name of implementation module */
  563. Extern Decl *impmods; /* name of implementation module(s) */
  564. Extern Decl *iota;
  565. Extern uchar isbyteinst[256];
  566. Extern int isfatal;
  567. Extern int isrelop[Oend];
  568. Extern uchar isused[Oend];
  569. Extern Inst *lastinst;
  570. Extern int lenadts;
  571. Extern int maxerr;
  572. Extern int maxlabdep; /* maximum nesting of breakable/continuable statements */
  573. Extern long maxstack; /* max size of a stack frame called */
  574. Extern int mustcompile; /* dis header flag */
  575. Extern int oldcycles;
  576. Extern int nadts;
  577. Extern int newfnptr; /* ISELF and -ve indices */
  578. Extern int nfns;
  579. Extern Decl *nildecl; /* declaration for limbo's nil */
  580. Extern int nlabel;
  581. Extern int dontinline;
  582. Extern Line noline;
  583. Extern Src nosrc;
  584. Extern uchar opcommute[Oend];
  585. Extern int opind[Tend];
  586. Extern uchar oprelinvert[Oend];
  587. Extern int optims;
  588. Extern char *outfile;
  589. Extern Type *precasttab[Tend][Tend];
  590. Extern int scope;
  591. Extern Decl *selfdecl; /* declaration for limbo's self */
  592. Extern uchar sideeffect[Oend];
  593. Extern char *signdump; /* dump sig for this fn */
  594. Extern int superwarn;
  595. Extern char *symfile;
  596. Extern Type *tany;
  597. Extern Type *tbig;
  598. Extern Type *tbyte;
  599. Extern Type *terror;
  600. Extern Type *tint;
  601. Extern Type *tnone;
  602. Extern Type *treal;
  603. Extern Node *tree;
  604. Extern Type *tstring;
  605. Extern Type *texception;
  606. Extern Type *tunknown;
  607. Extern Type *tfnptr;
  608. Extern Type *rtexception;
  609. Extern char unescmap[256];
  610. Extern Src unifysrc;
  611. Extern Node znode;
  612. extern int *blockstack;
  613. extern int blockdep;
  614. extern int nblocks;
  615. extern File **files;
  616. extern int nfiles;
  617. extern uchar chantab[Tend];
  618. extern uchar disoptab[Oend+1][7];
  619. extern char *instname[];
  620. extern char *kindname[Tend];
  621. extern uchar movetab[Mend][Tend];
  622. extern char *opname[];
  623. extern int setisbyteinst[];
  624. extern int setisused[];
  625. extern int setsideeffect[];
  626. extern char *storename[Dend];
  627. extern int storespace[Dend];
  628. extern Tattr tattr[Tend];
  629. #include "fns.h"
  630. #pragma varargck type "D" Decl*
  631. #pragma varargck type "I" Inst*
  632. #pragma varargck type "K" Decl*
  633. #pragma varargck type "k" Decl*
  634. #pragma varargck type "L" Line
  635. #pragma varargck type "M" Desc*
  636. #pragma varargck type "n" Node*
  637. #pragma varargck type "O" int
  638. #pragma varargck type "O" uint
  639. #pragma varargck type "g" double
  640. #pragma varargck type "Q" Node*
  641. #pragma varargck type "R" Type*
  642. #pragma varargck type "T" Type*
  643. #pragma varargck type "t" Type*
  644. #pragma varargck type "U" Src
  645. #pragma varargck type "v" Node*
  646. #pragma varargck type "V" Node*