srcfile.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 1996-2020 The NASM Authors - All Rights Reserved
  4. * See the file AUTHORS included with the NASM distribution for
  5. * the specific copyright holders.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following
  9. * conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  20. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * ----------------------------------------------------------------------- */
  33. /*
  34. * These functions are used to keep track of the source code file and name.
  35. */
  36. #ifndef ASM_SRCFILE_H
  37. #define ASM_SRCFILE_H
  38. #include "compiler.h"
  39. struct src_location {
  40. const char *filename;
  41. int32_t lineno;
  42. };
  43. /*
  44. * Comparing the *pointer value* of filenames is valid, because the
  45. * filename hash system guarantees that each unique filename string is
  46. * permanently allocated in exactly one location.
  47. */
  48. static inline bool
  49. src_location_same(struct src_location here, struct src_location there)
  50. {
  51. return here.filename == there.filename && here.lineno == there.lineno;
  52. }
  53. struct src_location_stack {
  54. struct src_location l;
  55. struct src_location_stack *up, *down;
  56. const void *macro;
  57. };
  58. extern struct src_location_stack _src_top;
  59. extern struct src_location_stack *_src_bottom;
  60. extern struct src_location_stack *_src_error;
  61. void src_init(void);
  62. void src_free(void);
  63. const char *src_set_fname(const char *newname);
  64. static inline const char *src_get_fname(void)
  65. {
  66. return _src_bottom->l.filename;
  67. }
  68. static inline int32_t src_set_linnum(int32_t newline)
  69. {
  70. int32_t oldline = _src_bottom->l.lineno;
  71. _src_bottom->l.lineno = newline;
  72. return oldline;
  73. }
  74. static inline int32_t src_get_linnum(void)
  75. {
  76. return _src_bottom->l.lineno;
  77. }
  78. /* Can be used when there is no need for the old information */
  79. void src_set(int32_t line, const char *filename);
  80. /*
  81. * src_get gets both the source file name and line.
  82. * It is also used if you maintain private status about the source location
  83. * It return 0 if the information was the same as the last time you
  84. * checked, -2 if the name changed and (new-old) if just the line changed.
  85. *
  86. * xname must point to a filename string previously returned from any
  87. * function of this subsystem or be NULL; another string value will
  88. * not work.
  89. */
  90. static inline int32_t src_get(int32_t *xline, const char **xname)
  91. {
  92. const char *xn = *xname;
  93. int32_t xl = *xline;
  94. int32_t line = _src_bottom->l.lineno;
  95. *xline = line;
  96. *xname = _src_bottom->l.filename;
  97. /* The return value is expected to be optimized out almost everywhere */
  98. if (!xn || xn != _src_bottom->l.filename)
  99. return -2;
  100. else
  101. return line - xl;
  102. }
  103. /*
  104. * Returns the current information as a structure.
  105. */
  106. static inline struct src_location src_where(void)
  107. {
  108. return _src_bottom->l;
  109. }
  110. /*
  111. * Returns the top-level information as a structure. Use this for panic
  112. * errors, since descent is not possible there.
  113. */
  114. static inline struct src_location src_where_top(void)
  115. {
  116. return _src_top.l;
  117. }
  118. /*
  119. * Returns the appropriate level of the location stack to use for error
  120. * messages. This is the same as the top level except during the descent
  121. * through the macro hierarchy for elucidation;
  122. */
  123. static inline struct src_location src_where_error(void)
  124. {
  125. return _src_error->l;
  126. }
  127. static inline const void *src_error_down(void)
  128. {
  129. if (_src_error->down) {
  130. _src_error = _src_error->down;
  131. return _src_error->macro;
  132. } else {
  133. return NULL;
  134. }
  135. }
  136. static inline void src_error_reset(void)
  137. {
  138. _src_error = &_src_top;
  139. }
  140. /*
  141. * Sets the current information. The filename member of the structure
  142. * *must* have been previously returned by src_get(), src_where(), or
  143. * src_get_fname() and therefore be present in the hash.
  144. */
  145. static inline struct src_location src_update(struct src_location whence)
  146. {
  147. struct src_location old = _src_bottom->l;
  148. _src_bottom->l = whence;
  149. return old;
  150. }
  151. /*
  152. * Push/pop macro expansion level. "macroname" must remain constant at
  153. * least until the same macro expansion level is popped.
  154. */
  155. void src_macro_push(const void *macroname, struct src_location where);
  156. static inline const void *src_macro_current(void)
  157. {
  158. return _src_bottom->macro;
  159. }
  160. void src_macro_pop(void);
  161. #endif /* ASM_SRCFILE_H */