symrec.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /**
  2. * \file libyasm/symrec.h
  3. * \brief YASM symbol table interface.
  4. *
  5. * \license
  6. * Copyright (C) 2001-2007 Michael Urman, Peter Johnson
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  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 copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
  21. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. * \endlicense
  29. */
  30. #ifndef YASM_SYMREC_H
  31. #define YASM_SYMREC_H
  32. #ifndef YASM_LIB_DECL
  33. #define YASM_LIB_DECL
  34. #endif
  35. /** Symbol status. YASM_SYM_DEFINED is set by yasm_symtab_define_label(),
  36. * yasm_symtab_define_equ(), or yasm_symtab_declare()/yasm_symrec_declare()
  37. * with a visibility of #YASM_SYM_EXTERN or #YASM_SYM_COMMON.
  38. */
  39. typedef enum yasm_sym_status {
  40. YASM_SYM_NOSTATUS = 0, /**< no status */
  41. YASM_SYM_USED = 1 << 0, /**< for use before definition */
  42. YASM_SYM_DEFINED = 1 << 1, /**< once it's been defined in the file */
  43. YASM_SYM_VALUED = 1 << 2, /**< once its value has been determined */
  44. YASM_SYM_NOTINTABLE = 1 << 3 /**< if it's not in sym_table (ex. '$') */
  45. } yasm_sym_status;
  46. /** Symbol record visibility.
  47. * \note YASM_SYM_EXTERN and YASM_SYM_COMMON are mutually exclusive.
  48. */
  49. typedef enum yasm_sym_vis {
  50. YASM_SYM_LOCAL = 0, /**< Default, local only */
  51. YASM_SYM_GLOBAL = 1 << 0, /**< If symbol is declared GLOBAL */
  52. YASM_SYM_COMMON = 1 << 1, /**< If symbol is declared COMMON */
  53. YASM_SYM_EXTERN = 1 << 2, /**< If symbol is declared EXTERN */
  54. YASM_SYM_DLOCAL = 1 << 3 /**< If symbol is explicitly declared LOCAL */
  55. } yasm_sym_vis;
  56. /** Create a new symbol table. */
  57. YASM_LIB_DECL
  58. yasm_symtab *yasm_symtab_create(void);
  59. /** Destroy a symbol table and all internal symbols.
  60. * \param symtab symbol table
  61. * \warning All yasm_symrec *'s into this symbol table become invalid after
  62. * this is called!
  63. */
  64. YASM_LIB_DECL
  65. void yasm_symtab_destroy(/*@only@*/ yasm_symtab *symtab);
  66. /** Set the symbol table to be case sensitive or not.
  67. * Should be called before adding any symbol.
  68. * \param symtab symbol table
  69. * \param sensitive whether the symbol table should be case sensitive.
  70. */
  71. YASM_LIB_DECL
  72. void yasm_symtab_set_case_sensitive(yasm_symtab *symtab, int sensitive);
  73. /** Get a reference to the symbol table's "absolute" symbol. This is
  74. * essentially an EQU with no name and value 0, and is used for relocating
  75. * absolute current-position-relative values.
  76. * \see yasm_value_set_curpos_rel().
  77. * \param symtab symbol table
  78. * \return Absolute symbol (dependent pointer, do not free).
  79. */
  80. YASM_LIB_DECL
  81. /*@dependent@*/ yasm_symrec *yasm_symtab_abs_sym(yasm_symtab *symtab);
  82. /** Get a reference to (use) a symbol. The symbol does not necessarily need to
  83. * be defined before it is used.
  84. * \param symtab symbol table
  85. * \param name symbol name
  86. * \param line virtual line where referenced
  87. * \return Symbol (dependent pointer, do not free).
  88. */
  89. YASM_LIB_DECL
  90. /*@dependent@*/ yasm_symrec *yasm_symtab_use
  91. (yasm_symtab *symtab, const char *name, unsigned long line);
  92. /** Get a reference to a symbol, without "using" it. Should be used for cases
  93. * when an internal assembler usage of a symbol shouldn't be treated like a
  94. * normal user symbol usage.
  95. * \param symtab symbol table
  96. * \param name symbol name
  97. * \return Symbol (dependent pointer, do not free). May be NULL if symbol
  98. * doesn't exist.
  99. */
  100. YASM_LIB_DECL
  101. /*@null@*/ /*@dependent@*/ yasm_symrec *yasm_symtab_get
  102. (yasm_symtab *symtab, const char *name);
  103. /** Define a symbol as an EQU value.
  104. * \param symtab symbol table
  105. * \param name symbol (EQU) name
  106. * \param e EQU value (expression)
  107. * \param line virtual line of EQU
  108. * \return Symbol (dependent pointer, do not free).
  109. */
  110. YASM_LIB_DECL
  111. /*@dependent@*/ yasm_symrec *yasm_symtab_define_equ
  112. (yasm_symtab *symtab, const char *name, /*@keep@*/ yasm_expr *e,
  113. unsigned long line);
  114. /** Define a symbol as a label.
  115. * \param symtab symbol table
  116. * \param name symbol (label) name
  117. * \param precbc bytecode preceding label
  118. * \param in_table nonzero if the label should be inserted into the symbol
  119. * table (some specially-generated ones should not be)
  120. * \param line virtual line of label
  121. * \return Symbol (dependent pointer, do not free).
  122. */
  123. YASM_LIB_DECL
  124. /*@dependent@*/ yasm_symrec *yasm_symtab_define_label
  125. (yasm_symtab *symtab, const char *name,
  126. /*@dependent@*/ yasm_bytecode *precbc, int in_table, unsigned long line);
  127. /** Define a symbol as a label representing the current assembly position.
  128. * This should be used for this purpose instead of yasm_symtab_define_label()
  129. * as value_finalize_scan() looks for usage of this symbol type for special
  130. * handling. The symbol created is not inserted into the symbol table.
  131. * \param symtab symbol table
  132. * \param name symbol (label) name
  133. * \param precbc bytecode preceding label
  134. * \param line virtual line of label
  135. * \return Symbol (dependent pointer, do not free).
  136. */
  137. YASM_LIB_DECL
  138. /*@dependent@*/ yasm_symrec *yasm_symtab_define_curpos
  139. (yasm_symtab *symtab, const char *name,
  140. /*@dependent@*/ yasm_bytecode *precbc, unsigned long line);
  141. /** Define a special symbol that will appear in the symbol table and have a
  142. * defined name, but have no other data associated with it within the
  143. * standard symrec.
  144. * \param symtab symbol table
  145. * \param name symbol name
  146. * \param vis symbol visibility
  147. * \return Symbol (dependent pointer, do not free).
  148. */
  149. YASM_LIB_DECL
  150. /*@dependent@*/ yasm_symrec *yasm_symtab_define_special
  151. (yasm_symtab *symtab, const char *name, yasm_sym_vis vis);
  152. /** Declare external visibility of a symbol.
  153. * \note Not all visibility combinations are allowed.
  154. * \param symtab symbol table
  155. * \param name symbol name
  156. * \param vis visibility
  157. * \param line virtual line of visibility-setting
  158. * \return Symbol (dependent pointer, do not free).
  159. */
  160. YASM_LIB_DECL
  161. /*@dependent@*/ yasm_symrec *yasm_symtab_declare
  162. (yasm_symtab *symtab, const char *name, yasm_sym_vis vis,
  163. unsigned long line);
  164. /** Declare external visibility of a symbol.
  165. * \note Not all visibility combinations are allowed.
  166. * \param symrec symbol
  167. * \param vis visibility
  168. * \param line virtual line of visibility-setting
  169. */
  170. YASM_LIB_DECL
  171. void yasm_symrec_declare(yasm_symrec *symrec, yasm_sym_vis vis,
  172. unsigned long line);
  173. /** Callback function for yasm_symrec_traverse().
  174. * \param sym symbol
  175. * \param d data passed into yasm_symrec_traverse()
  176. * \return Nonzero to stop symbol traversal.
  177. */
  178. typedef int (*yasm_symtab_traverse_callback)
  179. (yasm_symrec *sym, /*@null@*/ void *d);
  180. /** Traverse all symbols in the symbol table.
  181. * \param symtab symbol table
  182. * \param d data to pass to each call of callback function
  183. * \param func callback function called on each symbol
  184. * \return Nonzero value returned by callback function if it ever returned
  185. * nonzero.
  186. */
  187. YASM_LIB_DECL
  188. int /*@alt void@*/ yasm_symtab_traverse
  189. (yasm_symtab *symtab, /*@null@*/ void *d,
  190. yasm_symtab_traverse_callback func);
  191. /** Symbol table iterator (opaque type). */
  192. typedef struct yasm_symtab_iter yasm_symtab_iter;
  193. /** Get an iterator pointing to the first symbol in the symbol table.
  194. * \param symtab symbol table
  195. * \return Iterator for the symbol table.
  196. */
  197. YASM_LIB_DECL
  198. const yasm_symtab_iter *yasm_symtab_first(const yasm_symtab *symtab);
  199. /** Move a symbol table iterator to the next symbol in the symbol table.
  200. * \param prev Previous iterator value
  201. * \return Next iterator value, or NULL if no more symbols in the table.
  202. */
  203. YASM_LIB_DECL
  204. /*@null@*/ const yasm_symtab_iter *yasm_symtab_next
  205. (const yasm_symtab_iter *prev);
  206. /** Get the symbol corresponding to the current symbol table iterator value.
  207. * \param cur iterator value
  208. * \return Corresponding symbol.
  209. */
  210. YASM_LIB_DECL
  211. yasm_symrec *yasm_symtab_iter_value(const yasm_symtab_iter *cur);
  212. /** Finalize symbol table after parsing stage. Checks for symbols that are
  213. * used but never defined or declared #YASM_SYM_EXTERN or #YASM_SYM_COMMON.
  214. * \param symtab symbol table
  215. * \param undef_extern if nonzero, all undef syms should be declared extern
  216. * \param errwarns error/warning set
  217. * \note Errors/warnings are stored into errwarns.
  218. */
  219. YASM_LIB_DECL
  220. void yasm_symtab_parser_finalize(yasm_symtab *symtab, int undef_extern,
  221. yasm_errwarns *errwarns);
  222. /** Print the symbol table. For debugging purposes.
  223. * \param symtab symbol table
  224. * \param f file
  225. * \param indent_level indentation level
  226. */
  227. YASM_LIB_DECL
  228. void yasm_symtab_print(yasm_symtab *symtab, FILE *f, int indent_level);
  229. /** Get the name of a symbol.
  230. * \param sym symbol
  231. * \return Symbol name.
  232. */
  233. YASM_LIB_DECL
  234. /*@observer@*/ const char *yasm_symrec_get_name(const yasm_symrec *sym);
  235. /** Get the externally-visible (global) name of a symbol.
  236. * \param sym symbol
  237. * \param object object
  238. * \return Externally-visible symbol name (allocated, caller must free).
  239. */
  240. YASM_LIB_DECL
  241. /*@only@*/ char *yasm_symrec_get_global_name(const yasm_symrec *sym,
  242. const yasm_object *object);
  243. /** Get the visibility of a symbol.
  244. * \param sym symbol
  245. * \return Symbol visibility.
  246. */
  247. YASM_LIB_DECL
  248. yasm_sym_vis yasm_symrec_get_visibility(const yasm_symrec *sym);
  249. /** Get the status of a symbol.
  250. * \param sym symbol
  251. * \return Symbol status.
  252. */
  253. YASM_LIB_DECL
  254. yasm_sym_status yasm_symrec_get_status(const yasm_symrec *sym);
  255. /** Get the virtual line of where a symbol was first defined.
  256. * \param sym symbol
  257. * \return line virtual line
  258. */
  259. YASM_LIB_DECL
  260. unsigned long yasm_symrec_get_def_line(const yasm_symrec *sym);
  261. /** Get the virtual line of where a symbol was first declared.
  262. * \param sym symbol
  263. * \return line virtual line
  264. */
  265. YASM_LIB_DECL
  266. unsigned long yasm_symrec_get_decl_line(const yasm_symrec *sym);
  267. /** Get the virtual line of where a symbol was first used.
  268. * \param sym symbol
  269. * \return line virtual line
  270. */
  271. YASM_LIB_DECL
  272. unsigned long yasm_symrec_get_use_line(const yasm_symrec *sym);
  273. /** Get EQU value of a symbol.
  274. * \param sym symbol
  275. * \return EQU value, or NULL if symbol is not an EQU or is not defined.
  276. */
  277. YASM_LIB_DECL
  278. /*@observer@*/ /*@null@*/ const yasm_expr *yasm_symrec_get_equ
  279. (const yasm_symrec *sym);
  280. /** Dependent pointer to a bytecode. */
  281. typedef /*@dependent@*/ yasm_bytecode *yasm_symrec_get_label_bytecodep;
  282. /** Get the label location of a symbol.
  283. * \param sym symbol
  284. * \param precbc bytecode preceding label (output)
  285. * \return 0 if not symbol is not a label or if the symbol's visibility is
  286. * #YASM_SYM_EXTERN or #YASM_SYM_COMMON (not defined in the file).
  287. */
  288. YASM_LIB_DECL
  289. int yasm_symrec_get_label(const yasm_symrec *sym,
  290. /*@out@*/ yasm_symrec_get_label_bytecodep *precbc);
  291. /** Set the size of a symbol.
  292. * \param sym symbol
  293. * \param size size to be set
  294. */
  295. YASM_LIB_DECL
  296. void yasm_symrec_set_size(yasm_symrec *sym, int size);
  297. /** Get the size of a symbol.
  298. * \param sym symbol
  299. * \return size of the symbol, 0 if none specified by the user.
  300. */
  301. YASM_LIB_DECL
  302. int yasm_symrec_get_size(const yasm_symrec *sym);
  303. /** Set the segment of a symbol.
  304. * \param sym symbol
  305. * \param segment segment to be set
  306. */
  307. YASM_LIB_DECL
  308. void yasm_symrec_set_segment(yasm_symrec *sym, const char *segment);
  309. /** Get the segment of a symbol.
  310. * \param sym symbol
  311. * \return segment of the symbol, NULL if none specified by the user.
  312. */
  313. YASM_LIB_DECL
  314. const char *yasm_symrec_get_segment(const yasm_symrec *sym);
  315. /** Determine if symbol is the "absolute" symbol created by
  316. * yasm_symtab_abs_sym().
  317. * \param sym symbol
  318. * \return 0 if symbol is not the "absolute" symbol, nonzero otherwise.
  319. */
  320. YASM_LIB_DECL
  321. int yasm_symrec_is_abs(const yasm_symrec *sym);
  322. /** Determine if symbol is a special symbol.
  323. * \param sym symbol
  324. * \return 0 if symbol is not a special symbol, nonzero otherwise.
  325. */
  326. YASM_LIB_DECL
  327. int yasm_symrec_is_special(const yasm_symrec *sym);
  328. /** Determine if symbol is a label representing the current assembly position.
  329. * \param sym symbol
  330. * \return 0 if symbol is not a current position label, nonzero otherwise.
  331. */
  332. YASM_LIB_DECL
  333. int yasm_symrec_is_curpos(const yasm_symrec *sym);
  334. /** Set object-extended valparams.
  335. * \param sym symbol
  336. * \param objext_valparams object-extended valparams
  337. */
  338. YASM_LIB_DECL
  339. void yasm_symrec_set_objext_valparams
  340. (yasm_symrec *sym, /*@only@*/ yasm_valparamhead *objext_valparams);
  341. /** Get object-extended valparams, if any, associated with symbol's
  342. * declaration.
  343. * \param sym symbol
  344. * \return Object-extended valparams (NULL if none).
  345. */
  346. YASM_LIB_DECL
  347. /*@null@*/ /*@dependent@*/ yasm_valparamhead *yasm_symrec_get_objext_valparams
  348. (yasm_symrec *sym);
  349. /** Set common size of symbol.
  350. * \param sym symbol
  351. * \param common_size common size expression
  352. */
  353. YASM_LIB_DECL
  354. void yasm_symrec_set_common_size
  355. (yasm_symrec *sym, /*@only@*/ yasm_expr *common_size);
  356. /** Get common size of symbol, if symbol is declared COMMON and a size was set
  357. * for it.
  358. * \param sym symbol
  359. * \return Common size (NULL if none).
  360. */
  361. YASM_LIB_DECL
  362. /*@dependent@*/ /*@null@*/ yasm_expr **yasm_symrec_get_common_size
  363. (yasm_symrec *sym);
  364. /** Get associated data for a symbol and data callback.
  365. * \param sym symbol
  366. * \param callback callback used when adding data
  367. * \return Associated data (NULL if none).
  368. */
  369. YASM_LIB_DECL
  370. /*@dependent@*/ /*@null@*/ void *yasm_symrec_get_data
  371. (yasm_symrec *sym, const yasm_assoc_data_callback *callback);
  372. /** Add associated data to a symbol.
  373. * \attention Deletes any existing associated data for that data callback.
  374. * \param sym symbol
  375. * \param callback callback
  376. * \param data data to associate
  377. */
  378. YASM_LIB_DECL
  379. void yasm_symrec_add_data(yasm_symrec *sym,
  380. const yasm_assoc_data_callback *callback,
  381. /*@only@*/ /*@null@*/ void *data);
  382. /** Print a symbol. For debugging purposes.
  383. * \param f file
  384. * \param indent_level indentation level
  385. * \param sym symbol
  386. */
  387. YASM_LIB_DECL
  388. void yasm_symrec_print(const yasm_symrec *sym, FILE *f, int indent_level);
  389. #endif