1d65182f1818d1c19e6f3866ab6e68a262fad5185hbono@chromium.org/*
245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * GAS-compatible parser header file
345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *  Copyright (C) 2005-2007  Peter Johnson
545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Redistribution and use in source and binary forms, with or without
745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * modification, are permitted provided that the following conditions
845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * are met:
945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 1. Redistributions of source code must retain the above copyright
1045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    notice, this list of conditions and the following disclaimer.
1145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 2. Redistributions in binary form must reproduce the above copyright
1245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    notice, this list of conditions and the following disclaimer in the
1345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    documentation and/or other materials provided with the distribution.
1445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 3. Neither the name of the author nor the names of other contributors
1545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    may be used to endorse or promote products derived from this
1645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    software without specific prior written permission.
1745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
1845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
1945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
2245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * POSSIBILITY OF SUCH DAMAGE.
2945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org */
3045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifndef YASM_GAS_PARSER_H
3145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define YASM_GAS_PARSER_H
3245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
3345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define YYCTYPE         unsigned char
3445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
3545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define MAX_SAVED_LINE_LEN  80
3645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
3745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgenum tokentype {
3845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    INTNUM = 258,
3945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    FLTNUM,
4045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    STRING,
4145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    REG,
4245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    REGGROUP,
4345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    SEGREG,
4445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    TARGETMOD,
4545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    LEFT_OP,
4645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    RIGHT_OP,
4745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    ID,
4845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    LABEL,
4945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    CPP_LINE_MARKER,
5045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    NASM_LINE_MARKER,
5145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    NONE
5245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org};
5345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
5445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgtypedef union {
5545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    unsigned int int_info;
5645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    yasm_intnum *intn;
5745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    yasm_floatnum *flt;
5845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    yasm_bytecode *bc;
5945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    uintptr_t arch_data;
6045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    struct {
6145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        char *contents;
6245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        size_t len;
6345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    } str;
6445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} yystype;
6545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define YYSTYPE yystype
6645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
6745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgenum gas_parser_state {
6845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    INITIAL,
6945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    COMMENT,
7045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    SECTION_DIRECTIVE,
7145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    NASM_FILENAME
7245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org};
7345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
7445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgtypedef struct yasm_parser_gas {
7545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /*@only@*/ yasm_object *object;
7645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
7745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /* last "base" label for local (.) labels */
7845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /*@null@*/ char *locallabel_base;
7945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    size_t locallabel_base_len;
8045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
8145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /* .line/.file: we have to see both to start setting linemap versions */
8245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    int dir_fileline;
8345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /*@null@*/ char *dir_file;
8445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    unsigned long dir_line;
8545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
8645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /* Have we seen a line marker? */
8745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    int seen_line_marker;
8845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
8945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /*@dependent@*/ yasm_preproc *preproc;
9045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /*@dependent@*/ yasm_errwarns *errwarns;
9145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
9245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /*@dependent@*/ yasm_linemap *linemap;
9345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
9445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /*@null@*/ yasm_bytecode *prev_bc;
9545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    yasm_bytecode *temp_bc;
9645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
9745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    int save_input;
9845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    YYCTYPE save_line[2][MAX_SAVED_LINE_LEN];
9945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    int save_last;
10045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
10145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /* Line data storage used in preproc_input(). */
10245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    char *line, *linepos;
10345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    size_t lineleft;
10445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
10545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    yasm_scanner s;
10645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    enum gas_parser_state state;
10745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
10845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    int token;          /* enum tokentype or any character */
10945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    yystype tokval;
11045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    char tokch;         /* first character of token */
11145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
11245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /* one token of lookahead; used sparingly */
11345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    int peek_token;     /* NONE if none */
11445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    yystype peek_tokval;
11545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    char peek_tokch;
11645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
11745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /* Index of local labels; what's stored here is the /next/ index,
11845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org     * so these are all 0 at start.
11945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org     */
12045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    unsigned long local[10];
12145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
12245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    /* Parser-handled directives HAMT lookup */
12345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    HAMT *dirs;
12445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
125a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org    int intel_syntax;
126a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org
12745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    int is_nasm_preproc;
12845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    int is_cpp_preproc;
12945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} yasm_parser_gas;
13045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
13145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/* shorter access names to commonly used parser_gas fields */
13245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define p_object        (parser_gas->object)
13345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define p_symtab        (parser_gas->object->symtab)
13445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define cursect         (parser_gas->object->cur_section)
13545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define curtok          (parser_gas->token)
13645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define curval          (parser_gas->tokval)
13745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
13845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define INTNUM_val              (curval.intn)
13945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define FLTNUM_val              (curval.flt)
14045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define STRING_val              (curval.str)
14145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define REG_val                 (curval.arch_data)
14245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define REGGROUP_val            (curval.arch_data)
14345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define SEGREG_val              (curval.arch_data)
14445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define TARGETMOD_val           (curval.arch_data)
14545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define ID_val                  (curval.str.contents)
14645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define ID_len                  (curval.str.len)
14745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define LABEL_val               (curval.str.contents)
14845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define LABEL_len               (curval.str.len)
14945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
15045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define cur_line        (yasm_linemap_get_current(parser_gas->linemap))
15145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
15245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define p_expr_new(l,o,r)       yasm_expr_create(o,l,r,cur_line)
15345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define p_expr_new_tree(l,o,r)  yasm_expr_create_tree(l,o,r,cur_line)
15445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define p_expr_new_branch(o,r)  yasm_expr_create_branch(o,r,cur_line)
15545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define p_expr_new_ident(r)     yasm_expr_create_ident(r,cur_line)
15645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
157a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.orgyasm_bytecode *parse_instr_intel(yasm_parser_gas *parser_gas);
158a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org
15945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid gas_parser_parse(yasm_parser_gas *parser_gas);
16045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid gas_parser_cleanup(yasm_parser_gas *parser_gas);
16145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgint gas_parser_lex(YYSTYPE *lvalp, yasm_parser_gas *parser_gas);
16245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
16345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif
164