c.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*
  2. * GTools C compiler
  3. * =================
  4. * source file :
  5. * C
  6. *
  7. * Copyright 2001-2004 Paul Froissart.
  8. * Credits to Christoph van Wuellen and Matthew Brandt.
  9. * All commercial rights reserved.
  10. *
  11. * This compiler may be redistributed as long there is no
  12. * commercial interest. The compiler must not be redistributed
  13. * without its full sources. This notice must stay intact.
  14. */
  15. #ifndef C_H
  16. #define C_H
  17. #ifdef PC
  18. #ifdef SHORT_INT
  19. #undef int
  20. #endif
  21. #include <stddef.h>
  22. //#include <times.h>
  23. #include <stdio.h>
  24. #ifdef SHORT_INT
  25. #define int short
  26. #endif
  27. #else
  28. #include "define.h"
  29. #endif
  30. //#warning hey hey!
  31. #include "error.h"
  32. #ifdef CPU_DEFINED
  33. #undef CPU_DEFINED
  34. #endif
  35. #ifdef MC68000
  36. #define MC680X0
  37. #ifdef CPU_DEFINED
  38. error, define only one target CPU!
  39. #endif
  40. #define CPU_DEFINED
  41. #define LIST_NAME "gtclist.txt"
  42. #define ICODE_NAME "gtcicode.txt"
  43. #endif
  44. #ifdef MC68010
  45. #define MC680X0
  46. #ifdef CPU_DEFINED
  47. error, define only one target CPU!
  48. #endif
  49. #define CPU_DEFINED
  50. #define LIST_NAME "gtclist.txt"
  51. #define ICODE_NAME "gtcicode.txt"
  52. #endif
  53. #ifdef MC68020
  54. #define MC680X0
  55. #ifdef CPU_DEFINED
  56. error, define only one target CPU!
  57. #endif
  58. #define CPU_DEFINED
  59. #define LIST_NAME "gtclist.txt"
  60. #define ICODE_NAME "gtcicode.txt"
  61. #endif
  62. #ifdef MC68030
  63. #define MC680X0
  64. #ifdef CPU_DEFINED
  65. error, define only one target CPU!
  66. #endif
  67. #define CPU_DEFINED
  68. #define LIST_NAME "gtclist.txt"
  69. #define ICODE_NAME "gtcicode.txt"
  70. #endif
  71. #ifdef MC68040
  72. #define MC680X0
  73. #ifdef CPU_DEFINED
  74. error, define only one target CPU!
  75. #endif
  76. #define CPU_DEFINED
  77. #define LIST_NAME "gtclist.txt"
  78. #define ICODE_NAME "gtcicode.txt"
  79. #endif
  80. #ifdef INTEL_486
  81. #ifndef INTEL_386
  82. #define INTEL_386
  83. #endif
  84. #endif
  85. #ifdef INTEL_386
  86. #ifdef CPU_DEFINED
  87. #error define only one target CPU!
  88. #endif
  89. #define CPU_DEFINED
  90. #define LIST_NAME "c386.list"
  91. #define ICODE_NAME "c386.icode"
  92. /*
  93. * if FUNCS_USE_387 is defined, extra library calls are generated if the
  94. * nofpu option is in effect that allows to use code generated by
  95. * this compiler to be linked with functions that return values
  96. * on the 387 stack
  97. */
  98. #define FUNCS_USE_387
  99. #endif
  100. #ifndef CPU_DEFINED
  101. #error target CPU type must be defined
  102. #endif
  103. /* the tokens returned from lexical analysis */
  104. /* assumptions about the order :
  105. * none currently (beware of is_lang_ext though) */
  106. enum e_sym {
  107. // 0x00
  108. cconst, iconst, lconst, uconst, ulconst, sconst, rconst, plus, minus,
  109. // 0x09
  110. divide, lshift, rshift, modop, eq, neq, lt, leq, gt,
  111. // 0x12
  112. geq, assign, asplus, asminus, astimes, asdivide, asmodop,
  113. asuparrow,
  114. // 0x1A
  115. aslshift, asrshift, asand, asor, autoinc, autodec, hook, compl,
  116. // 0x22
  117. comma, colon, semicolon, uparrow, openbr, closebr, begin, end,
  118. // 0x2A
  119. closepa, pointsto, dot, lor, land, not, or, and,
  120. // 0x32
  121. star, openpa, id, kw_int, kw_char, kw_short, kw_long,
  122. kw_void, kw_float, kw_double, kw_struct, kw_union,
  123. kw_enum, kw_unsigned, kw_signed, kw_auto, kw_extern,
  124. kw_const, kw_volatile,
  125. // 0x45
  126. kw_register, kw_typedef, kw_static, kw_goto, kw_return,
  127. kw_sizeof, kw_break, kw_continue, kw_if, kw_else, kw_for,
  128. kw_do, kw_while, kw_switch, kw_case, kw_default,
  129. // 0x55
  130. eof,
  131. // ANSI extensions
  132. kw_typeof, dots, kw_alloca,
  133. // GTC extensions
  134. kw_loop, kw_until, kw_count, kw_eval, // (lang extensions)
  135. kw_defined, kw_incbin, kw_softcast, kw_c,
  136. // GNU C extensions
  137. kw_asm, kwb_constant_p, kw_attr,
  138. // ASM keywords
  139. kw_dreg, kw_areg, sharp,
  140. #ifdef OLD_AMODE_INPUT
  141. kw_offs_end,
  142. #endif
  143. kw_instr,
  144. };
  145. #define is_lang_ext(__st) (__st>=kw_loop && __st<=kw_eval)
  146. /* storage classes */
  147. enum e_sc {
  148. sc_static, sc_auto, sc_global, sc_external, sc_type, sc_const,
  149. sc_member, sc_label, sc_ulabel, sc_typedef, sc_parms, sc_parms2,
  150. sc_define, sc_vamac
  151. };
  152. /* basic data types */
  153. #ifndef BCDFLT
  154. #define bt_size(__x) ((__x)<bt_long?((__x)<bt_short?1:2):4)
  155. #define float_size 4
  156. #else
  157. #ifdef DOUBLE
  158. #define bt_size(__x) ((__x)<bt_long?((__x)<bt_short?1:2):((__x)==bt_double?10:4))
  159. #define float_size !!! CAUTION, VARIES BETWEEN DOUBLE AND FLOAT !!!
  160. #else
  161. #define bt_size(__x) ((__x)<bt_long?((__x)<bt_short?1:2):((__x)==bt_float?10:4))
  162. #define float_size 10
  163. #endif
  164. #endif
  165. #define bt_uns(__x) ((__x)&1)
  166. #define bt_integral(__x) ((__x)<=bt_ulong)
  167. #ifndef DOUBLE
  168. #define bt_bcd bt_float
  169. #define bt_double bt_float
  170. #define tp_double tp_float
  171. #define bt_scalar(__x) ((__x)<=bt_double)
  172. #else
  173. #define bt_bcd bt_double
  174. #endif
  175. #ifdef NOBCDFLT
  176. #undef bt_bcd
  177. #endif
  178. #define bt_comparable(__x) ((__x)<=bt_pointer)
  179. #ifndef NOBCDFLT
  180. #define bt_aggregate(__x) ((__x)==bt_bcd || (__x)>=bt_struct)
  181. #else
  182. #define bt_aggregate(__x) ((__x)>=bt_struct)
  183. #endif
  184. #define bt_uncastable(__x) ((__x)>=bt_struct)
  185. #define iscomparable(tp) bt_comparable((tp)->type)
  186. #define isscalar(tp) bt_scalar((tp)->type)
  187. #define integral(tp) bt_integral((tp)->type)
  188. #define isaggregate(tp) bt_aggregate((tp)->type)
  189. enum e_bt {
  190. bt_char, bt_uchar, bt_short, bt_ushort,
  191. bt_long, bt_ulong, bt_float,
  192. #ifdef DOUBLE
  193. bt_double,
  194. #endif
  195. bt_pointer,
  196. bt_func,
  197. bt_void, bt_bitfield,
  198. bt_struct, bt_union,
  199. };
  200. /* these form the string literal pool */
  201. struct slit {
  202. struct slit *next;
  203. char *str;
  204. short label;
  205. short len;
  206. };
  207. /* a symbol table */
  208. struct stab {
  209. int hash;
  210. struct sym *head;
  211. struct sym *tail;
  212. };
  213. /* important points for reconsidering N_HASH value :
  214. - a HTABLE is about 8*N_HASH -byte long
  215. - each imbricated 'compound()' call takes the space of 2 HTABLEs on the stack
  216. (512 bytes with N_HASH=32, thus allowing only 20 imbricated compound stmts)
  217. - there are 6 static HTABLEs, 7 if AS is defined (but could be improved with malloc() / BSS)
  218. - N_HASH = 1 << HASH_LOG */
  219. #define N_HASH 32
  220. #define N_HASH_AND "31"
  221. #define HASH_LOG 5
  222. typedef struct hstab {
  223. int hash;
  224. struct htab {
  225. struct sym *head;
  226. struct sym *tail;
  227. } h[N_HASH];
  228. } HTABLE;
  229. /* structure defining a data type */
  230. struct typ {
  231. struct stab lst;
  232. struct typ *btp;
  233. #ifdef LISTING
  234. char *sname;
  235. #endif
  236. long size;
  237. enum(e_bt) type;
  238. /*
  239. * The following six chars may be unsigned -- no harm.
  240. * They could be ints without restriction -- this is to save memory
  241. */
  242. /*
  243. * val_flag is normally 0, except for:
  244. * - type==bt_pointer: set to 1 if it's an array, to 0 if it's a pointer
  245. * - type==bt_func: always set to 1
  246. * (passing a function is like passing an array, we should actually be passing a pointer;
  247. * and like an array, we do not want cond_deref() to build an en_ref node)
  248. * - type==bt_integral: set to 1 to indicate an enum (this feature is currently unused)
  249. */
  250. char val_flag;
  251. char st_flag; // is the type already stored globally?
  252. char const_flag; // is it a 'const' type?
  253. char vol_flag; // is it a 'volatile' type?
  254. char bit_width;
  255. char bit_offset;
  256. };
  257. #ifdef REGPARM
  258. #define rp_dn bit_width
  259. #define rp_an bit_offset
  260. #endif
  261. /* a symbol table entry */
  262. struct sym {
  263. char *name;
  264. struct sym *prev;
  265. struct sym *next;
  266. struct typ *tp;
  267. union {
  268. long i;
  269. unsigned long u;
  270. int splab;
  271. char *s;
  272. } value;
  273. enum(e_sc) storage_class;
  274. int used; // note:
  275. // - for 'normal' symbols (C variables), this belongs to {-1,0,1}
  276. // - for preprocessor symbols, the lower 8 bits is the number of arguments to the macro
  277. // and the higher 8 bits are described in enum(e_ppsymflags) below
  278. };
  279. enum e_ppsymflags {
  280. PPSYM_UNDER_EXPANSION = 0x8000, // set if we are currently expanding this symbol (avoid recursive expansion)
  281. #ifdef ASM
  282. PPSYM_ASM_KEYWORD = 0x4000, // set if this an ASM reserved keyword ('move', 'b', 'w', 'l', 'd7', 'sp')
  283. PPSYM_DEFINED_IN_ASM = 0x2000, // set if this symbol was defined inside an asm{} statement
  284. #endif
  285. };
  286. #define SYM struct sym
  287. #define TYP struct typ
  288. #define TABLE struct stab
  289. #ifdef PC
  290. #define MAX_ERROR_COUNT 1
  291. #else
  292. #define MAX_ERROR_COUNT 1
  293. #endif
  294. #define MAX_STRLEN 1500
  295. #define MAX_ID_LEN 50
  296. #ifdef OLD_MACRO
  297. #define MAX_MAC_LEN 800
  298. #define LINE_LENGTH (1800+MAX_MAC_LEN+1)
  299. #else
  300. #define LINE_LENGTH (1800+1)
  301. #endif
  302. #define MAX_PARAMS 20
  303. #define REG_LIST 20
  304. #define AUTO_LIST 100
  305. #define ERR_SYNTAX 0 /* general error */
  306. #define ERR_ILLCHAR 1 /* illegal character */
  307. #define ERR_FPCON 2 /* illegal floating-point constant */
  308. #define ERR_ILLTYPE 3 /* illegal type */
  309. #define ERR_UNDEFINED 4 /* undefined identifier */
  310. #define ERR_FIELD 5 /* no field allowed here */
  311. #define ERR_PUNCT 6 /* expected symbol not found */
  312. #define ERR_IDEXPECT 7 /* identifier expected */
  313. #define ERR_NOINIT 8 /* initialization invalid */
  314. #define ERR_INCOMPLETE 9 /* incomplete struct/union/enum declaration */
  315. #define ERR_ILLINIT 10 /* illegal initialization */
  316. #define ERR_INITSIZE 11 /* too many initializers */
  317. #define ERR_ILLCLASS 12 /* illegal storage class */
  318. #define ERR_BLOCK 13 /* function body expected */
  319. #define ERR_NOPOINTER 14 /* pointer type expected */
  320. #define ERR_NOFUNC 15 /* function type expected */
  321. #define ERR_NOMEMBER 16 /* struct/union member expected */
  322. #define ERR_LVALUE 17 /* l-value required */
  323. #define ERR_DEREF 18 /* error dereferencing a pointer */
  324. #define ERR_MISMATCH 19 /* type mismatch error */
  325. #define ERR_EXPREXPECT 20 /* expression expected */
  326. #define ERR_WHILEXPECT 21 /* 'while' expected in do-loop */
  327. #define ERR_ENUMVAL 22 /* enum value out of range */
  328. #define ERR_DUPCASE 23 /* duplicate case label */
  329. #define ERR_LABEL 24 /* undefined label */
  330. //#define ERR_PREPROC 25 /* preprocessing error */
  331. #define ERR_ARG 26 /* declared Argument missing */
  332. #define ERR_WIDTH 27 /* illegal field width */
  333. #define ERR_INTEXPR 28 /* illegal constant integer expression */
  334. #define ERR_CAST 29 /* error doing a cast */
  335. #define ERR_INTEGER 30 /* integer-valued type expected */
  336. #define ERR_CASTCON 31 /* error casting a constant */
  337. #define ERR_REDECL 32 /* illegal redeclaration */
  338. //#define ERR_PARMS 33 /* error while scanning a parameter list */
  339. #define ERR_FTYPE 34 /* bad host type for bit fields */
  340. #define ERR_INCLFILE 35 /* #include: no include file specified */
  341. #define ERR_CANTOPEN 36 /* can't open include file */
  342. #define ERR_DEFINE 37 /* wrong #define */
  343. #define ERR_CUSTOM 38 /* #error */
  344. #define ERR_DUPSYM 39 /* duplicate symbol */
  345. #define ERR_CONSTEXPECT 40 /* constant expression expected */
  346. #define ERR_OUTRANGE 41 /* value out of range */
  347. #define ERR_TOOMPARAMS 42 /* too many parameters to function */
  348. #define ERR_TOOFPARAMS 43 /* too few parameters to function */
  349. #define ERR_CASERANGE 44 /* invalid case range */
  350. #define ERR_UNEXPECTEOF 45 /* unexpected end of file */
  351. #define ERR_OTH 46 /* (custom error) */
  352. #define ERR_SYS 47 /* (system limitation) */
  353. #define ERRA_INVALIDREL 48 /* invalid relocation in expression */
  354. /* alignment sizes */
  355. #ifdef MC68000
  356. /*
  357. * MC68000 is a 16-bit processor. Word alignment is OK in all cases
  358. */
  359. #define AL_CHAR 1
  360. #define AL_SHORT 2
  361. #define AL_LONG 2
  362. #define AL_POINTER 2
  363. #define AL_FLOAT 2
  364. #define AL_DOUBLE 2
  365. #define AL_STRUCT 2
  366. #define AL_FUNC 2
  367. #define AL_DEFAULT 2 /* alignment suitable for all types */
  368. #endif /* MC68000 */
  369. #ifdef MC68010
  370. /*
  371. * MC68010 is a 16-bit processor. Word alignment is OK in all cases
  372. */
  373. #define AL_CHAR 1
  374. #define AL_SHORT 2
  375. #define AL_LONG 2
  376. #define AL_POINTER 2
  377. #define AL_FLOAT 2
  378. #define AL_DOUBLE 2
  379. #define AL_STRUCT 2
  380. #define AL_FUNC 2
  381. #define AL_DEFAULT 2 /* alignment suitable for all types */
  382. #endif /* MC68010 */
  383. #ifdef MC68020
  384. /*
  385. * perhaps not necessary, but useful: 32-bit alignment for 32-bit types
  386. */
  387. #define AL_CHAR 1
  388. #define AL_SHORT 2
  389. #define AL_LONG 4
  390. #define AL_POINTER 4
  391. #define AL_FLOAT 4
  392. #define AL_DOUBLE 4
  393. #define AL_STRUCT 4
  394. #define AL_FUNC 4
  395. #define AL_DEFAULT 4 /* alignment suitable for all types */
  396. #endif /* MC68020 */
  397. #ifdef MC68030
  398. /*
  399. * perhaps not necessary, but useful: 32-bit alignment for 32-bit types
  400. */
  401. #define AL_CHAR 1
  402. #define AL_SHORT 2
  403. #define AL_LONG 4
  404. #define AL_POINTER 4
  405. #define AL_FLOAT 4
  406. #define AL_DOUBLE 4
  407. #define AL_STRUCT 4
  408. #define AL_FUNC 4
  409. #define AL_DEFAULT 4 /* alignment suitable for all types */
  410. #endif /* MC68030 */
  411. #ifdef MC68040
  412. /*
  413. * perhaps not necessary, but useful: 32-bit alignment for 32-bit types
  414. */
  415. #define AL_CHAR 1
  416. #define AL_SHORT 2
  417. #define AL_LONG 4
  418. #define AL_POINTER 4
  419. #define AL_FLOAT 4
  420. #define AL_DOUBLE 4
  421. #define AL_STRUCT 4
  422. #define AL_FUNC 4
  423. #define AL_DEFAULT 4 /* alignment suitable for all types */
  424. #endif /* MC68040 */
  425. #ifdef INTEL_386
  426. /*
  427. * perhaps not necessary, but useful: 32-bit alignment for 32-bit types
  428. */
  429. #define AL_CHAR 1
  430. #define AL_SHORT 2
  431. #define AL_LONG 4
  432. #define AL_POINTER 4
  433. #define AL_FLOAT 4
  434. #define AL_DOUBLE 4
  435. #define AL_STRUCT 4
  436. #define AL_FUNC 4
  437. #define AL_DEFAULT 4 /* alignment suitable for all types */
  438. #endif /* INTEL_386 */
  439. #ifdef SPARC
  440. #define AL_CHAR 1
  441. #define AL_SHORT 2
  442. #define AL_LONG 4
  443. #define AL_POINTER 4
  444. #define AL_FLOAT 4
  445. #define AL_DOUBLE 8
  446. #define AL_STRUCT 8
  447. #define AL_FUNC 4
  448. #define AL_DEFAULT 8 /* alignment suitable for all types */
  449. #endif /* SPARC */
  450. int *_xalloc(int);
  451. struct sym *search();
  452. struct sym *gsearch();
  453. char *strsave();
  454. TYP *expression();
  455. TYP *exprnc();
  456. TYP *cast_op();
  457. TYP *mk_type();
  458. long intexpr();
  459. int getch();
  460. //void error();
  461. void getsym();
  462. void needpunc();
  463. void initsym();
  464. void append();
  465. long strip_icon();
  466. long push_param();
  467. void do_warning(char *,...);
  468. struct sym *symremove();
  469. void getidstr();
  470. void skipspace();
  471. void msg(char *s);
  472. void rel_local();
  473. void rel_global();
  474. void clean_up();
  475. void out_init();
  476. void out_close();
  477. void do_compile();
  478. void initpch();
  479. void closepch();
  480. void insert(SYM *sp,HTABLE *table);
  481. #ifdef DUAL_STACK
  482. extern void *dualstack;
  483. extern void *ds_currentlo,*ds_currenthi;
  484. void ds_allocatleast(unsigned int size);
  485. void ds_free(void);
  486. #define ds_remaining (unsigned int)((char *)ds_currenthi-(char *)dualstack)
  487. #define ds_ensure(size) ((ds_remaining>=(size) ? 0 : ds_allocatleast(size),0), dualstack)
  488. #define ds_pop(target) (void)((target)!=ds_currentlo ? (dualstack = (target)) : ds_free(),0)
  489. #ifdef PC
  490. #define ds_update(ptr) (void)(dualstack=(ptr),dualstack+=(-(size_t)dualstack)&3,0)
  491. #else
  492. #define ds_update(ptr) (void)(dualstack=(ptr),1&(short)(long)dualstack ? ++dualstack,0 : 0,0)
  493. #endif
  494. #define ds_var(type) (type)dualstack
  495. #define DS_BSIZE 6000 // we set aside 6 kb for each stack block
  496. #endif
  497. #endif
  498. // vim:ts=4:sw=4