bytecode.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /**
  2. * \file libyasm/bytecode.h
  3. * \brief YASM bytecode interface.
  4. *
  5. * \license
  6. * Copyright (C) 2001-2007 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_BYTECODE_H
  31. #define YASM_BYTECODE_H
  32. #ifndef YASM_LIB_DECL
  33. #define YASM_LIB_DECL
  34. #endif
  35. /** A data value (opaque type). */
  36. typedef struct yasm_dataval yasm_dataval;
  37. /** A list of data values. */
  38. typedef struct yasm_datavalhead yasm_datavalhead;
  39. /** Linked list of data values. */
  40. /*@reldef@*/ STAILQ_HEAD(yasm_datavalhead, yasm_dataval);
  41. /** Add a dependent span for a bytecode.
  42. * \param add_span_data add_span_data passed into bc_calc_len()
  43. * \param bc bytecode containing span
  44. * \param id non-zero identifier for span; may be any non-zero value
  45. * if <0, expand is called for any change;
  46. * if >0, expand is only called when exceeds threshold
  47. * \param value dependent value for bytecode expansion
  48. * \param neg_thres negative threshold for long/short decision
  49. * \param pos_thres positive threshold for long/short decision
  50. */
  51. typedef void (*yasm_bc_add_span_func)
  52. (void *add_span_data, yasm_bytecode *bc, int id, const yasm_value *value,
  53. long neg_thres, long pos_thres);
  54. /** Bytecode callback structure. Any implementation of a specific bytecode
  55. * must implement these functions and this callback structure. The bytecode
  56. * implementation-specific data is stored in #yasm_bytecode.contents.
  57. */
  58. typedef struct yasm_bytecode_callback {
  59. /** Destroys the implementation-specific data.
  60. * Called from yasm_bc_destroy().
  61. * \param contents #yasm_bytecode.contents
  62. */
  63. void (*destroy) (/*@only@*/ void *contents);
  64. /** Prints the implementation-specific data (for debugging purposes).
  65. * Called from yasm_bc_print().
  66. * \param contents #yasm_bytecode.contents
  67. * \param f file
  68. * \param indent_level indentation level
  69. */
  70. void (*print) (const void *contents, FILE *f, int indent_level);
  71. /** Finalizes the bytecode after parsing. Called from yasm_bc_finalize().
  72. * A generic fill-in for this is yasm_bc_finalize_common().
  73. * \param bc bytecode
  74. * \param prev_bc bytecode directly preceding bc
  75. */
  76. void (*finalize) (yasm_bytecode *bc, yasm_bytecode *prev_bc);
  77. /** Return elements size of a data bytecode.
  78. * This function should return the size of each elements of a data
  79. * bytecode, for proper dereference of symbols attached to it.
  80. * \param bc bytecode
  81. * \return 0 if element size is unknown.
  82. */
  83. int (*elem_size) (yasm_bytecode *bc);
  84. /** Calculates the minimum size of a bytecode.
  85. * Called from yasm_bc_calc_len().
  86. * A generic fill-in for this is yasm_bc_calc_len_common(), but as this
  87. * function internal errors when called, be very careful when using it!
  88. * This function should simply add to bc->len and not set it directly
  89. * (it's initialized by yasm_bc_calc_len() prior to passing control to
  90. * this function).
  91. *
  92. * \param bc bytecode
  93. * \param add_span function to call to add a span
  94. * \param add_span_data extra data to be passed to add_span function
  95. * \return 0 if no error occurred, nonzero if there was an error
  96. * recognized (and output) during execution.
  97. * \note May store to bytecode updated expressions.
  98. */
  99. int (*calc_len) (yasm_bytecode *bc, yasm_bc_add_span_func add_span,
  100. void *add_span_data);
  101. /** Recalculates the bytecode's length based on an expanded span length.
  102. * Called from yasm_bc_expand().
  103. * A generic fill-in for this is yasm_bc_expand_common(), but as this
  104. * function internal errors when called, if used, ensure that calc_len()
  105. * never adds a span.
  106. * This function should simply add to bc->len to increase the length by
  107. * a delta amount.
  108. * \param bc bytecode
  109. * \param span span ID (as given to add_span in calc_len)
  110. * \param old_val previous span value
  111. * \param new_val new span value
  112. * \param neg_thres negative threshold for long/short decision
  113. * (returned)
  114. * \param pos_thres positive threshold for long/short decision
  115. * (returned)
  116. * \return 0 if bc no longer dependent on this span's length, negative if
  117. * there was an error recognized (and output) during execution,
  118. * and positive if bc size may increase for this span further
  119. * based on the new negative and positive thresholds returned.
  120. * \note May store to bytecode updated expressions.
  121. */
  122. int (*expand) (yasm_bytecode *bc, int span, long old_val, long new_val,
  123. /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres);
  124. /** Convert a bytecode into its byte representation.
  125. * Called from yasm_bc_tobytes().
  126. * A generic fill-in for this is yasm_bc_tobytes_common(), but as this
  127. * function internal errors when called, be very careful when using it!
  128. * \param bc bytecode
  129. * \param bufp byte representation destination buffer;
  130. * should be incremented as it's written to,
  131. * so that on return its delta from the
  132. * passed-in buf matches the bytecode length
  133. * (it's okay not to do this if an error
  134. * indication is returned)
  135. * \param bufstart For calculating the correct offset parameter for
  136. * the \a output_value calls: *bufp - bufstart.
  137. * \param d data to pass to each call to
  138. * output_value/output_reloc
  139. * \param output_value function to call to convert values into their byte
  140. * representation
  141. * \param output_reloc function to call to output relocation entries
  142. * for a single sym
  143. * \return Nonzero on error, 0 on success.
  144. * \note May result in non-reversible changes to the bytecode, but it's
  145. * preferable if calling this function twice would result in the
  146. * same output.
  147. */
  148. int (*tobytes) (yasm_bytecode *bc, unsigned char **bufp,
  149. unsigned char *bufstart, void *d,
  150. yasm_output_value_func output_value,
  151. /*@null@*/ yasm_output_reloc_func output_reloc);
  152. /** Special bytecode classifications. Most bytecode types should use
  153. * #YASM_BC_SPECIAL_NONE. Others cause special handling to kick in
  154. * in various parts of yasm.
  155. */
  156. enum yasm_bytecode_special_type {
  157. YASM_BC_SPECIAL_NONE = 0,
  158. /** Bytecode reserves space instead of outputting data. */
  159. YASM_BC_SPECIAL_RESERVE,
  160. /** Adjusts offset instead of calculating len. */
  161. YASM_BC_SPECIAL_OFFSET,
  162. /** Instruction bytecode. */
  163. YASM_BC_SPECIAL_INSN
  164. } special;
  165. } yasm_bytecode_callback;
  166. /** A bytecode. */
  167. struct yasm_bytecode {
  168. /** Bytecodes are stored as a singly linked list, with tail insertion.
  169. * \see section.h (#yasm_section).
  170. */
  171. /*@reldef@*/ STAILQ_ENTRY(yasm_bytecode) link;
  172. /** The bytecode callback structure for this bytecode. May be NULL
  173. * during partial initialization.
  174. */
  175. /*@null@*/ const yasm_bytecode_callback *callback;
  176. /** Pointer to section containing bytecode; NULL if not part of a
  177. * section.
  178. */
  179. /*@dependent@*/ /*@null@*/ yasm_section *section;
  180. /** Number of times bytecode is repeated.
  181. * NULL=1 (to save space in the common case).
  182. */
  183. /*@only@*/ /*@null@*/ yasm_expr *multiple;
  184. /** Total length of entire bytecode (not including multiple copies). */
  185. unsigned long len;
  186. /** Number of copies, integer version. */
  187. long mult_int;
  188. /** Line number where bytecode was defined. */
  189. unsigned long line;
  190. /** Offset of bytecode from beginning of its section.
  191. * 0-based, ~0UL (e.g. all 1 bits) if unknown.
  192. */
  193. unsigned long offset;
  194. /** Unique integer index of bytecode. Used during optimization. */
  195. unsigned long bc_index;
  196. /** NULL-terminated array of labels that point to this bytecode (as the
  197. * bytecode previous to the label). NULL if no labels point here.
  198. */
  199. /*@null@*/ yasm_symrec **symrecs;
  200. /** Implementation-specific data (type identified by callback). */
  201. void *contents;
  202. };
  203. /** Create a bytecode of any specified type.
  204. * \param callback bytecode callback functions, if NULL, creates empty
  205. * bytecode (may not be resolved or output)
  206. * \param contents type-specific data
  207. * \param line virtual line (from yasm_linemap)
  208. * \return Newly allocated bytecode of the specified type.
  209. */
  210. YASM_LIB_DECL
  211. /*@only@*/ yasm_bytecode *yasm_bc_create_common
  212. (/*@null@*/ const yasm_bytecode_callback *callback,
  213. /*@only@*/ /*@null@*/ void *contents, unsigned long line);
  214. /** Transform a bytecode of any type into a different type.
  215. * \param bc bytecode to transform
  216. * \param callback new bytecode callback function
  217. * \param contents new type-specific data
  218. */
  219. YASM_LIB_DECL
  220. void yasm_bc_transform(yasm_bytecode *bc,
  221. const yasm_bytecode_callback *callback,
  222. void *contents);
  223. /** Common bytecode callback finalize function, for where no finalization
  224. * is ever required for this type of bytecode.
  225. */
  226. YASM_LIB_DECL
  227. void yasm_bc_finalize_common(yasm_bytecode *bc, yasm_bytecode *prev_bc);
  228. /** Common bytecode callback calc_len function, for where the bytecode has
  229. * no calculatable length. Causes an internal error if called.
  230. */
  231. YASM_LIB_DECL
  232. int yasm_bc_calc_len_common(yasm_bytecode *bc, yasm_bc_add_span_func add_span,
  233. void *add_span_data);
  234. /** Common bytecode callback expand function, for where the bytecode is
  235. * always short (calc_len never calls add_span). Causes an internal
  236. * error if called.
  237. */
  238. YASM_LIB_DECL
  239. int yasm_bc_expand_common
  240. (yasm_bytecode *bc, int span, long old_val, long new_val,
  241. /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres);
  242. /** Common bytecode callback tobytes function, for where the bytecode
  243. * cannot be converted to bytes. Causes an internal error if called.
  244. */
  245. YASM_LIB_DECL
  246. int yasm_bc_tobytes_common
  247. (yasm_bytecode *bc, unsigned char **bufp, unsigned char *bufstart, void *d,
  248. yasm_output_value_func output_value,
  249. /*@null@*/ yasm_output_reloc_func output_reloc);
  250. /** Get the next bytecode in a linked list of bytecodes.
  251. * \param bc bytecode
  252. * \return Next bytecode.
  253. */
  254. #define yasm_bc__next(bc) STAILQ_NEXT(bc, link)
  255. /** Set multiple field of a bytecode.
  256. * A bytecode can be repeated a number of times when output. This function
  257. * sets that multiple.
  258. * \param bc bytecode
  259. * \param e multiple (kept, do not free)
  260. */
  261. YASM_LIB_DECL
  262. void yasm_bc_set_multiple(yasm_bytecode *bc, /*@keep@*/ yasm_expr *e);
  263. /** Create a bytecode containing data value(s).
  264. * \param datahead list of data values (kept, do not free)
  265. * \param size storage size (in bytes) for each data value
  266. * \param append_zero append a single zero byte after each data value
  267. * (if non-zero)
  268. * \param arch architecture (optional); if provided, data items
  269. * are directly simplified to bytes if possible
  270. * \param line virtual line (from yasm_linemap)
  271. * \return Newly allocated bytecode.
  272. */
  273. YASM_LIB_DECL
  274. /*@only@*/ yasm_bytecode *yasm_bc_create_data
  275. (yasm_datavalhead *datahead, unsigned int size, int append_zero,
  276. /*@null@*/ yasm_arch *arch, unsigned long line);
  277. /** Create a bytecode containing LEB128-encoded data value(s).
  278. * \param datahead list of data values (kept, do not free)
  279. * \param sign signedness (1=signed, 0=unsigned) of each data value
  280. * \param line virtual line (from yasm_linemap)
  281. * \return Newly allocated bytecode.
  282. */
  283. YASM_LIB_DECL
  284. /*@only@*/ yasm_bytecode *yasm_bc_create_leb128
  285. (yasm_datavalhead *datahead, int sign, unsigned long line);
  286. /** Create a bytecode reserving space.
  287. * \param numitems number of reserve "items" (kept, do not free)
  288. * \param itemsize reserved size (in bytes) for each item
  289. * \param line virtual line (from yasm_linemap)
  290. * \return Newly allocated bytecode.
  291. */
  292. YASM_LIB_DECL
  293. /*@only@*/ yasm_bytecode *yasm_bc_create_reserve
  294. (/*@only@*/ yasm_expr *numitems, unsigned int itemsize,
  295. unsigned long line);
  296. /** Get the number of items and itemsize for a reserve bytecode. If bc
  297. * is not a reserve bytecode, returns NULL.
  298. * \param bc bytecode
  299. * \param itemsize reserved size (in bytes) for each item (returned)
  300. * \return NULL if bc is not a reserve bytecode, otherwise an expression
  301. * for the number of items to reserve.
  302. */
  303. YASM_LIB_DECL
  304. /*@null@*/ const yasm_expr *yasm_bc_reserve_numitems
  305. (yasm_bytecode *bc, /*@out@*/ unsigned int *itemsize);
  306. /** Create a bytecode that includes a binary file verbatim.
  307. * \param filename path to binary file (kept, do not free)
  308. * \param start starting location in file (in bytes) to read data from
  309. * (kept, do not free); may be NULL to indicate 0
  310. * \param maxlen maximum number of bytes to read from the file (kept, do
  311. * do not free); may be NULL to indicate no maximum
  312. * \param linemap line mapping repository
  313. * \param line virtual line (from yasm_linemap) for the bytecode
  314. * \return Newly allocated bytecode.
  315. */
  316. YASM_LIB_DECL
  317. /*@only@*/ yasm_bytecode *yasm_bc_create_incbin
  318. (/*@only@*/ char *filename, /*@only@*/ /*@null@*/ yasm_expr *start,
  319. /*@only@*/ /*@null@*/ yasm_expr *maxlen, yasm_linemap *linemap,
  320. unsigned long line);
  321. /** Create a bytecode that aligns the following bytecode to a boundary.
  322. * \param boundary byte alignment (must be a power of two)
  323. * \param fill fill data (if NULL, code_fill or 0 is used)
  324. * \param maxskip maximum number of bytes to skip
  325. * \param code_fill code fill data (if NULL, 0 is used)
  326. * \param line virtual line (from yasm_linemap)
  327. * \return Newly allocated bytecode.
  328. * \note The precedence on generated fill is as follows:
  329. * - from fill parameter (if not NULL)
  330. * - from code_fill parameter (if not NULL)
  331. * - 0
  332. */
  333. YASM_LIB_DECL
  334. /*@only@*/ yasm_bytecode *yasm_bc_create_align
  335. (/*@keep@*/ yasm_expr *boundary, /*@keep@*/ /*@null@*/ yasm_expr *fill,
  336. /*@keep@*/ /*@null@*/ yasm_expr *maxskip,
  337. /*@null@*/ const unsigned char **code_fill, unsigned long line);
  338. /** Create a bytecode that puts the following bytecode at a fixed section
  339. * offset.
  340. * \param start section offset of following bytecode
  341. * \param fill fill value
  342. * \param line virtual line (from yasm_linemap)
  343. * \return Newly allocated bytecode.
  344. */
  345. YASM_LIB_DECL
  346. /*@only@*/ yasm_bytecode *yasm_bc_create_org
  347. (unsigned long start, unsigned long fill, unsigned long line);
  348. /** Get the section that contains a particular bytecode.
  349. * \param bc bytecode
  350. * \return Section containing bc (can be NULL if bytecode is not part of a
  351. * section).
  352. */
  353. YASM_LIB_DECL
  354. /*@dependent@*/ /*@null@*/ yasm_section *yasm_bc_get_section
  355. (yasm_bytecode *bc);
  356. /** Add to the list of symrecs that reference a bytecode. For symrec use
  357. * only.
  358. * \param bc bytecode
  359. * \param sym symbol
  360. */
  361. YASM_LIB_DECL
  362. void yasm_bc__add_symrec(yasm_bytecode *bc, /*@dependent@*/ yasm_symrec *sym);
  363. /** Delete (free allocated memory for) a bytecode.
  364. * \param bc bytecode (only pointer to it); may be NULL
  365. */
  366. YASM_LIB_DECL
  367. void yasm_bc_destroy(/*@only@*/ /*@null@*/ yasm_bytecode *bc);
  368. /** Print a bytecode. For debugging purposes.
  369. * \param f file
  370. * \param indent_level indentation level
  371. * \param bc bytecode
  372. */
  373. YASM_LIB_DECL
  374. void yasm_bc_print(const yasm_bytecode *bc, FILE *f, int indent_level);
  375. /** Finalize a bytecode after parsing.
  376. * \param bc bytecode
  377. * \param prev_bc bytecode directly preceding bc in a list of bytecodes
  378. */
  379. YASM_LIB_DECL
  380. void yasm_bc_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc);
  381. /** Determine the distance between the starting offsets of two bytecodes.
  382. * \param precbc1 preceding bytecode to the first bytecode
  383. * \param precbc2 preceding bytecode to the second bytecode
  384. * \return Distance in bytes between the two bytecodes (bc2-bc1), or NULL if
  385. * the distance was indeterminate.
  386. * \warning Only valid /after/ optimization.
  387. */
  388. YASM_LIB_DECL
  389. /*@null@*/ /*@only@*/ yasm_intnum *yasm_calc_bc_dist
  390. (yasm_bytecode *precbc1, yasm_bytecode *precbc2);
  391. /** Get the offset of the next bytecode (the next bytecode doesn't have to
  392. * actually exist).
  393. * \param precbc preceding bytecode
  394. * \return Offset of the next bytecode in bytes.
  395. * \warning Only valid /after/ optimization.
  396. */
  397. YASM_LIB_DECL
  398. unsigned long yasm_bc_next_offset(yasm_bytecode *precbc);
  399. /** Return elemens size of a data bytecode.
  400. * Returns the size of each elements of a data bytecode, for proper dereference
  401. * of symbols attached to it.
  402. * \param bc bytecode
  403. * \return 0 if element size is unknown
  404. */
  405. YASM_LIB_DECL
  406. int yasm_bc_elem_size(yasm_bytecode *bc);
  407. /** Resolve EQUs in a bytecode and calculate its minimum size.
  408. * Generates dependent bytecode spans for cases where, if the length spanned
  409. * increases, it could cause the bytecode size to increase.
  410. * Any bytecode multiple is NOT included in the length or spans generation;
  411. * this must be handled at a higher level.
  412. * \param bc bytecode
  413. * \param add_span function to call to add a span
  414. * \param add_span_data extra data to be passed to add_span function
  415. * \return 0 if no error occurred, nonzero if there was an error recognized
  416. * (and output) during execution.
  417. * \note May store to bytecode updated expressions and the short length.
  418. */
  419. YASM_LIB_DECL
  420. int yasm_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span,
  421. void *add_span_data);
  422. /** Recalculate a bytecode's length based on an expanded span length.
  423. * \param bc bytecode
  424. * \param span span ID (as given to yasm_bc_add_span_func in
  425. * yasm_bc_calc_len)
  426. * \param old_val previous span value
  427. * \param new_val new span value
  428. * \param neg_thres negative threshold for long/short decision (returned)
  429. * \param pos_thres positive threshold for long/short decision (returned)
  430. * \return 0 if bc no longer dependent on this span's length, negative if
  431. * there was an error recognized (and output) during execution, and
  432. * positive if bc size may increase for this span further based on the
  433. * new negative and positive thresholds returned.
  434. * \note May store to bytecode updated expressions and the updated length.
  435. */
  436. YASM_LIB_DECL
  437. int yasm_bc_expand(yasm_bytecode *bc, int span, long old_val, long new_val,
  438. /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres);
  439. /** Convert a bytecode into its byte representation.
  440. * \param bc bytecode
  441. * \param buf byte representation destination buffer
  442. * \param bufsize size of buf (in bytes) prior to call; size of the
  443. * generated data after call
  444. * \param gap if nonzero, indicates the data does not really need to
  445. * exist in the object file; if nonzero, contents of buf
  446. * are undefined [output]
  447. * \param d data to pass to each call to output_value/output_reloc
  448. * \param output_value function to call to convert values into their byte
  449. * representation
  450. * \param output_reloc function to call to output relocation entries
  451. * for a single sym
  452. * \return Newly allocated buffer that should be used instead of buf for
  453. * reading the byte representation, or NULL if buf was big enough to
  454. * hold the entire byte representation.
  455. * \note Calling twice on the same bytecode may \em not produce the same
  456. * results on the second call, as calling this function may result in
  457. * non-reversible changes to the bytecode.
  458. */
  459. YASM_LIB_DECL
  460. /*@null@*/ /*@only@*/ unsigned char *yasm_bc_tobytes
  461. (yasm_bytecode *bc, unsigned char *buf, unsigned long *bufsize,
  462. /*@out@*/ int *gap, void *d, yasm_output_value_func output_value,
  463. /*@null@*/ yasm_output_reloc_func output_reloc)
  464. /*@sets *buf@*/;
  465. /** Get the bytecode multiple value as an integer.
  466. * \param bc bytecode
  467. * \param multiple multiple value (output)
  468. * \param calc_bc_dist nonzero if distances between bytecodes should be
  469. * calculated, 0 if error should be returned in this case
  470. * \return 1 on error (set with yasm_error_set), 0 on success.
  471. */
  472. YASM_LIB_DECL
  473. int yasm_bc_get_multiple(yasm_bytecode *bc, /*@out@*/ long *multiple,
  474. int calc_bc_dist);
  475. /** Get the bytecode multiple value as an expression.
  476. * \param bc bytecode
  477. * \return Bytecode multiple, NULL if =1.
  478. */
  479. YASM_LIB_DECL
  480. const yasm_expr *yasm_bc_get_multiple_expr(const yasm_bytecode *bc);
  481. /** Get a #yasm_insn structure from an instruction bytecode (if possible).
  482. * \param bc bytecode
  483. * \return Instruction details if bytecode is an instruction bytecode,
  484. * otherwise NULL.
  485. */
  486. YASM_LIB_DECL
  487. /*@dependent@*/ /*@null@*/ yasm_insn *yasm_bc_get_insn(yasm_bytecode *bc);
  488. /** Create a new data value from an expression.
  489. * \param expn expression
  490. * \return Newly allocated data value.
  491. */
  492. YASM_LIB_DECL
  493. yasm_dataval *yasm_dv_create_expr(/*@keep@*/ yasm_expr *expn);
  494. /** Create a new data value from a string.
  495. * \param contents string (may contain NULs)
  496. * \param len length of string
  497. * \return Newly allocated data value.
  498. */
  499. YASM_LIB_DECL
  500. yasm_dataval *yasm_dv_create_string(/*@keep@*/ char *contents, size_t len);
  501. /** Create a new data value from raw bytes data.
  502. * \param contents raw data (may contain NULs)
  503. * \param len length
  504. * \return Newly allocated data value.
  505. */
  506. YASM_LIB_DECL
  507. yasm_dataval *yasm_dv_create_raw(/*@keep@*/ unsigned char *contents,
  508. unsigned long len);
  509. /** Create a new uninitialized data value.
  510. * \return Newly allocated data value.
  511. */
  512. YASM_LIB_DECL
  513. yasm_dataval *yasm_dv_create_reserve(void);
  514. #ifndef YASM_DOXYGEN
  515. #define yasm_dv_create_string(s, l) yasm_dv_create_raw((unsigned char *)(s), \
  516. (unsigned long)(l))
  517. #endif
  518. /** Get the underlying value of a data value.
  519. * \param dv data value
  520. * \return Value, or null if non-value (e.g. string or raw).
  521. */
  522. YASM_LIB_DECL
  523. yasm_value *yasm_dv_get_value(yasm_dataval *dv);
  524. /** Set multiple field of a data value.
  525. * A data value can be repeated a number of times when output. This function
  526. * sets that multiple.
  527. * \param dv data value
  528. * \param e multiple (kept, do not free)
  529. */
  530. YASM_LIB_DECL
  531. void yasm_dv_set_multiple(yasm_dataval *dv, /*@keep@*/ yasm_expr *e);
  532. /** Get the data value multiple value as an unsigned long integer.
  533. * \param dv data value
  534. * \param multiple multiple value (output)
  535. * \return 1 on error (set with yasm_error_set), 0 on success.
  536. */
  537. YASM_LIB_DECL
  538. int yasm_dv_get_multiple(yasm_dataval *dv, /*@out@*/ unsigned long *multiple);
  539. /** Initialize a list of data values.
  540. * \param headp list of data values
  541. */
  542. void yasm_dvs_initialize(yasm_datavalhead *headp);
  543. #ifndef YASM_DOXYGEN
  544. #define yasm_dvs_initialize(headp) STAILQ_INIT(headp)
  545. #endif
  546. /** Delete (free allocated memory for) a list of data values.
  547. * \param headp list of data values
  548. */
  549. YASM_LIB_DECL
  550. void yasm_dvs_delete(yasm_datavalhead *headp);
  551. /** Add data value to the end of a list of data values.
  552. * \note Does not make a copy of the data value; so don't pass this function
  553. * static or local variables, and discard the dv pointer after calling
  554. * this function.
  555. * \param headp data value list
  556. * \param dv data value (may be NULL)
  557. * \return If data value was actually appended (it wasn't NULL), the data
  558. * value; otherwise NULL.
  559. */
  560. YASM_LIB_DECL
  561. /*@null@*/ yasm_dataval *yasm_dvs_append
  562. (yasm_datavalhead *headp, /*@returned@*/ /*@null@*/ yasm_dataval *dv);
  563. /** Print a data value list. For debugging purposes.
  564. * \param f file
  565. * \param indent_level indentation level
  566. * \param headp data value list
  567. */
  568. YASM_LIB_DECL
  569. void yasm_dvs_print(const yasm_datavalhead *headp, FILE *f, int indent_level);
  570. #endif