dcc.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 <bitset>
  8. #include "types.h"
  9. #include "ast.h"
  10. #include "icode.h"
  11. #include "locident.h"
  12. #include "error.h"
  13. #include "graph.h"
  14. #include "bundle.h"
  15. #include "Procedure.h"
  16. #include "BasicBlock.h"
  17. typedef llvm::iplist<Function> FunctionListType;
  18. typedef FunctionListType lFunction;
  19. typedef lFunction::iterator ilFunction;
  20. /* SYMBOL TABLE */
  21. struct SYM
  22. {
  23. SYM() : label(0),size(0),flg(0),type(TYPE_UNKNOWN)
  24. {
  25. }
  26. char name[10]; /* New name for this variable */
  27. dword label; /* physical address (20 bit) */
  28. Int size; /* maximum size */
  29. flags32 flg; /* SEG_IMMED, IMPURE, WORD_OFF */
  30. hlType type; /* probable type */
  31. eDuVal duVal; /* DEF, USE, VAL */
  32. };
  33. typedef std::vector<SYM> SYMTAB;
  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=0x00000100,/* Proc contains invalid or 386 instruction */
  59. PROC_IJMP =0x00000200,/* Proc incomplete due to indirect jmp */
  60. PROC_ICALL =0x00000400, /* Proc incomplete due to indirect call */
  61. PROC_HLL =0x00001000, /* Proc is likely to be from a HLL */
  62. CALL_PASCAL =0x00002000, /* Proc uses Pascal calling convention */
  63. CALL_C =0x00004000, /* Proc uses C calling convention */
  64. CALL_UNKNOWN=0x00008000, /* Proc uses unknown calling convention */
  65. PROC_NEAR =0x00010000, /* Proc exits with near return */
  66. PROC_FAR =0x00020000, /* Proc exits with far return */
  67. GRAPH_IRRED =0x00100000, /* Proc generates an irreducible graph */
  68. SI_REGVAR =0x00200000, /* SI is used as a stack variable */
  69. DI_REGVAR =0x00400000, /* DI is used as a stack variable */
  70. PROC_IS_FUNC=0x00800000, /* Proc is a function */
  71. REG_ARGS =0x01000000, /* Proc has registers as arguments */
  72. PROC_VARARG =0x02000000, /* Proc has variable arguments */
  73. PROC_OUTPUT =0x04000000, /* C for this proc has been output */
  74. PROC_RUNTIME=0x08000000, /* 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 std::bitset<32> duReg[30]; /* def/use bits for registers */
  115. //extern dword duReg[30]; /* def/use bits for registers */
  116. extern dword maskDuReg[30]; /* masks off du bits for regs */
  117. /* Registers used by icode instructions */
  118. static constexpr const char *allRegs[21] = {"ax", "cx", "dx", "bx", "sp", "bp",
  119. "si", "di", "es", "cs", "ss", "ds",
  120. "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh",
  121. "tmp"};
  122. /* Memory map states */
  123. #define BM_UNKNOWN 0 /* Unscanned memory */
  124. #define BM_DATA 1 /* Data */
  125. #define BM_CODE 2 /* Code */
  126. #define BM_IMPURE 3 /* Used as Data and Code*/
  127. /* Intermediate instructions statistics */
  128. struct STATS
  129. {
  130. Int numBBbef; /* number of basic blocks initially */
  131. Int numBBaft; /* number of basic blocks at the end */
  132. Int nOrder; /* n-th order */
  133. Int numLLIcode; /* number of low-level Icode instructions */
  134. Int numHLIcode; /* number of high-level Icode instructions */
  135. Int totalLL; /* total number of low-level Icode insts */
  136. Int totalHL; /* total number of high-level Icod insts */
  137. };
  138. extern STATS stats; /* Icode statistics */
  139. /**** Global function prototypes ****/
  140. void FrontEnd(char *filename, CALL_GRAPH * *); /* frontend.c */
  141. void *allocMem(Int cb); /* frontend.c */
  142. void udm(void); /* udm.c */
  143. void freeCFG(BB * cfg); /* graph.c */
  144. BB * newBB(BB *, Int, Int, byte, Int, Function *); /* graph.c */
  145. void BackEnd(char *filename, CALL_GRAPH *); /* backend.c */
  146. char *cChar(byte c); /* backend.c */
  147. eErrorId scan(dword ip, ICODE * p); /* scanner.c */
  148. void parse (CALL_GRAPH * *); /* 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. bool 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. bool LibCheck(Function &p); /* chklib.c */
  157. /* Exported functions from procs.c */
  158. boolT insertCallGraph (CALL_GRAPH *, ilFunction, ilFunction);
  159. boolT newStkArg (ICODE *, COND_EXPR *, llIcode, Function *);
  160. void allocStkArgs (ICODE *, Int);
  161. void placeStkArg (ICODE *, COND_EXPR *, Int);
  162. void adjustActArgType (COND_EXPR *, hlType, Function *);
  163. /* Exported functions from ast.c */
  164. void removeRegFromLong (byte, LOCAL_ID *, COND_EXPR *);
  165. std::string walkCondExpr (const COND_EXPR *exp, Function * pProc, Int *);
  166. Int hlTypeSize (const COND_EXPR *, Function *);
  167. hlType expType (const COND_EXPR *, Function *);
  168. bool insertSubTreeReg(COND_EXPR *, COND_EXPR **, byte, LOCAL_ID *);
  169. bool insertSubTreeLongReg (COND_EXPR *, COND_EXPR **, Int);
  170. /* Exported functions from hlicode.c */
  171. std::string writeCall (Function *, STKFRAME *, Function *, Int *);
  172. char *write1HlIcode (HLTYPE, Function *, Int *);
  173. char *writeJcond (HLTYPE, Function *, Int *);
  174. char *writeJcondInv (HLTYPE, Function *, Int *);
  175. Int power2 (Int);
  176. void inverseCondOp (COND_EXPR **);
  177. /* Exported funcions from locident.c */
  178. boolT checkLongEq (LONG_STKID_TYPE, iICODE, Int, Function *, Assignment &asgn, Int);
  179. boolT checkLongRegEq (LONGID_TYPE, iICODE, Int, Function *, COND_EXPR *&, COND_EXPR *&, Int);
  180. byte otherLongRegi (byte, Int, LOCAL_ID *);
  181. void insertIdx (IDX_ARRAY *, Int);