dcc.h 9.5 KB

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