home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 357_01 / cstar1.exe / GLB.C < prev    next >
C/C++ Source or Header  |  1991-06-18  |  9KB  |  341 lines

  1. /*
  2.     C* -- global variable definitions.
  3.  
  4.     Source:  glb.c
  5.     Started: April 2, 1986
  6.     Version:
  7.         December 18, 1986
  8.         March 7, 1989
  9.  
  10.     PUBLIC DOMAIN SOFTWARE
  11.  
  12.     The CSTAR program was placed in    the public domain on June 15, 1991,
  13.     by its author and sole owner,
  14.  
  15.         Edward K. Ream
  16.         1617 Monroe Street
  17.         Madison, WI 53711
  18.         (608) 257-0802
  19.  
  20.     CSTAR may be used for any commercial or non-commercial purpose.
  21.  
  22.     See cstar.h or cstar.c for a DISCLAIMER OF WARRANTIES.
  23. */
  24. #define no_extern 1
  25. #include "cstar.h"
  26.  
  27. /*
  28.     ========= global tables =========
  29. */
  30. char * FAR xzp_tab [] = {
  31.     "<NULL>",
  32.     "abcd",  "add",   "adda",  "addi",  "addq",
  33.     "addx",  "and",   "addi",  "asl",   "asr",
  34.     "bchg",  "bclr",  "bra",   "bset",  "bsr",
  35.     "btst",  "bxx",   "chk",   "clr",  "cmp",
  36.     "cmpa",  "cmpi",  "cmpm",  "dbxx",  "divs",
  37.     "divu",  "eor",   "eori",  "exg",   "ext",
  38.     "jmp",   "jsr",   "lea",   "link",  "lsl",
  39.     "lsr",   "move",  "movea", "movem", "movep",
  40.     "moveq", "muls",  "mulu",  "nbcd",  "neg",
  41.     "negx",  "nop",   "not",   "or",    "ori",    
  42.     "pea",   "reset", "rol",   "ror",   "roxl",
  43.     "roxr",  "rte",   "rtr",   "rts",   "sbcd",
  44.     "sxx",   "stop",  "sub",   "suba",  "subi",
  45.     "subq",  "subx",  "swap",  "tas",   "trap",
  46.     "trapv", "tst",   "unlk",
  47.  
  48.     "mods", "modu",
  49.  
  50.     "bcc",   "bcs",   "beq",   "bge",   "bgt",
  51.     "bhi",   "ble",   "bls",   "blt",   "bmi",
  52.     "bne",   "bpl",   "bvc",   "bvs",
  53.  
  54.     "dbcc",  "dbcs",  "dbeq",  "dbf",   "dbge",
  55.     "dbgt",  "dbhi",  "dble",  "dbls",  "dblt",
  56.     "dbmi",  "dbne",  "dbpl",  "dbt",   "dbvc",
  57.     "dbvs",
  58.  
  59.     "scc",   "scs",   "seq",   "sf",    "sge",
  60.     "sgt",   "shi",   "sle",   "sls",   "slt",
  61.     "smi",   "sne",   "spl",   "st",    "svc",
  62.     "svs",
  63.  
  64.     "addsp", "adjsp", "base",  "bss",   "data",
  65.     "dc",    "dcb",   "dcl",   "dcw",   "ds",
  66.     "dsb",   "dsl",   "dsw",   "nobase", "org",
  67.     "pop",   "push",  "subsp", "text",
  68.  
  69.     "<LABEL>", "<ULABEL>", "<LITERAL>", "<O_LOCATION>", "<LINE>",
  70.     "<Q_BRN>"
  71. };
  72.  
  73. char * FAR xlentab[] = {
  74.     "", ".b", ".w", "<error>", ".l"
  75. };
  76.  
  77. char * FAR arp_tab [] = {
  78.     "<NULL>",
  79.     "a0",  "a1",  "a2",  "a3",  "a4",  "a5",  "a6",  "a7", 
  80.     "a0w", "a1w", "a2w", "a3w", "a4w", "a5w", "a6w", "a7w",
  81.  
  82.     "d0",  "d1",  "d2",  "d3",  "d4",  "d5",  "d6",  "d7", 
  83.     "d0w", "d1w", "d2w", "d3w", "d4w", "d5w", "d6w", "d7w",
  84.     "d0b", "d1b", "d2b", "d3b", "d4b", "d5b", "d6b", "d7b",
  85.  
  86.     "ccr", "pc", "ssp", "sspw", "ssr",
  87.  
  88.     "cc", "cs", "eq", "f",  "ge", "gt", "hi", "le",
  89.     "ls", "lt", "mi", "ne", "pl", "t",  "vc", "vs"
  90. };
  91.  
  92. int FAR reg_size[] = {0,
  93.     4, 4, 4, 4, 4, 4, 4, 4,
  94.     2, 2, 2, 2, 2, 2, 2, 2,
  95.     4, 4, 4, 4, 4, 4, 4, 4,
  96.     2, 2, 2, 2, 2, 2, 2, 2,
  97.     1, 1, 1, 1, 1, 1, 1, 1,
  98.     1, 4, 4, 2, 2 };
  99.  
  100.  
  101. int FAR reg_itb[] = {
  102.     0,
  103.     
  104.     0, 1, 2, 3, 4, 5, 6, 7,
  105.     0, 1, 2, 3, 4, 5, 6, 7,
  106.     
  107.     0, 1, 2, 3, 4, 5, 6, 7,
  108.     0, 1, 2, 3, 4, 5, 6, 7,
  109.     0, 1, 2, 3, 4, 5, 6, 7
  110. };
  111.  
  112. int FAR d_rtb[] = {
  113.     R_D0, R_D1, R_D2, R_D3, R_D4, R_D5, R_D6, R_D7
  114. };
  115.  
  116. int FAR a_rtb[] = {
  117.     R_A0, R_A1, R_A2, R_A3, R_A4, R_A5, R_A6, R_A7
  118. };
  119.  
  120. int FAR x_rtb[] = {
  121.     0,
  122.     R_A0, R_A1, R_A2, R_A3, R_A4, R_A5, R_A6, R_A7,
  123.     R_A0, R_A1, R_A2, R_A3, R_A4, R_A5, R_A6, R_A7,
  124.     R_D0, R_D1, R_D2, R_D3, R_D4, R_D5, R_D6, R_D7,
  125.     R_D0, R_D1, R_D2, R_D3, R_D4, R_D5, R_D6, R_D7,
  126.     R_D0, R_D1, R_D2, R_D3, R_D4, R_D5, R_D6, R_D7,
  127.     R_CCR, R_PC, R_SP, R_SPW, R_SR, R_USP,
  128.     CC_CC, CC_CS, CC_EQ, CC_F, CC_GE, CC_GT, CC_HI, CC_LE,
  129.     CC_LS, CC_LT, CC_MI, CC_NE, CC_PL, CC_T, CC_VC, CC_VS
  130. };
  131.  
  132. char * FAR kp_tab [] = {
  133.     "<NULL>",
  134.  
  135.     "auto", "char", "const", "double", "extern",
  136.     "float", "int", "long", "register", "short",
  137.     "static", "typedef", "signed", "struct", "union",
  138.     "unsigned", "void", "volatile",
  139.  
  140.     "break", "case",
  141.     "continue", "default", "do", "else", "for",
  142.     "goto",    "if", "return", "switch", "while",
  143.  
  144.     "entry", "enum", "sizeof",
  145.  
  146.     ",", "<newline>", ";", "[", "{", "(", "]", "}", ")",
  147.  
  148.     ":", "?",
  149.  
  150.     "ARRAY", "->", ".", "&&", "||", ",",
  151.  
  152.     "=",
  153.     "&=", "/=", "<<=", "-=", "%=",
  154.     "|=", "+=", ">>=", "*=", "^=",
  155.  
  156.     "&", "|", "+", "*", "^",
  157.     "/", "<<", "-", "%", ">>",
  158.  
  159.     "==", ">=", ">", "<=", "<", "!=",
  160.  
  161.     "--", "++", "!", "~",
  162.  
  163.     "(cast)", "post--", "post++", "pre--", "pre++",
  164.     "&", "-", "+", "*", "call",
  165.     
  166.     "A-reg", "CC-reg", "character", "D-reg", "EOF", "EOP",
  167.     "X-reg", "id", "integer", "long integer", "pseudo-op",
  168.     "string", "pseudo-function", 
  169.     "boolean", "label"
  170. };
  171.  
  172. /*
  173.     ========= global variables =========
  174. */
  175.  
  176. /*
  177.     The variable ch should be allocated a register if possible.
  178. */
  179. unsigned char     ch;    /* current character */
  180.  
  181. int    nest_flag = FALSE;    /* TRUE if comments can nest.    */
  182. int    full_c    = FALSE;    /* TRUE if full C allowed.            */
  183. int    array_opt = FALSE;    /* TRUE for normal subscript computation    */
  184. int    no_local  = TRUE;    /* TRUE for no local names in ass'y output    */
  185.  
  186. /*
  187.     Internal flags:  These allow accurate timing statistics to be gathered
  188.     by suppressing various phases of the compiler.  They also enable dumping of
  189.     internal data structures and tokens to the output.
  190.  
  191.     These flags correspond to the following command line arguments.
  192.  
  193.     -noparse  Suppress parsing.
  194.     -nomacro  Suppress macro expansion.
  195.     -nogen    Suppress code generation.
  196.     -nopeep   Suppress peephole optimizer.
  197.  
  198.     -tokens   Output tokens.
  199.     -tree     Output parse tree.
  200.     -code1    Output code list before peephole.
  201.     -code2    Output code list after peephole.
  202. */
  203. bool nomacro_flag = FALSE;
  204. bool noparse_flag = FALSE;
  205. bool nogen_flag = FALSE;
  206. bool nopeep_flag = FALSE;
  207.  
  208. bool token_flag  = FALSE;
  209. bool tree_flag   = FALSE;
  210. bool code1_flag  = FALSE;
  211. bool code2_flag  = FALSE;
  212.  
  213.  
  214. /*
  215.     ----- PREPROCESSOR AND TOKENS -----
  216. */
  217.  
  218. /*
  219.     The code assumes that no token or string will ever be longer than
  220.     MAX_SYMBOL.  If that ever is not so the program may crash.  Thus,
  221.     MAX_SYMBOL should be very large -- say 1000 or more.
  222. */
  223. char    t_symbol [MAX_SYMBOL];    /* The spelling of the token.        */
  224. int    t_length;        /* The length of the token (in the text)*/
  225. long    t_value;        /* Value of integer constants.        */
  226. int    t_type;            /* The token itself or it's class.    */
  227. int    t_subtype;        /* The subtype of token class.        */
  228.  
  229. /*
  230.     The following globals are set ONLY by the system module.
  231.     They are picked up and used by the preprocessor and the
  232.     parser.
  233. */
  234. char *    t_file;            /* Name of current input file.    */
  235. int    t_line;            /* Line number within file.    */
  236. int    t_inlevel;        /* Current input file level.    */
  237.  
  238. /*
  239.     Globals for use internally to the token routines.
  240. */
  241. short    t_ifstack [MAX_IF];    /* Stack for nexted #if's    */
  242. int    t_iflevel;        /* Nexting depth of #if's    */
  243. long    t_errcount;        /* Number of errors seen.    */
  244.  
  245. int    t_evalstk [MAX_EVAL];    /* Operator/operand stack.    */
  246. int    t_opptr;        /* Operator index to evalstk[].    */
  247. int    t_valptr;        /* Operand index to evalstk[].    */
  248.  
  249. struct u_node undef_list;    /* Initial undef list for PP.    */
  250.  
  251. /*
  252.     Defines for the path table.
  253.     This table is used to search for #include files.
  254. */
  255. char *    paths [MAX_PATHS];    /* Pointers to path names.    */
  256. int    n_paths  = 0;        /* Number of paths defined.    */
  257.  
  258. /*
  259.     ----- PARSING -----
  260. */
  261.  
  262. /*
  263.     this is just too inconvenient to return as a return-value
  264.     it is needed for code generation
  265. */
  266. int call_1arg;        /* Incremented by the parser when it sets up a
  267.                 call to a function having one argument */
  268.  
  269. /*
  270.     ----- CODE GENERATION -----
  271. */
  272. /* g1.c */
  273. int segment;
  274. struct type_node * intrn_decl;    /* needed from exp.c to pick up block and
  275.                 implicit internal decls e.g. strings */
  276. struct type_node * intrn_ptr;
  277.  
  278. /*
  279.     reg.c:
  280.     These do not appear in glb.h since they are used by so few routines.
  281. */
  282. int    na_free;        /* needed by g2.c to check for temps */
  283. int     nd_free;
  284. int    sa_used [8];        /* needed by g1.c for register push and pop */
  285. int    sd_used [8];
  286. int    sa_push [8];
  287. int    sd_push [8];
  288. int    sa_avail[8];
  289. int    sd_avail[8];
  290. int    ss_count, ss_forks, ss_bot;    /* needed by g2.c for branch points */
  291.  
  292. /* exp.c */
  293.  
  294. struct node *    code_head;    /* Head of list of global code list.    */
  295. struct node *    code_tail;    /* Pointer to last node in list.    */
  296.  
  297. int    u_lab = 0;    /* Number of the current user label. */
  298. int    cur_lab = 0;    /* Number of the current internal label. */
  299. struct scope_node scope;    /* Current scope. */
  300.  
  301. int        f_base;        /* See par.c; this has no conceivable use as
  302.                         an integer */
  303.  
  304. struct type_node *    byte_type;    /* Fixed typenode */
  305. struct type_node *    int_type;    /* Fixed integer typenode */
  306. struct type_node *    long_type;    /* Fixed long typenode */
  307.  
  308. struct node * a0_loc;
  309. struct node * d0_loc;
  310. struct node * a6_loc;        /* frame reg    */
  311. struct node * a7_loc;        /* stack itself */
  312. struct node * sr_loc;        /* sr        */
  313. struct node * ccr_loc;        /* ccr        */
  314. struct node * push_loc;        /*  *--sp    */
  315. struct node * nopush_loc;    /*  *--sp    */
  316. struct node * pop_loc;        /*  *sp++    */
  317. struct node * one_loc;        /* constant 1    */
  318. struct node * zero_loc;
  319.  
  320. /* g2.c */
  321. struct diop ops_true = {X_BRA, Q_BRN};
  322. struct diop ops_false = {Q_BRN, X_BRA};
  323. struct diop ops_cond = {X_BNE, X_BEQ};
  324.  
  325. /*
  326.     ----- C* REGISTERS -----
  327. */
  328.  
  329. /* -----
  330. long d0, d1, d2, d3, d4, d5, d6, d7;
  331. char *a0, *a1, *a2, *a3, *a4, *a5, *a6, *a7, *sp;
  332. int a0w, a1w, a2w, a3w, a4w, a5w, a6w;
  333. char ccr;
  334. int ssr, spw;
  335. ----- */
  336.  
  337. /* remove these after the next comp */
  338. char *    f_name;
  339. char *    f_locals;
  340. char *  f_formals;
  341.