• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GAS-compatible parser header file
3  *
4  *  Copyright (C) 2005-2007  Peter Johnson
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the author nor the names of other contributors
15  *    may be used to endorse or promote products derived from this
16  *    software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 #ifndef YASM_GAS_PARSER_H
31 #define YASM_GAS_PARSER_H
32 
33 #define YYCTYPE         unsigned char
34 
35 #define MAX_SAVED_LINE_LEN  80
36 
37 enum tokentype {
38     INTNUM = 258,
39     FLTNUM,
40     STRING,
41     REG,
42     REGGROUP,
43     SEGREG,
44     TARGETMOD,
45     LEFT_OP,
46     RIGHT_OP,
47     ID,
48     LABEL,
49     CPP_LINE_MARKER,
50     NASM_LINE_MARKER,
51     NONE
52 };
53 
54 typedef union {
55     unsigned int int_info;
56     yasm_intnum *intn;
57     yasm_floatnum *flt;
58     yasm_bytecode *bc;
59     uintptr_t arch_data;
60     struct {
61         char *contents;
62         size_t len;
63     } str;
64 } yystype;
65 #define YYSTYPE yystype
66 
67 enum gas_parser_state {
68     INITIAL,
69     COMMENT,
70     SECTION_DIRECTIVE,
71     NASM_FILENAME
72 };
73 
74 typedef struct yasm_parser_gas {
75     /*@only@*/ yasm_object *object;
76 
77     /* last "base" label for local (.) labels */
78     /*@null@*/ char *locallabel_base;
79     size_t locallabel_base_len;
80 
81     /* .line/.file: we have to see both to start setting linemap versions */
82     int dir_fileline;
83     /*@null@*/ char *dir_file;
84     unsigned long dir_line;
85 
86     /* Have we seen a line marker? */
87     int seen_line_marker;
88 
89     /*@dependent@*/ yasm_preproc *preproc;
90     /*@dependent@*/ yasm_errwarns *errwarns;
91 
92     /*@dependent@*/ yasm_linemap *linemap;
93 
94     /*@null@*/ yasm_bytecode *prev_bc;
95     yasm_bytecode *temp_bc;
96 
97     int save_input;
98     YYCTYPE save_line[2][MAX_SAVED_LINE_LEN];
99     int save_last;
100 
101     /* Line data storage used in preproc_input(). */
102     char *line, *linepos;
103     size_t lineleft;
104 
105     yasm_scanner s;
106     enum gas_parser_state state;
107 
108     int token;          /* enum tokentype or any character */
109     yystype tokval;
110     char tokch;         /* first character of token */
111 
112     /* one token of lookahead; used sparingly */
113     int peek_token;     /* NONE if none */
114     yystype peek_tokval;
115     char peek_tokch;
116 
117     /* Index of local labels; what's stored here is the /next/ index,
118      * so these are all 0 at start.
119      */
120     unsigned long local[10];
121 
122     /* Parser-handled directives HAMT lookup */
123     HAMT *dirs;
124 
125     int intel_syntax;
126 
127     int is_nasm_preproc;
128     int is_cpp_preproc;
129 } yasm_parser_gas;
130 
131 /* shorter access names to commonly used parser_gas fields */
132 #define p_object        (parser_gas->object)
133 #define p_symtab        (parser_gas->object->symtab)
134 #define cursect         (parser_gas->object->cur_section)
135 #define curtok          (parser_gas->token)
136 #define curval          (parser_gas->tokval)
137 
138 #define INTNUM_val              (curval.intn)
139 #define FLTNUM_val              (curval.flt)
140 #define STRING_val              (curval.str)
141 #define REG_val                 (curval.arch_data)
142 #define REGGROUP_val            (curval.arch_data)
143 #define SEGREG_val              (curval.arch_data)
144 #define TARGETMOD_val           (curval.arch_data)
145 #define ID_val                  (curval.str.contents)
146 #define ID_len                  (curval.str.len)
147 #define LABEL_val               (curval.str.contents)
148 #define LABEL_len               (curval.str.len)
149 
150 #define cur_line        (yasm_linemap_get_current(parser_gas->linemap))
151 
152 #define p_expr_new(l,o,r)       yasm_expr_create(o,l,r,cur_line)
153 #define p_expr_new_tree(l,o,r)  yasm_expr_create_tree(l,o,r,cur_line)
154 #define p_expr_new_branch(o,r)  yasm_expr_create_branch(o,r,cur_line)
155 #define p_expr_new_ident(r)     yasm_expr_create_ident(r,cur_line)
156 
157 yasm_bytecode *parse_instr_intel(yasm_parser_gas *parser_gas);
158 
159 void gas_parser_parse(yasm_parser_gas *parser_gas);
160 void gas_parser_cleanup(yasm_parser_gas *parser_gas);
161 int gas_parser_lex(YYSTYPE *lvalp, yasm_parser_gas *parser_gas);
162 
163 #endif
164