home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / nasm097s.zip / NASM.H < prev    next >
C/C++ Source or Header  |  1997-11-26  |  30KB  |  751 lines

  1. /* nasm.h   main header file for the Netwide Assembler: inter-module interface
  2.  *
  3.  * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
  4.  * Julian Hall. All rights reserved. The software is
  5.  * redistributable under the licence given in the file "Licence"
  6.  * distributed in the NASM archive.
  7.  *
  8.  * initial version: 27/iii/95 by Simon Tatham
  9.  */
  10.  
  11. #ifndef NASM_NASM_H
  12. #define NASM_NASM_H
  13.  
  14. #define NASM_MAJOR_VER 0
  15. #define NASM_MINOR_VER 97
  16. #define NASM_VER "0.97"
  17.  
  18. #ifndef NULL
  19. #define NULL 0
  20. #endif
  21.  
  22. #ifndef FALSE
  23. #define FALSE 0                   /* comes in handy */
  24. #endif
  25. #ifndef TRUE
  26. #define TRUE 1
  27. #endif
  28.  
  29. #define NO_SEG -1L               /* null segment value */
  30. #define SEG_ABS 0x40000000L           /* mask for far-absolute segments */
  31.  
  32. #ifndef FILENAME_MAX
  33. #define FILENAME_MAX 256
  34. #endif
  35.  
  36. /*
  37.  * Name pollution problems: <time.h> on Digital UNIX pulls in some
  38.  * strange hardware header file which sees fit to define R_SP. We
  39.  * undefine it here so as not to break the enum below.
  40.  */
  41. #ifdef R_SP
  42. #undef R_SP
  43. #endif
  44.  
  45. /*
  46.  * We must declare the existence of this structure type up here,
  47.  * since we have to reference it before we define it...
  48.  */
  49. struct ofmt;
  50.  
  51. /*
  52.  * -------------------------
  53.  * Error reporting functions
  54.  * -------------------------
  55.  */
  56.  
  57. /*
  58.  * An error reporting function should look like this.
  59.  */
  60. typedef void (*efunc) (int severity, char *fmt, ...);
  61.  
  62. /*
  63.  * These are the error severity codes which get passed as the first
  64.  * argument to an efunc.
  65.  */
  66.  
  67. #define ERR_WARNING 0               /* warn only: no further action */
  68. #define ERR_NONFATAL 1               /* terminate assembly after phase */
  69. #define ERR_FATAL 2               /* instantly fatal: exit with error */
  70. #define ERR_PANIC 3               /* internal error: panic instantly
  71.                     * and dump core for reference */
  72. #define ERR_MASK 0x0F               /* mask off the above codes */
  73. #define ERR_NOFILE 0x10               /* don't give source file name/line */
  74. #define ERR_USAGE 0x20               /* print a usage message */
  75. #define ERR_OFFBY1 0x40               /* report error as being on the line 
  76.                     * we're just _about_ to read, not
  77.                     * the one we've just read */
  78. #define ERR_PASS1 0x80               /* only print this error on pass one */
  79.  
  80. /*
  81.  * These codes define specific types of suppressible warning.
  82.  */
  83. #define ERR_WARN_MNP  0x0100           /* macro-num-parameters warning */
  84. #define ERR_WARN_OL   0x0200           /* orphan label (no colon, and
  85.                     * alone on line) */
  86. #define ERR_WARN_NOV  0x0300           /* numeric overflow */
  87. #define ERR_WARN_MASK 0xFF00           /* the mask for this feature */
  88. #define ERR_WARN_SHR  8               /* how far to shift right */
  89. #define ERR_WARN_MAX  3               /* the highest numbered one */
  90.  
  91. /*
  92.  * -----------------------
  93.  * Other function typedefs
  94.  * -----------------------
  95.  */
  96.  
  97. /*
  98.  * A label-lookup function should look like this.
  99.  */
  100. typedef int (*lfunc) (char *label, long *segment, long *offset);
  101.  
  102. /*
  103.  * And a label-definition function like this. The boolean parameter
  104.  * `is_norm' states whether the label is a `normal' label (which
  105.  * should affect the local-label system), or something odder like
  106.  * an EQU or a segment-base symbol, which shouldn't.
  107.  */
  108. typedef void (*ldfunc) (char *label, long segment, long offset, char *special,
  109.             int is_norm, int isextrn, struct ofmt *ofmt,
  110.             efunc error);
  111.  
  112. /*
  113.  * List-file generators should look like this:
  114.  */
  115. typedef struct {
  116.     /*
  117.      * Called to initialise the listing file generator. Before this
  118.      * is called, the other routines will silently do nothing when
  119.      * called. The `char *' parameter is the file name to write the
  120.      * listing to.
  121.      */
  122.     void (*init) (char *, efunc);
  123.  
  124.     /*
  125.      * Called to clear stuff up and close the listing file.
  126.      */
  127.     void (*cleanup) (void);
  128.  
  129.     /*
  130.      * Called to output binary data. Parameters are: the offset;
  131.      * the data; the data type. Data types are similar to the
  132.      * output-format interface, only OUT_ADDRESS will _always_ be
  133.      * displayed as if it's relocatable, so ensure that any non-
  134.      * relocatable address has been converted to OUT_RAWDATA by
  135.      * then. Note that OUT_RAWDATA+0 is a valid data type, and is a
  136.      * dummy call used to give the listing generator an offset to
  137.      * work with when doing things like uplevel(LIST_TIMES) or
  138.      * uplevel(LIST_INCBIN).
  139.      */
  140.     void (*output) (long, void *, unsigned long);
  141.  
  142.     /*
  143.      * Called to send a text line to the listing generator. The
  144.      * `int' parameter is LIST_READ or LIST_MACRO depending on
  145.      * whether the line came directly from an input file or is the
  146.      * result of a multi-line macro expansion.
  147.      */
  148.     void (*line) (int, char *);
  149.  
  150.     /*
  151.      * Called to change one of the various levelled mechanisms in
  152.      * the listing generator. LIST_INCLUDE and LIST_MACRO can be
  153.      * used to increase the nesting level of include files and
  154.      * macro expansions; LIST_TIMES and LIST_INCBIN switch on the
  155.      * two binary-output-suppression mechanisms for large-scale
  156.      * pseudo-instructions.
  157.      *
  158.      * LIST_MACRO_NOLIST is synonymous with LIST_MACRO except that
  159.      * it indicates the beginning of the expansion of a `nolist'
  160.      * macro, so anything under that level won't be expanded unless
  161.      * it includes another file.
  162.      */
  163.     void (*uplevel) (int);
  164.  
  165.     /*
  166.      * Reverse the effects of uplevel.
  167.      */
  168.     void (*downlevel) (int);
  169. } ListGen;
  170.  
  171. /*
  172.  * The expression evaluator must be passed a scanner function; a
  173.  * standard scanner is provided as part of nasmlib.c. The
  174.  * preprocessor will use a different one. Scanners, and the
  175.  * token-value structures they return, look like this.
  176.  *
  177.  * The return value from the scanner is always a copy of the
  178.  * `t_type' field in the structure.
  179.  */
  180. struct tokenval {
  181.     int t_type;
  182.     long t_integer, t_inttwo;
  183.     char *t_charptr;
  184. };
  185. typedef int (*scanner) (void *private_data, struct tokenval *tv);
  186.  
  187. /*
  188.  * Token types returned by the scanner, in addition to ordinary
  189.  * ASCII character values, and zero for end-of-string.
  190.  */
  191. enum {                       /* token types, other than chars */
  192.     TOKEN_INVALID = -1,               /* a placeholder value */
  193.     TOKEN_EOS = 0,               /* end of string */
  194.     TOKEN_EQ = '=', TOKEN_GT = '>', TOKEN_LT = '<',   /* aliases */
  195.     TOKEN_ID = 256, TOKEN_NUM, TOKEN_REG, TOKEN_INSN,  /* major token types */
  196.     TOKEN_ERRNUM,               /* numeric constant with error in */
  197.     TOKEN_HERE, TOKEN_BASE,           /* $ and $$ */
  198.     TOKEN_SPECIAL,               /* BYTE, WORD, DWORD, FAR, NEAR, etc */
  199.     TOKEN_PREFIX,               /* A32, O16, LOCK, REPNZ, TIMES, etc */
  200.     TOKEN_SHL, TOKEN_SHR,           /* << and >> */
  201.     TOKEN_SDIV, TOKEN_SMOD,           /* // and %% */
  202.     TOKEN_GE, TOKEN_LE, TOKEN_NE,      /* >=, <= and <> (!= is same as <>) */
  203.     TOKEN_DBL_AND, TOKEN_DBL_OR, TOKEN_DBL_XOR,   /* &&, || and ^^ */
  204.     TOKEN_SEG, TOKEN_WRT,           /* SEG and WRT */
  205.     TOKEN_FLOAT                   /* floating-point constant */
  206. };
  207.  
  208. /*
  209.  * Expression-evaluator datatype. Expressions, within the
  210.  * evaluator, are stored as an array of these beasts, terminated by
  211.  * a record with type==0. Mostly, it's a vector type: each type
  212.  * denotes some kind of a component, and the value denotes the
  213.  * multiple of that component present in the expression. The
  214.  * exception is the WRT type, whose `value' field denotes the
  215.  * segment to which the expression is relative. These segments will
  216.  * be segment-base types, i.e. either odd segment values or SEG_ABS
  217.  * types. So it is still valid to assume that anything with a
  218.  * `value' field of zero is insignificant.
  219.  */
  220. typedef struct {
  221.     long type;                   /* a register, or EXPR_xxx */
  222.     long value;                   /* must be >= 32 bits */
  223. } expr;
  224.  
  225. /*
  226.  * The evaluator can also return hints about which of two registers
  227.  * used in an expression should be the base register. See also the
  228.  * `operand' structure.
  229.  */
  230. struct eval_hints {
  231.     int base;
  232.     int type;
  233. };
  234.  
  235. /*
  236.  * The actual expression evaluator function looks like this. When
  237.  * called, it expects the first token of its expression to already
  238.  * be in `*tv'; if it is not, set tv->t_type to TOKEN_INVALID and
  239.  * it will start by calling the scanner.
  240.  *
  241.  * If a forward reference happens during evaluation, the evaluator
  242.  * must set `*fwref' to TRUE if `fwref' is non-NULL.
  243.  *
  244.  * `critical' is non-zero if the expression may not contain forward
  245.  * references. The evaluator will report its own error if this
  246.  * occurs; if `critical' is 1, the error will be "symbol not
  247.  * defined before use", whereas if `critical' is 2, the error will
  248.  * be "symbol undefined".
  249.  *
  250.  * If `critical' has bit 4 set (in addition to its main value: 0x11
  251.  * and 0x12 correspond to 1 and 2) then an extended expression
  252.  * syntax is recognised, in which relational operators such as =, <
  253.  * and >= are accepted, as well as low-precedence logical operators
  254.  * &&, ^^ and ||.
  255.  *
  256.  * If `hints' is non-NULL, it gets filled in with some hints as to
  257.  * the base register in complex effective addresses.
  258.  */
  259. typedef expr *(*evalfunc) (scanner sc, void *scprivate, struct tokenval *tv,
  260.                int *fwref, int critical, efunc error,
  261.                struct eval_hints *hints);
  262.  
  263. /*
  264.  * There's also an auxiliary routine through which the evaluator
  265.  * needs to hear about the value of $ and the label (if any)
  266.  * defined on the current line.
  267.  */
  268. typedef void (*evalinfofunc) (char *labelname, long segment, long offset);
  269.  
  270. /*
  271.  * Special values for expr->type. ASSUMPTION MADE HERE: the number
  272.  * of distinct register names (i.e. possible "type" fields for an
  273.  * expr structure) does not exceed 124 (EXPR_REG_START through
  274.  * EXPR_REG_END).
  275.  */
  276. #define EXPR_REG_START 1
  277. #define EXPR_REG_END 124
  278. #define EXPR_UNKNOWN 125L           /* for forward references */
  279. #define EXPR_SIMPLE 126L
  280. #define EXPR_WRT 127L
  281. #define EXPR_SEGBASE 128L
  282.  
  283. /*
  284.  * Preprocessors ought to look like this:
  285.  */
  286. typedef struct {
  287.     /*
  288.      * Called at the start of a pass; given a file name, the number
  289.      * of the pass, an error reporting function, an evaluator
  290.      * function, and a listing generator to talk to.
  291.      */
  292.     void (*reset) (char *, int, efunc, evalfunc, ListGen *);
  293.  
  294.     /*
  295.      * Called to fetch a line of preprocessed source. The line
  296.      * returned has been malloc'ed, and so should be freed after
  297.      * use.
  298.      */
  299.     char *(*getline) (void);
  300.  
  301.     /*
  302.      * Called at the end of a pass.
  303.      */
  304.     void (*cleanup) (void);
  305. } Preproc;
  306.  
  307. /*
  308.  * ----------------------------------------------------------------
  309.  * Some lexical properties of the NASM source language, included
  310.  * here because they are shared between the parser and preprocessor
  311.  * ----------------------------------------------------------------
  312.  */
  313.  
  314. /* isidstart matches any character that may start an identifier, and isidchar
  315.  * matches any character that may appear at places other than the start of an
  316.  * identifier. E.g. a period may only appear at the start of an identifier
  317.  * (for local labels), whereas a number may appear anywhere *but* at the
  318.  * start. */
  319.  
  320. #define isidstart(c) ( isalpha(c) || (c)=='_' || (c)=='.' || (c)=='?' \
  321.                                   || (c)=='@' )
  322. #define isidchar(c)  ( isidstart(c) || isdigit(c) || (c)=='$' || (c)=='#' \
  323.                                                   || (c)=='~' )
  324.  
  325. /* Ditto for numeric constants. */
  326.  
  327. #define isnumstart(c)  ( isdigit(c) || (c)=='$' )
  328. #define isnumchar(c)   ( isalnum(c) )
  329.  
  330. /* This returns the numeric value of a given 'digit'. */
  331.  
  332. #define numvalue(c)  ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
  333.  
  334. /*
  335.  * Data-type flags that get passed to listing-file routines.
  336.  */
  337. enum {
  338.     LIST_READ, LIST_MACRO, LIST_MACRO_NOLIST, LIST_INCLUDE,
  339.     LIST_INCBIN, LIST_TIMES
  340. };
  341.  
  342. /*
  343.  * -----------------------------------------------------------
  344.  * Format of the `insn' structure returned from `parser.c' and
  345.  * passed into `assemble.c'
  346.  * -----------------------------------------------------------
  347.  */
  348.  
  349. /*
  350.  * Here we define the operand types. These are implemented as bit
  351.  * masks, since some are subsets of others; e.g. AX in a MOV
  352.  * instruction is a special operand type, whereas AX in other
  353.  * contexts is just another 16-bit register. (Also, consider CL in
  354.  * shift instructions, DX in OUT, etc.)
  355.  */
  356.  
  357. /* size, and other attributes, of the operand */
  358. #define BITS8     0x00000001L
  359. #define BITS16    0x00000002L
  360. #define BITS32    0x00000004L
  361. #define BITS64    0x00000008L           /* FPU only */
  362. #define BITS80    0x00000010L           /* FPU only */
  363. #define FAR       0x00000020L           /* grotty: this means 16:16 or */
  364.                        /* 16:32, like in CALL/JMP */
  365. #define NEAR      0x00000040L
  366. #define SHORT     0x00000080L           /* and this means what it says :) */
  367.  
  368. #define SIZE_MASK 0x000000FFL           /* all the size attributes */
  369. #define NON_SIZE  (~SIZE_MASK)
  370.  
  371. #define TO        0x00000100L          /* reverse effect in FADD, FSUB &c */
  372. #define COLON     0x00000200L           /* operand is followed by a colon */
  373.  
  374. /* type of operand: memory reference, register, etc. */
  375. #define MEMORY    0x00204000L
  376. #define REGISTER  0x00001000L           /* register number in 'basereg' */
  377. #define IMMEDIATE 0x00002000L
  378.  
  379. #define REGMEM    0x00200000L           /* for r/m, ie EA, operands */
  380. #define REGNORM   0x00201000L           /* 'normal' reg, qualifies as EA */
  381. #define REG8      0x00201001L
  382. #define REG16     0x00201002L
  383. #define REG32     0x00201004L
  384. #define MMXREG    0x00201008L           /* MMX registers */
  385. #define FPUREG    0x01000000L           /* floating point stack registers */
  386. #define FPU0      0x01000800L           /* FPU stack register zero */
  387.  
  388. /* special register operands: these may be treated differently */
  389. #define REG_SMASK 0x00070000L           /* a mask for the following */
  390. #define REG_ACCUM 0x00211000L           /* accumulator: AL, AX or EAX */
  391. #define REG_AL    0x00211001L           /* REG_ACCUM | BITSxx */
  392. #define REG_AX    0x00211002L           /* ditto */
  393. #define REG_EAX   0x00211004L           /* and again */
  394. #define REG_COUNT 0x00221000L           /* counter: CL, CX or ECX */
  395. #define REG_CL    0x00221001L           /* REG_COUNT | BITSxx */
  396. #define REG_CX    0x00221002L           /* ditto */
  397. #define REG_ECX   0x00221004L           /* another one */
  398. #define REG_DX    0x00241002L
  399. #define REG_SREG  0x00081002L           /* any segment register */
  400. #define REG_CS    0x01081002L           /* CS */
  401. #define REG_DESS  0x02081002L           /* DS, ES, SS (non-CS 86 registers) */
  402. #define REG_FSGS  0x04081002L           /* FS, GS (386 extended registers) */
  403. #define REG_CDT   0x00101004L           /* CRn, DRn and TRn */
  404. #define REG_CREG  0x08101004L           /* CRn */
  405. #define REG_CR4   0x08101404L           /* CR4 (Pentium only) */
  406. #define REG_DREG  0x10101004L           /* DRn */
  407. #define REG_TREG  0x20101004L           /* TRn */
  408.  
  409. /* special type of EA */
  410. #define MEM_OFFS  0x00604000L           /* simple [address] offset */
  411.  
  412. /* special type of immediate operand */
  413. #define ONENESS   0x00800000L          /* so UNITY == IMMEDIATE | ONENESS */
  414. #define UNITY     0x00802000L           /* for shift/rotate instructions */
  415.  
  416. /*
  417.  * Next, the codes returned from the parser, for registers and
  418.  * instructions.
  419.  */
  420.  
  421. enum {                       /* register names */
  422.     R_AH = EXPR_REG_START, R_AL, R_AX, R_BH, R_BL, R_BP, R_BX, R_CH,
  423.     R_CL, R_CR0, R_CR2, R_CR3, R_CR4, R_CS, R_CX, R_DH, R_DI, R_DL,
  424.     R_DR0, R_DR1, R_DR2, R_DR3, R_DR6, R_DR7, R_DS, R_DX, R_EAX,
  425.     R_EBP, R_EBX, R_ECX, R_EDI, R_EDX, R_ES, R_ESI, R_ESP, R_FS,
  426.     R_GS, R_MM0, R_MM1, R_MM2, R_MM3, R_MM4, R_MM5, R_MM6, R_MM7,
  427.     R_SI, R_SP, R_SS, R_ST0, R_ST1, R_ST2, R_ST3, R_ST4, R_ST5,
  428.     R_ST6, R_ST7, R_TR3, R_TR4, R_TR5, R_TR6, R_TR7, REG_ENUM_LIMIT
  429. };
  430.  
  431. enum {                       /* instruction names */
  432.     I_AAA, I_AAD, I_AAM, I_AAS, I_ADC, I_ADD, I_AND, I_ARPL,
  433.     I_BOUND, I_BSF, I_BSR, I_BSWAP, I_BT, I_BTC, I_BTR, I_BTS,
  434.     I_CALL, I_CBW, I_CDQ, I_CLC, I_CLD, I_CLI, I_CLTS, I_CMC, I_CMP,
  435.     I_CMPSB, I_CMPSD, I_CMPSW, I_CMPXCHG, I_CMPXCHG486, I_CMPXCHG8B,
  436.     I_CPUID, I_CWD, I_CWDE, I_DAA, I_DAS, I_DB, I_DD, I_DEC, I_DIV,
  437.     I_DQ, I_DT, I_DW, I_EMMS, I_ENTER, I_EQU, I_F2XM1, I_FABS,
  438.     I_FADD, I_FADDP, I_FBLD, I_FBSTP, I_FCHS, I_FCLEX, I_FCMOVB,
  439.     I_FCMOVBE, I_FCMOVE, I_FCMOVNB, I_FCMOVNBE, I_FCMOVNE,
  440.     I_FCMOVNU, I_FCMOVU, I_FCOM, I_FCOMI, I_FCOMIP, I_FCOMP,
  441.     I_FCOMPP, I_FCOS, I_FDECSTP, I_FDISI, I_FDIV, I_FDIVP, I_FDIVR,
  442.     I_FDIVRP, I_FENI, I_FFREE, I_FIADD, I_FICOM, I_FICOMP, I_FIDIV,
  443.     I_FIDIVR, I_FILD, I_FIMUL, I_FINCSTP, I_FINIT, I_FIST, I_FISTP,
  444.     I_FISUB, I_FISUBR, I_FLD, I_FLD1, I_FLDCW, I_FLDENV, I_FLDL2E,
  445.     I_FLDL2T, I_FLDLG2, I_FLDLN2, I_FLDPI, I_FLDZ, I_FMUL, I_FMULP,
  446.     I_FNCLEX, I_FNDISI, I_FNENI, I_FNINIT, I_FNOP, I_FNSAVE,
  447.     I_FNSTCW, I_FNSTENV, I_FNSTSW, I_FPATAN, I_FPREM, I_FPREM1,
  448.     I_FPTAN, I_FRNDINT, I_FRSTOR, I_FSAVE, I_FSCALE, I_FSETPM,
  449.     I_FSIN, I_FSINCOS, I_FSQRT, I_FST, I_FSTCW, I_FSTENV, I_FSTP,
  450.     I_FSTSW, I_FSUB, I_FSUBP, I_FSUBR, I_FSUBRP, I_FTST, I_FUCOM,
  451.     I_FUCOMI, I_FUCOMIP, I_FUCOMP, I_FUCOMPP, I_FXAM, I_FXCH,
  452.     I_FXTRACT, I_FYL2X, I_FYL2XP1, I_HLT, I_IBTS, I_ICEBP, I_IDIV,
  453.     I_IMUL, I_IN, I_INC, I_INCBIN, I_INSB, I_INSD, I_INSW, I_INT,
  454.     I_INT1, I_INT01, I_INT3, I_INTO, I_INVD, I_INVLPG, I_IRET,
  455.     I_IRETD, I_IRETW, I_JCXZ, I_JECXZ, I_JMP, I_LAHF, I_LAR, I_LDS,
  456.     I_LEA, I_LEAVE, I_LES, I_LFS, I_LGDT, I_LGS, I_LIDT, I_LLDT,
  457.     I_LMSW, I_LOADALL, I_LOADALL286, I_LODSB, I_LODSD, I_LODSW,
  458.     I_LOOP, I_LOOPE, I_LOOPNE, I_LOOPNZ, I_LOOPZ, I_LSL, I_LSS,
  459.     I_LTR, I_MOV, I_MOVD, I_MOVQ, I_MOVSB, I_MOVSD, I_MOVSW,
  460.     I_MOVSX, I_MOVZX, I_MUL, I_NEG, I_NOP, I_NOT, I_OR, I_OUT,
  461.     I_OUTSB, I_OUTSD, I_OUTSW, I_PACKSSDW, I_PACKSSWB, I_PACKUSWB,
  462.     I_PADDB, I_PADDD, I_PADDSB, I_PADDSIW, I_PADDSW, I_PADDUSB,
  463.     I_PADDUSW, I_PADDW, I_PAND, I_PANDN, I_PAVEB, I_PCMPEQB,
  464.     I_PCMPEQD, I_PCMPEQW, I_PCMPGTB, I_PCMPGTD, I_PCMPGTW,
  465.     I_PDISTIB, I_PMACHRIW, I_PMADDWD, I_PMAGW, I_PMULHRW,
  466.     I_PMULHRIW, I_PMULHW, I_PMULLW, I_PMVGEZB, I_PMVLZB, I_PMVNZB,
  467.     I_PMVZB, I_POP, I_POPA, I_POPAD, I_POPAW, I_POPF, I_POPFD,
  468.     I_POPFW, I_POR, I_PSLLD, I_PSLLQ, I_PSLLW, I_PSRAD, I_PSRAW,
  469.     I_PSRLD, I_PSRLQ, I_PSRLW, I_PSUBB, I_PSUBD, I_PSUBSB,
  470.     I_PSUBSIW, I_PSUBSW, I_PSUBUSB, I_PSUBUSW, I_PSUBW, I_PUNPCKHBW,
  471.     I_PUNPCKHDQ, I_PUNPCKHWD, I_PUNPCKLBW, I_PUNPCKLDQ, I_PUNPCKLWD,
  472.     I_PUSH, I_PUSHA, I_PUSHAD, I_PUSHAW, I_PUSHF, I_PUSHFD,
  473.     I_PUSHFW, I_PXOR, I_RCL, I_RCR, I_RDMSR, I_RDPMC, I_RDTSC,
  474.     I_RESB, I_RESD, I_RESQ, I_REST, I_RESW, I_RET, I_RETF, I_RETN,
  475.     I_ROL, I_ROR, I_RSM, I_SAHF, I_SAL, I_SALC, I_SAR, I_SBB,
  476.     I_SCASB, I_SCASD, I_SCASW, I_SGDT, I_SHL, I_SHLD, I_SHR, I_SHRD,
  477.     I_SIDT, I_SLDT, I_SMI, I_SMSW, I_STC, I_STD, I_STI, I_STOSB,
  478.     I_STOSD, I_STOSW, I_STR, I_SUB, I_TEST, I_UMOV, I_VERR, I_VERW,
  479.     I_WAIT, I_WBINVD, I_WRMSR, I_XADD, I_XBTS, I_XCHG, I_XLATB,
  480.     I_XOR, I_CMOVcc, I_Jcc, I_SETcc
  481. };
  482.  
  483. enum {                       /* condition code names */
  484.     C_A, C_AE, C_B, C_BE, C_C, C_E, C_G, C_GE, C_L, C_LE, C_NA, C_NAE,
  485.     C_NB, C_NBE, C_NC, C_NE, C_NG, C_NGE, C_NL, C_NLE, C_NO, C_NP,
  486.     C_NS, C_NZ, C_O, C_P, C_PE, C_PO, C_S, C_Z
  487. };
  488.  
  489. /*
  490.  * Note that because segment registers may be used as instruction
  491.  * prefixes, we must ensure the enumerations for prefixes and
  492.  * register names do not overlap.
  493.  */
  494. enum {                       /* instruction prefixes */
  495.     PREFIX_ENUM_START = REG_ENUM_LIMIT,
  496.     P_A16 = PREFIX_ENUM_START, P_A32, P_LOCK, P_O16, P_O32, P_REP, P_REPE,
  497.     P_REPNE, P_REPNZ, P_REPZ, P_TIMES
  498. };
  499.  
  500. enum {                       /* extended operand types */
  501.     EOT_NOTHING, EOT_DB_STRING, EOT_DB_NUMBER
  502. };
  503.  
  504. enum {                       /* special EA flags */
  505.     EAF_BYTEOFFS = 1,               /* force offset part to byte size */
  506.     EAF_WORDOFFS = 2,               /* force offset part to [d]word size */
  507.     EAF_TIMESTWO = 4               /* really do EAX*2 not EAX+EAX */
  508. };
  509.  
  510. enum {                       /* values for `hinttype' */
  511.     EAH_NOHINT = 0,               /* no hint at all - our discretion */
  512.     EAH_MAKEBASE = 1,               /* try to make given reg the base */
  513.     EAH_NOTBASE = 2               /* try _not_ to make reg the base */
  514. };
  515.  
  516. typedef struct {               /* operand to an instruction */
  517.     long type;                   /* type of operand */
  518.     int addr_size;               /* 0 means default; 16; 32 */
  519.     int basereg, indexreg, scale;      /* registers and scale involved */
  520.     int hintbase, hinttype;           /* hint as to real base register */
  521.     long segment;               /* immediate segment, if needed */
  522.     long offset;               /* any immediate number */
  523.     long wrt;                   /* segment base it's relative to */
  524.     int eaflags;               /* special EA flags */
  525. } operand;
  526.  
  527. typedef struct extop {               /* extended operand */
  528.     struct extop *next;               /* linked list */
  529.     long type;                   /* defined above */
  530.     char *stringval;               /* if it's a string, then here it is */
  531.     int stringlen;               /* ... and here's how long it is */
  532.     long segment;               /* if it's a number/address, then... */
  533.     long offset;               /* ... it's given here ... */
  534.     long wrt;                   /* ... and here */
  535. } extop;
  536.  
  537. #define MAXPREFIX 4
  538.  
  539. typedef struct {               /* an instruction itself */
  540.     char *label;               /* the label defined, or NULL */
  541.     int prefixes[MAXPREFIX];           /* instruction prefixes, if any */
  542.     int nprefix;               /* number of entries in above */
  543.     int opcode;                   /* the opcode - not just the string */
  544.     int condition;               /* the condition code, if Jcc/SETcc */
  545.     int operands;               /* how many operands? 0-3 */
  546.     operand oprs[3];                  /* the operands, defined as above */
  547.     extop *eops;               /* extended operands */
  548.     long times;                   /* repeat count (TIMES prefix) */
  549.     int forw_ref;               /* is there a forward reference? */
  550. } insn;
  551.  
  552. /*
  553.  * ------------------------------------------------------------
  554.  * The data structure defining an output format driver, and the
  555.  * interfaces to the functions therein.
  556.  * ------------------------------------------------------------
  557.  */
  558.  
  559. struct ofmt {
  560.     /*
  561.      * This is a short (one-liner) description of the type of
  562.      * output generated by the driver.
  563.      */
  564.     char *fullname;
  565.  
  566.     /*
  567.      * This is a single keyword used to select the driver.
  568.      */
  569.     char *shortname;
  570.  
  571.     /*
  572.      * This, if non-NULL, is a NULL-terminated list of `char *'s
  573.      * pointing to extra standard macros supplied by the object
  574.      * format (e.g. a sensible initial default value of __SECT__,
  575.      * and user-level equivalents for any format-specific
  576.      * directives).
  577.      */
  578.     char **stdmac;
  579.  
  580.     /*
  581.      * This procedure is called at the start of an output session.
  582.      * It tells the output format what file it will be writing to,
  583.      * what routine to report errors through, and how to interface
  584.      * to the label manager and expression evaluator if necessary.
  585.      * It also gives it a chance to do other initialisation.
  586.      */
  587.     void (*init) (FILE *fp, efunc error, ldfunc ldef, evalfunc eval);
  588.  
  589.     /*
  590.      * This procedure is called by assemble() to write actual
  591.      * generated code or data to the object file. Typically it
  592.      * doesn't have to actually _write_ it, just store it for
  593.      * later.
  594.      *
  595.      * The `type' argument specifies the type of output data, and
  596.      * usually the size as well: its contents are described below.
  597.      */
  598.     void (*output) (long segto, void *data, unsigned long type,
  599.             long segment, long wrt);
  600.  
  601.     /*
  602.      * This procedure is called once for every symbol defined in
  603.      * the module being assembled. It gives the name and value of
  604.      * the symbol, in NASM's terms, and indicates whether it has
  605.      * been declared to be global. Note that the parameter "name",
  606.      * when passed, will point to a piece of static storage
  607.      * allocated inside the label manager - it's safe to keep using
  608.      * that pointer, because the label manager doesn't clean up
  609.      * until after the output driver has.
  610.      *
  611.      * Values of `is_global' are: 0 means the symbol is local; 1
  612.      * means the symbol is global; 2 means the symbol is common (in
  613.      * which case `offset' holds the _size_ of the variable).
  614.      * Anything else is available for the output driver to use
  615.      * internally.
  616.      *
  617.      * This routine explicitly _is_ allowed to call the label
  618.      * manager to define further symbols, if it wants to, even
  619.      * though it's been called _from_ the label manager. That much
  620.      * re-entrancy is guaranteed in the label manager. However, the
  621.      * label manager will in turn call this routine, so it should
  622.      * be prepared to be re-entrant itself.
  623.      *
  624.      * The `special' parameter contains special information passed
  625.      * through from the command that defined the label: it may have
  626.      * been an EXTERN, a COMMON or a GLOBAL. The distinction should
  627.      * be obvious to the output format from the other parameters.
  628.      */
  629.     void (*symdef) (char *name, long segment, long offset, int is_global,
  630.             char *special);
  631.  
  632.     /*
  633.      * This procedure is called when the source code requests a
  634.      * segment change. It should return the corresponding segment
  635.      * _number_ for the name, or NO_SEG if the name is not a valid
  636.      * segment name.
  637.      *
  638.      * It may also be called with NULL, in which case it is to
  639.      * return the _default_ section number for starting assembly in.
  640.      *
  641.      * It is allowed to modify the string it is given a pointer to.
  642.      *
  643.      * It is also allowed to specify a default instruction size for
  644.      * the segment, by setting `*bits' to 16 or 32. Or, if it
  645.      * doesn't wish to define a default, it can leave `bits' alone.
  646.      */
  647.     long (*section) (char *name, int pass, int *bits);
  648.  
  649.     /*
  650.      * This procedure is called to modify the segment base values
  651.      * returned from the SEG operator. It is given a segment base
  652.      * value (i.e. a segment value with the low bit set), and is
  653.      * required to produce in return a segment value which may be
  654.      * different. It can map segment bases to absolute numbers by
  655.      * means of returning SEG_ABS types.
  656.      *
  657.      * It should return NO_SEG if the segment base cannot be
  658.      * determined; the evaluator (which calls this routine) is
  659.      * responsible for throwing an error condition if that occurs
  660.      * in pass two or in a critical expression.
  661.      */
  662.     long (*segbase) (long segment);
  663.  
  664.     /*
  665.      * This procedure is called to allow the output driver to
  666.      * process its own specific directives. When called, it has the
  667.      * directive word in `directive' and the parameter string in
  668.      * `value'. It is called in both assembly passes, and `pass'
  669.      * will be either 1 or 2.
  670.      *
  671.      * This procedure should return zero if it does not _recognise_
  672.      * the directive, so that the main program can report an error.
  673.      * If it recognises the directive but then has its own errors,
  674.      * it should report them itself and then return non-zero. It
  675.      * should also return non-zero if it correctly processes the
  676.      * directive.
  677.      */
  678.     int (*directive) (char *directive, char *value, int pass);
  679.  
  680.     /*
  681.      * This procedure is called before anything else - even before
  682.      * the "init" routine - and is passed the name of the input
  683.      * file from which this output file is being generated. It
  684.      * should return its preferred name for the output file in
  685.      * `outname', if outname[0] is not '\0', and do nothing to
  686.      * `outname' otherwise. Since it is called before the driver is
  687.      * properly initialised, it has to be passed its error handler
  688.      * separately.
  689.      *
  690.      * This procedure may also take its own copy of the input file
  691.      * name for use in writing the output file: it is _guaranteed_
  692.      * that it will be called before the "init" routine.
  693.      *
  694.      * The parameter `outname' points to an area of storage
  695.      * guaranteed to be at least FILENAME_MAX in size.
  696.      */
  697.     void (*filename) (char *inname, char *outname, efunc error);
  698.  
  699.     /*
  700.      * This procedure is called after assembly finishes, to allow
  701.      * the output driver to clean itself up and free its memory.
  702.      * Typically, it will also be the point at which the object
  703.      * file actually gets _written_.
  704.      *
  705.      * One thing the cleanup routine should always do is to close
  706.      * the output file pointer.
  707.      */
  708.     void (*cleanup) (void);
  709. };
  710.  
  711. /*
  712.  * values for the `type' parameter to an output function. Each one
  713.  * must have the actual number of _bytes_ added to it.
  714.  *
  715.  * Exceptions are OUT_RELxADR, which denote an x-byte relocation
  716.  * which will be a relative jump. For this we need to know the
  717.  * distance in bytes from the start of the relocated record until
  718.  * the end of the containing instruction. _This_ is what is stored
  719.  * in the size part of the parameter, in this case.
  720.  *
  721.  * Also OUT_RESERVE denotes reservation of N bytes of BSS space,
  722.  * and the contents of the "data" parameter is irrelevant.
  723.  *
  724.  * The "data" parameter for the output function points to a "long",
  725.  * containing the address in question, unless the type is
  726.  * OUT_RAWDATA, in which case it points to an "unsigned char"
  727.  * array.
  728.  */
  729. #define OUT_RAWDATA 0x00000000UL
  730. #define OUT_ADDRESS 0x10000000UL
  731. #define OUT_REL2ADR 0x20000000UL
  732. #define OUT_REL4ADR 0x30000000UL
  733. #define OUT_RESERVE 0x40000000UL
  734. #define OUT_TYPMASK 0xF0000000UL
  735. #define OUT_SIZMASK 0x0FFFFFFFUL
  736.  
  737. /*
  738.  * -----
  739.  * Other
  740.  * -----
  741.  */
  742.  
  743. /*
  744.  * This is a useful #define which I keep meaning to use more often:
  745.  * the number of elements of a statically defined array.
  746.  */
  747.  
  748. #define elements(x)     ( sizeof(x) / sizeof(*(x)) )
  749.  
  750. #endif
  751.