dcc.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. {
  22. SYM() : label(0),size(0),flg(0),type(TYPE_UNKNOWN)
  23. {
  24. }
  25. char name[10]; /* New name for this variable */
  26. dword label; /* physical address (20 bit) */
  27. Int size; /* maximum size */
  28. flags32 flg; /* SEG_IMMED, IMPURE, WORD_OFF */
  29. hlType type; /* probable type */
  30. eDuVal duVal; /* DEF, USE, VAL */
  31. };
  32. typedef std::vector<SYM> SYMTAB;
  33. /* CALL GRAPH NODE */
  34. struct CALL_GRAPH
  35. {
  36. ilFunction proc; /* Pointer to procedure in pProcList */
  37. std::vector<CALL_GRAPH *> outEdges; /* array of out edges */
  38. public:
  39. void write();
  40. CALL_GRAPH() : outEdges(0)
  41. {
  42. }
  43. public:
  44. void writeNodeCallGraph(Int indIdx);
  45. boolT insertCallGraph(ilFunction caller, ilFunction callee);
  46. boolT insertCallGraph(Function *caller, ilFunction callee);
  47. void insertArc(ilFunction newProc);
  48. };
  49. #define NUM_PROCS_DELTA 5 /* delta # procs a proc invokes */
  50. //extern std::list<Function> pProcList;
  51. extern FunctionListType pProcList;
  52. extern CALL_GRAPH * callGraph; /* Pointer to the head of the call graph */
  53. extern bundle cCode; /* Output C procedure's declaration and code */
  54. /* Procedure FLAGS */
  55. enum PROC_FLAGS
  56. {
  57. PROC_BADINST=0x00000100,/* Proc contains invalid or 386 instruction */
  58. PROC_IJMP =0x00000200,/* Proc incomplete due to indirect jmp */
  59. PROC_ICALL =0x00000400, /* Proc incomplete due to indirect call */
  60. PROC_HLL =0x00001000, /* Proc is likely to be from a HLL */
  61. CALL_PASCAL =0x00002000, /* Proc uses Pascal calling convention */
  62. CALL_C =0x00004000, /* Proc uses C calling convention */
  63. CALL_UNKNOWN=0x00008000, /* Proc uses unknown calling convention */
  64. PROC_NEAR =0x00010000, /* Proc exits with near return */
  65. PROC_FAR =0x00020000, /* Proc exits with far return */
  66. GRAPH_IRRED =0x00100000, /* Proc generates an irreducible graph */
  67. SI_REGVAR =0x00200000, /* SI is used as a stack variable */
  68. DI_REGVAR =0x00400000, /* DI is used as a stack variable */
  69. PROC_IS_FUNC=0x00800000, /* Proc is a function */
  70. REG_ARGS =0x01000000, /* Proc has registers as arguments */
  71. PROC_VARARG =0x02000000, /* Proc has variable arguments */
  72. PROC_OUTPUT =0x04000000, /* C for this proc has been output */
  73. PROC_RUNTIME=0x08000000, /* Proc is part of the runtime support */
  74. PROC_ISLIB =0x10000000, /* Proc is a library function */
  75. PROC_ASM =0x20000000, /* Proc is an intrinsic assembler routine */
  76. PROC_IS_HLL =0x40000000 /* Proc has HLL prolog code */
  77. };
  78. #define CALL_MASK 0xFFFF9FFF /* Masks off CALL_C and CALL_PASCAL */
  79. /**** Global variables ****/
  80. extern char *asm1_name, *asm2_name; /* Assembler output filenames */
  81. typedef struct { /* Command line option flags */
  82. unsigned verbose : 1;
  83. unsigned VeryVerbose : 1;
  84. unsigned asm1 : 1; /* Early disassembly listing */
  85. unsigned asm2 : 1; /* Disassembly listing after restruct */
  86. unsigned Map : 1;
  87. unsigned Stats : 1;
  88. unsigned Interact : 1; /* Interactive mode */
  89. unsigned Calls : 1; /* Follow register indirect calls */
  90. char filename[80]; /* The input filename */
  91. } OPTION;
  92. extern OPTION option; /* Command line options */
  93. extern SYMTAB symtab; /* Global symbol table */
  94. struct PROG /* Loaded program image parameters */
  95. {
  96. int16 initCS;
  97. int16 initIP; /* These are initial load values */
  98. int16 initSS; /* Probably not of great interest */
  99. int16 initSP;
  100. boolT fCOM; /* Flag set if COM program (else EXE)*/
  101. Int cReloc; /* No. of relocation table entries */
  102. dword *relocTable; /* Ptr. to relocation table */
  103. byte *map; /* Memory bitmap ptr */
  104. Int cProcs; /* Number of procedures so far */
  105. Int offMain; /* The offset of the main() proc */
  106. word segMain; /* The segment of the main() proc */
  107. boolT bSigs; /* True if signatures loaded */
  108. Int cbImage; /* Length of image in bytes */
  109. byte *Image; /* Allocated by loader to hold entire
  110. * program image */
  111. };
  112. extern PROG prog; /* Loaded program image parameters */
  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 constexpr 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 udm(void); /* udm.c */
  141. void freeCFG(BB * cfg); /* graph.c */
  142. BB * newBB(BB *, Int, Int, byte, Int, Function *); /* graph.c */
  143. void BackEnd(char *filename, CALL_GRAPH *); /* backend.c */
  144. char *cChar(byte c); /* backend.c */
  145. eErrorId scan(dword ip, ICODE * p); /* scanner.c */
  146. void parse (CALL_GRAPH * *); /* parser.c */
  147. Int strSize (byte *, char); /* parser.c */
  148. void disassem(Int pass, Function * pProc); /* disassem.c */
  149. void interactDis(Function * initProc, Int initIC); /* disassem.c */
  150. bool JmpInst(llIcode opcode); /* idioms.c */
  151. queue::iterator appendQueue(queue &Q, BB *node); /* reducible.c */
  152. void SetupLibCheck(void); /* chklib.c */
  153. void CleanupLibCheck(void); /* chklib.c */
  154. bool LibCheck(Function &p); /* chklib.c */
  155. /* Exported functions from procs.c */
  156. boolT insertCallGraph (CALL_GRAPH *, ilFunction, ilFunction);
  157. boolT newStkArg (ICODE *, COND_EXPR *, llIcode, Function *);
  158. void allocStkArgs (ICODE *, Int);
  159. void placeStkArg (ICODE *, COND_EXPR *, Int);
  160. void adjustActArgType (COND_EXPR *, hlType, Function *);
  161. /* Exported functions from ast.c */
  162. void removeRegFromLong (byte, LOCAL_ID *, COND_EXPR *);
  163. std::string walkCondExpr (const COND_EXPR *exp, Function * pProc, Int *);
  164. Int hlTypeSize (const COND_EXPR *, Function *);
  165. hlType expType (const COND_EXPR *, Function *);
  166. bool insertSubTreeReg(COND_EXPR *, COND_EXPR **, byte, LOCAL_ID *);
  167. bool insertSubTreeLongReg (COND_EXPR *, COND_EXPR **, Int);
  168. /* Exported functions from hlicode.c */
  169. std::string writeCall (Function *, STKFRAME *, Function *, Int *);
  170. char *write1HlIcode (HLTYPE, Function *, Int *);
  171. char *writeJcond (HLTYPE, Function *, Int *);
  172. char *writeJcondInv (HLTYPE, Function *, Int *);
  173. Int power2 (Int);
  174. void inverseCondOp (COND_EXPR **);
  175. /* Exported funcions from locident.c */
  176. boolT checkLongEq (LONG_STKID_TYPE, iICODE, Int, Int, Function *, COND_EXPR **,COND_EXPR **, Int);
  177. boolT checkLongRegEq (LONGID_TYPE, iICODE, Int, Int, Function *, COND_EXPR **,COND_EXPR **, Int);
  178. byte otherLongRegi (byte, Int, LOCAL_ID *);
  179. void insertIdx (IDX_ARRAY *, Int);