dcc.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /****************************************************************************
  2. * dcc project general header
  3. * (C) Cristina Cifuentes, Mike van Emmerik
  4. ****************************************************************************/
  5. #pragma once
  6. #include "types.h"
  7. #include "ast.h"
  8. #include "icode.h"
  9. #include "locident.h"
  10. #include "error.h"
  11. #include "graph.h"
  12. #include "bundle.h"
  13. #include "Procedure.h"
  14. #include "BasicBlock.h"
  15. typedef std::list<Function> lFunction;
  16. typedef std::list<Function>::iterator ilFunction;
  17. /* SYMBOL TABLE */
  18. struct SYM {
  19. char name[10]; /* New name for this variable */
  20. dword label; /* physical address (20 bit) */
  21. Int size; /* maximum size */
  22. flags32 flg; /* SEG_IMMED, IMPURE, WORD_OFF */
  23. hlType type; /* probable type */
  24. eDuVal duVal; /* DEF, USE, VAL */
  25. };
  26. struct SYMTAB
  27. {
  28. Int csym; /* No. of symbols in table */
  29. Int alloc; /* Allocation */
  30. SYM * sym; /* Symbols */
  31. };
  32. /* CALL GRAPH NODE */
  33. struct CALL_GRAPH
  34. {
  35. ilFunction proc; /* Pointer to procedure in pProcList */
  36. std::vector<CALL_GRAPH *> outEdges; /* array of out edges */
  37. public:
  38. void write();
  39. CALL_GRAPH() : outEdges(0)
  40. {
  41. }
  42. public:
  43. void writeNodeCallGraph(Int indIdx);
  44. boolT insertCallGraph(ilFunction caller, ilFunction callee);
  45. boolT insertCallGraph(Function *caller, ilFunction callee);
  46. void insertArc(ilFunction newProc);
  47. };
  48. #define NUM_PROCS_DELTA 5 /* delta # procs a proc invokes */
  49. extern std::list<Function> pProcList;
  50. extern CALL_GRAPH * callGraph; /* Pointer to the head of the call graph */
  51. extern bundle cCode; /* Output C procedure's declaration and code */
  52. /* Procedure FLAGS */
  53. enum PROC_FLAGS
  54. {
  55. PROC_BADINST=0x000100,/* Proc contains invalid or 386 instruction */
  56. PROC_IJMP =0x000200,/* Proc incomplete due to indirect jmp */
  57. PROC_ICALL =0x000400, /* Proc incomplete due to indirect call */
  58. PROC_HLL=0x001000, /* Proc is likely to be from a HLL */
  59. CALL_PASCAL=0x002000, /* Proc uses Pascal calling convention */
  60. CALL_C=0x004000, /* Proc uses C calling convention */
  61. CALL_UNKNOWN=0x008000, /* Proc uses unknown calling convention */
  62. PROC_NEAR=0x010000, /* Proc exits with near return */
  63. PROC_FAR=0x020000, /* Proc exits with far return */
  64. GRAPH_IRRED=0x100000, /* Proc generates an irreducible graph */
  65. SI_REGVAR=0x200000, /* SI is used as a stack variable */
  66. DI_REGVAR=0x400000, /* DI is used as a stack variable */
  67. PROC_IS_FUNC=0x800000, /* Proc is a function */
  68. REG_ARGS=0x1000000, /* Proc has registers as arguments */
  69. PROC_VARARG=0x2000000, /* Proc has variable arguments */
  70. PROC_OUTPUT=0x4000000, /* C for this proc has been output */
  71. PROC_RUNTIME=0x8000000, /* Proc is part of the runtime support */
  72. PROC_ISLIB=0x10000000, /* Proc is a library function */
  73. PROC_ASM=0x20000000, /* Proc is an intrinsic assembler routine */
  74. PROC_IS_HLL=0x40000000 /* Proc has HLL prolog code */
  75. };
  76. #define CALL_MASK 0xFFFF9FFF /* Masks off CALL_C and CALL_PASCAL */
  77. /**** Global variables ****/
  78. extern char *asm1_name, *asm2_name; /* Assembler output filenames */
  79. typedef struct { /* Command line option flags */
  80. unsigned verbose : 1;
  81. unsigned VeryVerbose : 1;
  82. unsigned asm1 : 1; /* Early disassembly listing */
  83. unsigned asm2 : 1; /* Disassembly listing after restruct */
  84. unsigned Map : 1;
  85. unsigned Stats : 1;
  86. unsigned Interact : 1; /* Interactive mode */
  87. unsigned Calls : 1; /* Follow register indirect calls */
  88. char filename[80]; /* The input filename */
  89. } OPTION;
  90. extern OPTION option; /* Command line options */
  91. extern SYMTAB symtab; /* Global symbol table */
  92. struct PROG /* Loaded program image parameters */
  93. {
  94. int16 initCS;
  95. int16 initIP; /* These are initial load values */
  96. int16 initSS; /* Probably not of great interest */
  97. int16 initSP;
  98. boolT fCOM; /* Flag set if COM program (else EXE)*/
  99. Int cReloc; /* No. of relocation table entries */
  100. dword *relocTable; /* Ptr. to relocation table */
  101. byte *map; /* Memory bitmap ptr */
  102. Int cProcs; /* Number of procedures so far */
  103. Int offMain; /* The offset of the main() proc */
  104. word segMain; /* The segment of the main() proc */
  105. boolT bSigs; /* True if signatures loaded */
  106. Int cbImage; /* Length of image in bytes */
  107. byte *Image; /* Allocated by loader to hold entire
  108. * program image */
  109. };
  110. extern PROG prog; /* Loaded program image parameters */
  111. extern char condExp[200]; /* Conditional expression buffer */
  112. extern char callBuf[100]; /* Function call buffer */
  113. extern dword duReg[30]; /* def/use bits for registers */
  114. extern dword maskDuReg[30]; /* masks off du bits for regs */
  115. /* Registers used by icode instructions */
  116. static const char *allRegs[21] = {"ax", "cx", "dx", "bx", "sp", "bp",
  117. "si", "di", "es", "cs", "ss", "ds",
  118. "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh",
  119. "tmp"};
  120. /* Memory map states */
  121. #define BM_UNKNOWN 0 /* Unscanned memory */
  122. #define BM_DATA 1 /* Data */
  123. #define BM_CODE 2 /* Code */
  124. #define BM_IMPURE 3 /* Used as Data and Code*/
  125. /* Intermediate instructions statistics */
  126. struct STATS
  127. {
  128. Int numBBbef; /* number of basic blocks initially */
  129. Int numBBaft; /* number of basic blocks at the end */
  130. Int nOrder; /* n-th order */
  131. Int numLLIcode; /* number of low-level Icode instructions */
  132. Int numHLIcode; /* number of high-level Icode instructions */
  133. Int totalLL; /* total number of low-level Icode insts */
  134. Int totalHL; /* total number of high-level Icod insts */
  135. };
  136. extern STATS stats; /* Icode statistics */
  137. /**** Global function prototypes ****/
  138. void FrontEnd(char *filename, CALL_GRAPH * *); /* frontend.c */
  139. void *allocMem(Int cb); /* frontend.c */
  140. void *reallocVar(void *p, Int newsize); /* frontend.c */
  141. void udm(void); /* udm.c */
  142. void freeCFG(BB * cfg); /* graph.c */
  143. BB * newBB(BB *, Int, Int, byte, Int, Function *); /* graph.c */
  144. void BackEnd(char *filename, CALL_GRAPH *); /* backend.c */
  145. char *cChar(byte c); /* backend.c */
  146. Int scan(dword ip, ICODE * p); /* scanner.c */
  147. void parse (CALL_GRAPH * *); /* parser.c */
  148. boolT labelSrch(ICODE * pIc, Int n, dword tg, Int *pIdx); /* parser.c */
  149. Int strSize (byte *, char); /* parser.c */
  150. void disassem(Int pass, Function * pProc); /* disassem.c */
  151. void interactDis(Function * initProc, Int initIC); /* disassem.c */
  152. boolT JmpInst(llIcode opcode); /* idioms.c */
  153. queue::iterator appendQueue(queue &Q, BB *node); /* reducible.c */
  154. void SetupLibCheck(void); /* chklib.c */
  155. void CleanupLibCheck(void); /* chklib.c */
  156. boolT LibCheck(Function &p); /* chklib.c */
  157. /* Exported functions from procs.c */
  158. boolT insertCallGraph (CALL_GRAPH *, ilFunction, ilFunction);
  159. void newRegArg (Function *, ICODE *, ICODE *);
  160. boolT newStkArg (ICODE *, COND_EXPR *, llIcode, Function *);
  161. void allocStkArgs (ICODE *, Int);
  162. void placeStkArg (ICODE *, COND_EXPR *, Int);
  163. void adjustActArgType (COND_EXPR *, hlType, Function *);
  164. /* Exported functions from ast.c */
  165. void removeRegFromLong (byte, LOCAL_ID *, COND_EXPR *);
  166. std::string walkCondExpr (const COND_EXPR *exp, Function * pProc, Int *);
  167. Int hlTypeSize (const COND_EXPR *, Function *);
  168. hlType expType (const COND_EXPR *, Function *);
  169. void copyDU (ICODE *, const ICODE *, operDu, operDu);
  170. boolT insertSubTreeReg (COND_EXPR *, COND_EXPR **, byte, LOCAL_ID *);
  171. boolT insertSubTreeLongReg (COND_EXPR *, COND_EXPR **, Int);
  172. //COND_EXPR *concatExps (SEQ_COND_EXPR *, COND_EXPR *, condNodeType);
  173. void initExpStk();
  174. void pushExpStk (COND_EXPR *);
  175. COND_EXPR *popExpStk();
  176. Int numElemExpStk();
  177. boolT emptyExpStk();
  178. /* Exported functions from hlicode.c */
  179. boolT removeDefRegi (byte, ICODE *, Int, LOCAL_ID *);
  180. std::string writeCall (Function *, STKFRAME *, Function *, Int *);
  181. char *write1HlIcode (HLTYPE, Function *, Int *);
  182. char *writeJcond (HLTYPE, Function *, Int *);
  183. char *writeJcondInv (HLTYPE, Function *, Int *);
  184. Int power2 (Int);
  185. void inverseCondOp (COND_EXPR **);
  186. /* Exported funcions from locident.c */
  187. boolT checkLongEq (LONG_STKID_TYPE, ICODE *, Int, Int, Function *, COND_EXPR **,COND_EXPR **, Int);
  188. boolT checkLongRegEq (LONGID_TYPE, ICODE *, Int, Int, Function *, COND_EXPR **,COND_EXPR **, Int);
  189. byte otherLongRegi (byte, Int, LOCAL_ID *);
  190. void insertIdx (IDX_ARRAY *, Int);