Home | History | Annotate | Download | only in gas
      1 /*
      2  * GAS-compatible parser Intel syntax support
      3  *
      4  *  Copyright (C) 2010  Alexei Svitkine
      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 "modules/parsers/gas/gas-parser.h"
     35 #include "modules/parsers/nasm/nasm-parser-struct.h"
     36 
     37 extern yasm_bytecode *gas_intel_syntax_parse_instr(yasm_parser_nasm *parser_nasm, unsigned char *instr);
     38 
     39 #define SET_FIELDS(to, from) \
     40     (to)->object = (from)->object; \
     41     (to)->locallabel_base = (from)->locallabel_base; \
     42     (to)->locallabel_base_len = (from)->locallabel_base_len; \
     43     (to)->preproc = (from)->preproc; \
     44     (to)->errwarns = (from)->errwarns; \
     45     (to)->linemap = (from)->linemap; \
     46     (to)->prev_bc = (from)->prev_bc;
     47 
     48 yasm_bytecode *parse_instr_intel(yasm_parser_gas *parser_gas)
     49 {
     50     char *stok, *slim;
     51     unsigned char *line;
     52     size_t length;
     53 
     54     yasm_parser_nasm parser_nasm;
     55     yasm_bytecode *bc;
     56 
     57     memset(&parser_nasm, 0, sizeof(parser_nasm));
     58 
     59     yasm_arch_set_var(parser_gas->object->arch, "gas_intel_mode", 1);
     60     SET_FIELDS(&parser_nasm, parser_gas);
     61     parser_nasm.masm = 1;
     62 
     63     stok = (char *) parser_gas->s.tok;
     64     slim = (char *) parser_gas->s.lim;
     65     length = 0;
     66     while (&stok[length] < slim && stok[length] != '\n') {
     67         length++;
     68     }
     69 
     70     if (&stok[length] == slim && parser_gas->line) {
     71         line = yasm_xmalloc(length + parser_gas->lineleft + 1);
     72         memcpy(line, parser_gas->s.tok, length);
     73         memcpy(line + length, parser_gas->linepos, parser_gas->lineleft);
     74         length += parser_gas->lineleft;
     75         if (line[length - 1] == '\n') length--;
     76     } else {
     77         line = yasm_xmalloc(length + 1);
     78         memcpy(line, parser_gas->s.tok, length);
     79     }
     80     line[length] = '\0';
     81 
     82     bc = gas_intel_syntax_parse_instr(&parser_nasm, line);
     83 
     84     SET_FIELDS(parser_gas, &parser_nasm);
     85     yasm_arch_set_var(parser_gas->object->arch, "gas_intel_mode", 0);
     86 
     87     yasm_xfree(line);
     88 
     89     return bc;
     90 }
     91