gas-parser.c 4.2 KB

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