home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dosdisas.zip / dccsrcoo.zip / dcc.h < prev    next >
C/C++ Source or Header  |  1997-10-14  |  18KB  |  404 lines

  1. /****************************************************************************
  2.  *$Log:    dcc.h,v $
  3.  * Revision 2.26  94/03/28  16:30:32  emmerik
  4.  * Added bSigs to prog structure. TRUE if signatures available
  5.  * 
  6.  * Revision 2.25  94/03/14  08:56:03  cifuente
  7.  * flags type definition changed to flags32 (Turbo C already defines it)
  8.  * propLong() prototype
  9.  * 
  10.  * Revision 2.24  94/02/22  15:11:57  cifuente
  11.  * New flags for the code generator.  New procedures to handle
  12.  * long vars (propagation).
  13.  * 
  14.  * Revision 2.23  93/12/13  12:26:16  cifuente
  15.  * Interprocedural live analysis finished.
  16.  * 
  17.  * Revision 2.22  93/12/08  16:30:06  emmerik
  18.  * Added PROC_VARARG for pProc->flg
  19.  * 
  20.  * Revision 2.21  93/12/08  16:03:56  cifuente
  21.  * new prototypes for arguments
  22.  * 
  23.  * Revision 2.20  93/12/08  11:51:28  cifuente
  24.  * New flag REG_ARGS, new function prototypes
  25.  * 
  26.  * Revision 2.19  93/11/18  11:28:42  emmerik
  27.  * Changed some protos associated with disassembly
  28.  * 
  29.  * Revision 2.18  93/11/17  16:27:27  cifuente
  30.  * Call graph structure
  31.  * 
  32.  * Revision 2.17  93/11/12  07:53:23  cifuente
  33.  * Changed argument to function prototype idCondExpLoc()
  34.  * 
  35.  * Revision 2.16  93/11/10  17:30:20  cifuente
  36.  * Procedure header, locals
  37.  * 
  38.  * Revision 2.15  93/11/08  12:05:41  cifuente
  39.  * du1 analysis finished.  Instantiates procedure arguments for user
  40.  * declared procedures.
  41.  * 
  42.  * Revision 2.14  93/11/01  15:06:15  cifuente
  43.  * Finds byte and integer expressions
  44.  * 
  45.  * Revision 2.13  93/10/25  10:59:50  cifuente
  46.  * New SYNTHETIC instructions for d/u analysis
  47.  * 
  48.  * Revision 2.12  93/10/20  14:39:29  cifuente
  49.  * New functions to handle long operands, abstract syntax trees and
  50.  * icodes
  51.  * 
  52.  * Revision 2.11  93/10/11  11:45:51  cifuente
  53.  * New function prototypes for HIGH_LEVEL icodes, abstract syntax trees
  54.  * and local identifiers.
  55.  * 
  56.  * Revision 2.10  93/10/01  14:32:17  emmerik
  57.  * Added segMain to prog struct, so interactive disassembler will get the
  58.  * segment and offset of main correct
  59.  * 
  60.  * Revision 2.9  93/10/01  09:02:19  cifuente
  61.  * boolT type - for compilation under unix SVR4.2
  62.  * 
  63.  * Revision 2.7  93/09/29  13:52:11  cifuente
  64.  * New prototype definition for hlicode.c functions 
  65.  * 
  66.  * Revision 2.6  93/09/29  10:53:41  cifuente
  67.  * Includes abstract syntax tree (ast.h) and high-level icode (hlicode.h)
  68.  * definitions.
  69.  * 
  70.  * Revision 2.5  93/09/20  11:48:37  cifuente
  71.  * Changed tabs to spaces
  72.  * 
  73.  * Revision 2.4  93/09/17  08:35:02  cifuente
  74.  * Moved pProclist to a global
  75.  * 
  76.  * Revision 2.3  93/08/23  12:34:39  cifuente
  77.  * Interactive mode with curses
  78.  * 
  79.  * Revision 2.1  93/03/30  14:49:12  cifuente
  80.  * Compiled with gcc.
  81.  *
  82.  *          dcc project general header
  83.  ****************************************************************************/
  84.  
  85.  
  86. #include "types.h"
  87. #include "ast.h"
  88. #include "icode.h"
  89. #include "locident.h"
  90. #include "error.h"
  91. #include "graph.h"
  92. #include "bundle.h"
  93.  
  94.  
  95. /* SYMBOL TABLE */
  96. typedef struct {        
  97.     char        name[10];   /* New name for this variable   */
  98.     dword       label;      /* physical address (20 bit)    */
  99.     Int         size;       /* maximum size                 */
  100.     flags32     flg;        /* SEG_IMMED, IMPURE, WORD_OFF  */
  101.     hlType      type;       /* probable type                */
  102.     word        duVal;      /* DEF, USE, VAL                */
  103. } SYM;
  104. typedef SYM *PSYM;
  105.  
  106. typedef struct {
  107.     Int         csym;       /* No. of symbols in table  */
  108.     Int         alloc;      /* Allocation               */
  109.     PSYM        sym;        /* Symbols                  */
  110. } SYMTAB;
  111. typedef SYMTAB *PSYMTAB;
  112.  
  113.  
  114. /* STACK FRAME */
  115. typedef struct {        
  116.     COND_EXPR    *actual;    /* Expression tree of actual parameter         */
  117.     COND_EXPR     *regs;        /* For register arguments only                */
  118.     int16       off;        /* Immediate off from BP (+:args, -:params) */
  119.     byte        regOff;     /* Offset is a register (e.g. SI, DI)       */
  120.     Int         size;       /* Size                                     */
  121.     hlType      type;       /* Probable type                            */
  122.     word        duVal;      /* DEF, USE, VAL                            */
  123.     boolT        hasMacro;    /* This type needs a macro                    */
  124.     char        macro[10];    /* Macro name                                */
  125.     char        name[10];   /* Name for this symbol/argument            */
  126.     boolT        invalid;    /* Boolean: invalid entry in formal arg list*/
  127. } STKSYM;
  128. typedef STKSYM *PSTKSYM;
  129.  
  130. typedef struct _STKFRAME {
  131.     Int         csym;       /* No. of symbols in table      */
  132.     Int         alloc;      /* Allocation                   */
  133.     PSTKSYM     sym;        /* Symbols                      */
  134.     int16        minOff;        /* Initial offset in stack frame*/
  135.     int16       maxOff;     /* Maximum offset in stack frame*/
  136.     Int            cb;            /* Number of bytes in arguments    */
  137.     Int            numArgs;    /* No. of arguments in the table*/
  138. } STKFRAME;
  139. typedef STKFRAME *PSTKFRAME;
  140.  
  141. /* PROCEDURE NODE */
  142. typedef struct _proc {
  143.     dword        procEntry; /* label number                              */
  144.     char         name[SYMLEN]; /* Meaningful name for this proc          */
  145.     STATE        state;     /* Entry state                               */
  146.     Int          depth;     /* Depth at which we found it - for printing */
  147.     flags32      flg;       /* Combination of Icode & Proc flags         */
  148.     int16        cbParam;   /* Probable no. of bytes of parameters       */
  149.     STKFRAME     args;      /* Array of arguments                        */
  150.     LOCAL_ID     localId;    /* Local identifiers                         */
  151.     ID             retVal;    /* Return value - identifier                 */
  152.  
  153.     /* Icodes and control flow graph */
  154.     CIcodeRec     Icode;        /* Object with ICODE records                 */
  155.     PBB          cfg;       /* Ptr. to BB list/CFG                       */
  156.     PBB         *dfsLast;   /* Array of pointers to BBs in dfsLast
  157.                              * (reverse postorder) order                 */
  158.     Int          numBBs;    /* Number of BBs in the graph cfg            */
  159.     boolT        hasCase;   /* Procedure has a case node                 */
  160.  
  161.     /* For interprocedural live analysis */
  162.     dword         liveIn;    /* Registers used before defined             */
  163.     dword         liveOut;    /* Registers that may be used in successors     */
  164.     boolT         liveAnal;    /* Procedure has been analysed already         */
  165.  
  166.     /* Double-linked list */
  167.     struct      _proc *next;
  168.     struct        _proc *prev;
  169. } PROCEDURE;
  170. typedef PROCEDURE *PPROC;
  171.  
  172.  
  173. /* CALL GRAPH NODE */
  174. typedef struct _callGraph {
  175.     PPROC              proc;            /* Pointer to procedure in pProcList    */
  176.     Int                  numOutEdges;    /* # of out edges (ie. # procs invoked)    */
  177.     Int                  numAlloc;        /* # of out edges allocated                */
  178.     struct _callGraph **outEdges;    /* array of out edges                    */
  179. } CALL_GRAPH;
  180. typedef CALL_GRAPH *PCALL_GRAPH;
  181. #define NUM_PROCS_DELTA        250        /* delta # procs a proc invokes             */
  182.  
  183. extern PPROC pProcList;         /* Pointer to the head of the procedure list */
  184. extern PPROC pLastProc;            /* Pointer to last node of the proc list     */
  185. extern PCALL_GRAPH callGraph;    /* Pointer to the head of the call graph     */
  186. extern bundle cCode;            /* Output C procedure's declaration and code */
  187.  
  188. /* Procedure FLAGS */
  189. #define PROC_BADINST   0x000100 /* Proc contains invalid or 386 instruction */
  190. #define PROC_IJMP      0x000200 /* Proc incomplete due to indirect jmp         */
  191. #define PROC_ICALL     0x000400 /* Proc incomplete due to indirect call        */
  192. #define PROC_HLL       0x001000 /* Proc is likely to be from a HLL            */
  193. #define CALL_PASCAL    0x002000 /* Proc uses Pascal calling convention        */
  194. #define CALL_C         0x004000 /* Proc uses C calling convention            */
  195. #define CALL_UNKNOWN   0x008000 /* Proc uses unknown calling convention        */
  196. #define PROC_NEAR      0x010000 /* Proc exits with near return                */
  197. #define PROC_FAR       0x020000 /* Proc exits with far return                */
  198. #define GRAPH_IRRED    0x100000 /* Proc generates an irreducible graph        */
  199. #define SI_REGVAR       0x200000 /* SI is used as a stack variable             */
  200. #define DI_REGVAR       0x400000 /* DI is used as a stack variable             */
  201. #define PROC_IS_FUNC   0x800000    /* Proc is a function                         */
  202. #define REG_ARGS      0x1000000 /* Proc has registers as arguments            */
  203. #define    PROC_VARARG      0x2000000    /* Proc has variable arguments                */
  204. #define PROC_OUTPUT   0x4000000 /* C for this proc has been output             */
  205. #define PROC_RUNTIME  0x8000000 /* Proc is part of the runtime support        */
  206. #define PROC_ISLIB     0x10000000 /* Proc is a library function                */
  207. #define PROC_ASM     0x20000000 /* Proc is an intrinsic assembler routine   */
  208. #define PROC_IS_HLL  0x40000000 /* Proc has HLL prolog code                    */
  209. #define CALL_MASK    0xFFFF9FFF /* Masks off CALL_C and CALL_PASCAL             */
  210.  
  211. /* duVal FLAGS */
  212. #define DEF 0x0010      /* Variable was first defined than used         */
  213. #define USE 0x0100      /* Variable was first used than defined         */
  214. #define VAL 0x1000      /* Variable has an initial value.  2 cases:
  215.                          * 1. When variable is used first (ie. global)
  216.                          * 2. When a value is moved into the variable
  217.                          *    for the first time.                       */
  218. #define USEVAL  0x1100  /* Use and Val                                  */
  219.  
  220.  
  221. /**** Global variables ****/
  222.  
  223. extern char *asm1_name, *asm2_name; /* Assembler output filenames         */
  224.  
  225. typedef struct {            /* Command line option flags */
  226.     unsigned verbose        : 1;
  227.     unsigned VeryVerbose    : 1;
  228.     unsigned asm1           : 1;    /* Early disassembly listing */
  229.     unsigned asm2           : 1;    /* Disassembly listing after restruct */
  230.     unsigned Map            : 1;
  231.     unsigned Stats          : 1;
  232.     unsigned Interact       : 1;    /* Interactive mode */
  233.     unsigned Calls            : 1;    /* Follow register indirect calls */
  234.     char    filename[80];            /* The input filename */
  235. } OPTION;
  236.  
  237. extern OPTION option;       /* Command line options             */
  238. extern SYMTAB symtab;       /* Global symbol table              */
  239.  
  240. typedef struct {            /* Loaded program image parameters  */
  241.     int16       initCS;
  242.     int16       initIP;     /* These are initial load values    */
  243.     int16       initSS;     /* Probably not of great interest   */
  244.     int16       initSP;
  245.     boolT       fCOM;       /* Flag set if COM program (else EXE)*/
  246.     Int         cReloc;     /* No. of relocation table entries  */
  247.     dword      *relocTable; /* Ptr. to relocation table         */
  248.     byte       *map;        /* Memory bitmap ptr                */
  249.     Int         cProcs;     /* Number of procedures so far      */
  250.     Int         offMain;    /* The offset  of the main() proc   */
  251.     word        segMain;    /* The segment of the main() proc   */
  252.     boolT        bSigs;        /* True if signatures loaded        */
  253.     Int         cbImage;    /* Length of image in bytes         */
  254.     byte       *Image;      /* Allocated by loader to hold entire 
  255.                              * program image                    */
  256. } PROG;
  257.  
  258. extern PROG prog;           /* Loaded program image parameters  */
  259. extern char condExp[200];    /* Conditional expression buffer     */
  260. extern char callBuf[100];    /* Function call buffer                */
  261. extern dword duReg[30];        /* def/use bits for registers        */
  262. extern dword maskDuReg[30];    /* masks off du bits for regs        */
  263.  
  264. /* Registers used by icode instructions */
  265. static char *allRegs[21] = {"ax", "cx", "dx", "bx", "sp", "bp", 
  266.                             "si", "di", "es", "cs", "ss", "ds", 
  267.                             "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh",
  268.                             "tmp"};
  269.  
  270. /* Memory map states */
  271. #define BM_UNKNOWN  0   /* Unscanned memory     */
  272. #define BM_DATA     1   /* Data                 */
  273. #define BM_CODE     2   /* Code                 */
  274. #define BM_IMPURE   3   /* Used as Data and Code*/
  275.  
  276. /* Intermediate instructions statistics */
  277. typedef struct {
  278.     Int        numBBbef;        /* number of basic blocks initially            */
  279.     Int        numBBaft;        /* number of basic blocks at the end            */
  280.     Int        nOrder;            /* n-th order                                   */
  281.     Int        numLLIcode;        /* number of low-level Icode instructions      */
  282.     Int        numHLIcode;     /* number of high-level Icode instructions     */
  283.     Int        totalLL;        /* total number of low-level Icode insts       */
  284.     Int        totalHL;        /* total number of high-level Icod insts       */
  285. } STATS;
  286.  
  287. extern STATS stats; /* Icode statistics */
  288.  
  289.  
  290. /**** Global function prototypes ****/
  291.  
  292. void    FrontEnd(char *filename, PCALL_GRAPH *);            /* frontend.c   */
  293. void   *allocMem(Int cb);                                   /* frontend.c   */
  294. void   *reallocVar(void *p, Int newsize);                   /* frontend.c   */
  295. void    udm(void);                                          /* udm.c        */
  296. PBB     createCFG(PPROC pProc);                             /* graph.c      */
  297. void    compressCFG(PPROC pProc);                           /* graph.c      */
  298. void    freeCFG(PBB cfg);                                   /* graph.c      */
  299. PBB     newBB(PBB, Int, Int, byte, Int, PPROC);                /* graph.c        */
  300. void    BackEnd(char *filename, PCALL_GRAPH);               /* backend.c    */
  301. char   *cChar(byte c);                                      /* backend.c    */
  302. Int     scan(dword ip, PICODE p);                           /* scanner.c    */
  303. void    parse (PCALL_GRAPH *);                              /* parser.c     */
  304. boolT   labelSrch(PICODE pIc, Int n, dword tg, Int *pIdx);  /* parser.c     */
  305. void    setState(PSTATE state, word reg, int16 value);      /* parser.c     */
  306. Int        strSize (byte *, char);                                /* parser.c        */
  307. void    disassem(Int pass, PPROC pProc);                    /* disassem.c   */
  308. void    interactDis(PPROC initProc, Int initIC);            /* disassem.c   */
  309. void    bindIcodeOff (PPROC);                                /* idioms.c        */
  310. void    lowLevelAnalysis (PPROC pProc);                     /* idioms.c     */
  311. void    propLong (PPROC pproc);                                /* proplong.c   */
  312. boolT   JmpInst(llIcode opcode);                            /* idioms.c     */
  313. void    checkReducibility(PPROC pProc, derSeq **derG);      /* reducible.c  */
  314. queue   *appendQueue(queue **Q, BB *node);                  /* reducible.c  */
  315. void    freeDerivedSeq(derSeq *derivedG);                   /* reducible.c  */
  316. void    displayDerivedSeq(derSeq *derG);                    /* reducible.c  */
  317. void    structure(PPROC pProc, derSeq *derG);               /* control.c    */
  318. void    compoundCond (PPROC);                                /* control.c    */
  319. void    dataFlow(PPROC pProc, dword liveOut);               /* dataflow.c   */
  320. void    writeIntComment (PICODE icode, char *s);              /* comwrite.c   */
  321. void    writeProcComments (PPROC pProc, strTable *sTab);    /* comwrite.c   */
  322. void    checkStartup(PSTATE pState);                        /* chklib.c     */
  323. void    SetupLibCheck(void);                                /* chklib.c     */
  324. void    CleanupLibCheck(void);                              /* chklib.c     */
  325. boolT   LibCheck(PPROC p);                                  /* chklib.c     */
  326.  
  327. /* Exported functions from procs.c */
  328. boolT    insertCallGraph (PCALL_GRAPH, PPROC, PPROC);
  329. void    writeCallGraph (PCALL_GRAPH);
  330. void     newRegArg (PPROC, PICODE, PICODE);
  331. boolT     newStkArg (PICODE, COND_EXPR *, llIcode, PPROC);
  332. void    allocStkArgs (PICODE, Int);
  333. void    placeStkArg (PICODE, COND_EXPR *, Int);
  334. void    adjustActArgType (COND_EXPR *, hlType, PPROC);        
  335. void    adjustForArgType (PSTKFRAME, Int, hlType);
  336.  
  337. /* Exported functions from ast.c */
  338. COND_EXPR *boolCondExp (COND_EXPR *lhs, COND_EXPR *rhs, condOp op);
  339. COND_EXPR *unaryCondExp (condNodeType, COND_EXPR *exp);
  340. COND_EXPR *idCondExpGlob (int16 segValue, int16 off);
  341. COND_EXPR *idCondExpReg (byte regi, flags32 flg, LOCAL_ID *);
  342. COND_EXPR *idCondExpRegIdx (Int idx, regType);
  343. COND_EXPR *idCondExpLoc (Int off, LOCAL_ID *);
  344. COND_EXPR *idCondExpParam (Int off, PSTKFRAME argSymtab);
  345. COND_EXPR *idCondExpKte (dword kte, byte);
  346. COND_EXPR *idCondExpLong (LOCAL_ID *, opLoc, PICODE, hlFirst, Int idx, operDu,
  347.                           Int);
  348. COND_EXPR *idCondExpLongIdx (Int);
  349. COND_EXPR *idCondExpFunc (PPROC, PSTKFRAME);
  350. COND_EXPR *idCondExpOther (byte seg, byte regi, int16 off);
  351. COND_EXPR *idCondExpID (ID *, LOCAL_ID *, Int);
  352. COND_EXPR *idCondExp (PICODE, opLoc, PPROC, Int i, PICODE duIcode, operDu);
  353. COND_EXPR *copyCondExp (COND_EXPR *);
  354. void      removeRegFromLong (byte, LOCAL_ID *, COND_EXPR *);
  355. char      *walkCondExpr (COND_EXPR *exp, PPROC pProc, Int *);
  356. condId       idType (PICODE pIcode, opLoc sd);
  357. Int          hlTypeSize (COND_EXPR *, PPROC);
  358. hlType      expType (COND_EXPR *, PPROC);
  359. void      setRegDU (PICODE, byte regi, operDu);
  360. void      copyDU (PICODE, PICODE, operDu, operDu);
  361. void      changeBoolCondExpOp (COND_EXPR *, condOp);
  362. boolT      insertSubTreeReg (COND_EXPR *, COND_EXPR **, byte, LOCAL_ID *);
  363. boolT      insertSubTreeLongReg (COND_EXPR *, COND_EXPR **, Int);
  364. void      freeCondExpr (COND_EXPR *exp);
  365. COND_EXPR *concatExps (SEQ_COND_EXPR *, COND_EXPR *, condNodeType);
  366. void      initExpStk();
  367. void      pushExpStk (COND_EXPR *);
  368. COND_EXPR *popExpStk();
  369. Int          numElemExpStk();
  370. boolT      emptyExpStk();
  371.  
  372. /* Exported functions from hlicode.c */
  373. void    newAsgnHlIcode (PICODE, COND_EXPR *, COND_EXPR *);
  374. void    newCallHlIcode (PICODE);
  375. void    newUnaryHlIcode (PICODE, hlIcode, COND_EXPR *);
  376. void    newJCondHlIcode (PICODE, COND_EXPR *);
  377. void    invalidateIcode (PICODE);
  378. boolT    removeDefRegi (byte, PICODE, Int, LOCAL_ID *);
  379. void    highLevelGen (PPROC);
  380. char    *writeCall (PPROC, PSTKFRAME, PPROC, Int *);
  381. char     *write1HlIcode (HLTYPE, PPROC, Int *);
  382. char     *writeJcond (HLTYPE, PPROC, Int *);
  383. char     *writeJcondInv (HLTYPE, PPROC, Int *);
  384. Int        power2 (Int);
  385. void    writeDU (PICODE, Int);
  386. void    inverseCondOp (COND_EXPR **);
  387.  
  388. /* Exported funcions from locident.c */ 
  389. Int newByteWordRegId (LOCAL_ID *, hlType t, byte regi);
  390. Int newByteWordStkId (LOCAL_ID *, hlType t, Int off, byte regOff);
  391. Int    newIntIdxId (LOCAL_ID *, int16 seg, int16 off, byte regi, Int, hlType);
  392. Int newLongRegId (LOCAL_ID *, hlType t, byte regH, byte regL, Int idx);
  393. Int newLongStkId (LOCAL_ID *, hlType t, Int offH, Int offL);
  394. Int newLongId (LOCAL_ID *, opLoc sd, PICODE, hlFirst, Int idx, operDu, Int);
  395. boolT checkLongEq (LONG_STKID_TYPE, PICODE, Int, Int, PPROC, COND_EXPR **,
  396.                    COND_EXPR **, Int);
  397. boolT checkLongRegEq (LONGID_TYPE, PICODE, Int, Int, PPROC, COND_EXPR **,
  398.                       COND_EXPR **, Int);
  399. byte otherLongRegi (byte, Int, LOCAL_ID *);
  400. void insertIdx (IDX_ARRAY *, Int);
  401. void propLongId (LOCAL_ID *, byte, byte, char *);
  402.  
  403.  
  404.