Home | History | Annotate | Download | only in gas
      1 /*
      2  * GAS-compatible parser
      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 #include <util.h>
     31 
     32 #include <libyasm.h>
     33 
     34 #include "gas-parser.h"
     35 
     36 
     37 static void
     38 gas_parser_do_parse(yasm_object *object, yasm_preproc *pp,
     39                     int save_input, yasm_linemap *linemap,
     40                     yasm_errwarns *errwarns)
     41 {
     42     yasm_parser_gas parser_gas;
     43     int i;
     44 
     45     parser_gas.object = object;
     46     parser_gas.linemap = linemap;
     47 
     48     parser_gas.locallabel_base = (char *)NULL;
     49     parser_gas.locallabel_base_len = 0;
     50 
     51     parser_gas.dir_fileline = 0;
     52     parser_gas.dir_file = NULL;
     53     parser_gas.dir_line = 0;
     54     parser_gas.seen_line_marker = 0;
     55 
     56     parser_gas.preproc = pp;
     57     parser_gas.errwarns = errwarns;
     58 
     59     parser_gas.prev_bc = yasm_section_bcs_first(object->cur_section);
     60 
     61     parser_gas.save_input = save_input;
     62     parser_gas.save_last = 0;
     63 
     64     parser_gas.peek_token = NONE;
     65 
     66     parser_gas.line = NULL;
     67 
     68     /* initialize scanner structure */
     69     yasm_scanner_initialize(&parser_gas.s);
     70 
     71     parser_gas.state = INITIAL;
     72 
     73     for (i=0; i<10; i++)
     74         parser_gas.local[i] = 0;
     75 
     76     parser_gas.intel_syntax = 0;
     77 
     78     parser_gas.is_cpp_preproc =
     79         yasm__strcasecmp(((yasm_preproc_base*)pp)->module->keyword, "cpp") == 0;
     80     parser_gas.is_nasm_preproc =
     81         yasm__strcasecmp(((yasm_preproc_base*)pp)->module->keyword, "nasm") == 0;
     82 
     83     gas_parser_parse(&parser_gas);
     84 
     85     /* Check for ending inside a comment */
     86     if (parser_gas.state == COMMENT) {
     87         yasm_warn_set(YASM_WARN_GENERAL, N_("end of file in comment"));
     88         /* XXX: Minus two to compensate for already having moved past the EOF
     89          * in the linemap.
     90          */
     91         yasm_errwarn_propagate(errwarns,
     92                                yasm_linemap_get_current(parser_gas.linemap)-2);
     93     }
     94 
     95     yasm_scanner_delete(&parser_gas.s);
     96 
     97     /* Free locallabel base if necessary */
     98     if (parser_gas.locallabel_base)
     99         yasm_xfree(parser_gas.locallabel_base);
    100 
    101     if (parser_gas.dir_file)
    102         yasm_xfree(parser_gas.dir_file);
    103 
    104     /* Convert all undefined symbols into extern symbols */
    105     yasm_symtab_parser_finalize(object->symtab, 1, errwarns);
    106 }
    107 
    108 /* Define valid preprocessors to use with this parser */
    109 static const char *gas_parser_preproc_keywords[] = {
    110     "gas",
    111     "raw",
    112     "cpp",
    113     "nasm",
    114     NULL
    115 };
    116 
    117 /* Define parser structure -- see parser.h for details */
    118 yasm_parser_module yasm_gas_LTX_parser = {
    119     "GNU AS (GAS)-compatible parser",
    120     "gas",
    121     gas_parser_preproc_keywords,
    122     "gas",
    123     NULL,   /* No standard macros */
    124     gas_parser_do_parse
    125 };
    126 yasm_parser_module yasm_gnu_LTX_parser = {
    127     "GNU AS (GAS)-compatible parser",
    128     "gnu",
    129     gas_parser_preproc_keywords,
    130     "gas",
    131     NULL,   /* No standard macros */
    132     gas_parser_do_parse
    133 };
    134