gas-parse-intel.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include <libyasm.h>
  32. #include "modules/parsers/gas/gas-parser.h"
  33. #include "modules/parsers/nasm/nasm-parser-struct.h"
  34. extern yasm_bytecode *gas_intel_syntax_parse_instr(yasm_parser_nasm *parser_nasm, unsigned char *instr);
  35. #define SET_FIELDS(to, from) \
  36. (to)->object = (from)->object; \
  37. (to)->locallabel_base = (from)->locallabel_base; \
  38. (to)->locallabel_base_len = (from)->locallabel_base_len; \
  39. (to)->preproc = (from)->preproc; \
  40. (to)->errwarns = (from)->errwarns; \
  41. (to)->linemap = (from)->linemap; \
  42. (to)->prev_bc = (from)->prev_bc;
  43. yasm_bytecode *parse_instr_intel(yasm_parser_gas *parser_gas)
  44. {
  45. char *stok, *slim;
  46. unsigned char *line;
  47. size_t length;
  48. yasm_parser_nasm parser_nasm;
  49. yasm_bytecode *bc;
  50. memset(&parser_nasm, 0, sizeof(parser_nasm));
  51. yasm_arch_set_var(parser_gas->object->arch, "gas_intel_mode", 1);
  52. SET_FIELDS(&parser_nasm, parser_gas);
  53. parser_nasm.masm = 1;
  54. stok = (char *) parser_gas->s.tok;
  55. slim = (char *) parser_gas->s.lim;
  56. length = 0;
  57. while (&stok[length] < slim && stok[length] != '\n') {
  58. length++;
  59. }
  60. if (&stok[length] == slim && parser_gas->line) {
  61. line = yasm_xmalloc(length + parser_gas->lineleft + 1);
  62. memcpy(line, parser_gas->s.tok, length);
  63. memcpy(line + length, parser_gas->linepos, parser_gas->lineleft);
  64. length += parser_gas->lineleft;
  65. if (line[length - 1] == '\n') length--;
  66. } else {
  67. line = yasm_xmalloc(length + 1);
  68. memcpy(line, parser_gas->s.tok, length);
  69. }
  70. line[length] = '\0';
  71. bc = gas_intel_syntax_parse_instr(&parser_nasm, line);
  72. SET_FIELDS(parser_gas, &parser_nasm);
  73. yasm_arch_set_var(parser_gas->object->arch, "gas_intel_mode", 0);
  74. yasm_xfree(line);
  75. return bc;
  76. }