bc-reserve.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Bytecode utility functions
  3. *
  4. * Copyright (C) 2001-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. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "util.h"
  28. #include "libyasm-stdint.h"
  29. #include "coretype.h"
  30. #include "errwarn.h"
  31. #include "intnum.h"
  32. #include "expr.h"
  33. #include "value.h"
  34. #include "bytecode.h"
  35. typedef struct bytecode_reserve {
  36. /*@only@*/ /*@null@*/ yasm_expr *numitems; /* number of items to reserve */
  37. unsigned int itemsize; /* size of each item (in bytes) */
  38. } bytecode_reserve;
  39. static void bc_reserve_destroy(void *contents);
  40. static void bc_reserve_print(const void *contents, FILE *f, int indent_level);
  41. static void bc_reserve_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc);
  42. static int bc_reserve_elem_size(yasm_bytecode *bc);
  43. static int bc_reserve_calc_len(yasm_bytecode *bc,
  44. yasm_bc_add_span_func add_span,
  45. void *add_span_data);
  46. static int bc_reserve_tobytes(yasm_bytecode *bc, unsigned char **bufp,
  47. unsigned char *bufstart, void *d,
  48. yasm_output_value_func output_value,
  49. /*@null@*/ yasm_output_reloc_func output_reloc);
  50. static const yasm_bytecode_callback bc_reserve_callback = {
  51. bc_reserve_destroy,
  52. bc_reserve_print,
  53. bc_reserve_finalize,
  54. bc_reserve_elem_size,
  55. bc_reserve_calc_len,
  56. yasm_bc_expand_common,
  57. bc_reserve_tobytes,
  58. YASM_BC_SPECIAL_RESERVE
  59. };
  60. static void
  61. bc_reserve_destroy(void *contents)
  62. {
  63. bytecode_reserve *reserve = (bytecode_reserve *)contents;
  64. yasm_expr_destroy(reserve->numitems);
  65. yasm_xfree(contents);
  66. }
  67. static void
  68. bc_reserve_print(const void *contents, FILE *f, int indent_level)
  69. {
  70. const bytecode_reserve *reserve = (const bytecode_reserve *)contents;
  71. fprintf(f, "%*s_Reserve_\n", indent_level, "");
  72. fprintf(f, "%*sNum Items=", indent_level, "");
  73. yasm_expr_print(reserve->numitems, f);
  74. fprintf(f, "\n%*sItem Size=%u\n", indent_level, "", reserve->itemsize);
  75. }
  76. static void
  77. bc_reserve_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc)
  78. {
  79. bytecode_reserve *reserve = (bytecode_reserve *)bc->contents;
  80. /* multiply reserve expression into multiple */
  81. if (!bc->multiple)
  82. bc->multiple = reserve->numitems;
  83. else
  84. bc->multiple = yasm_expr_create_tree(bc->multiple, YASM_EXPR_MUL,
  85. reserve->numitems, bc->line);
  86. reserve->numitems = NULL;
  87. }
  88. static int
  89. bc_reserve_elem_size(yasm_bytecode *bc)
  90. {
  91. bytecode_reserve *reserve = (bytecode_reserve *)bc->contents;
  92. return reserve->itemsize;
  93. }
  94. static int
  95. bc_reserve_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span,
  96. void *add_span_data)
  97. {
  98. bytecode_reserve *reserve = (bytecode_reserve *)bc->contents;
  99. bc->len += reserve->itemsize;
  100. return 0;
  101. }
  102. static int
  103. bc_reserve_tobytes(yasm_bytecode *bc, unsigned char **bufp,
  104. unsigned char *bufstart, void *d,
  105. yasm_output_value_func output_value,
  106. /*@unused@*/ yasm_output_reloc_func output_reloc)
  107. {
  108. yasm_internal_error(N_("bc_reserve_tobytes called"));
  109. /*@notreached@*/
  110. return 1;
  111. }
  112. yasm_bytecode *
  113. yasm_bc_create_reserve(yasm_expr *numitems, unsigned int itemsize,
  114. unsigned long line)
  115. {
  116. bytecode_reserve *reserve = yasm_xmalloc(sizeof(bytecode_reserve));
  117. /*@-mustfree@*/
  118. reserve->numitems = numitems;
  119. /*@=mustfree@*/
  120. reserve->itemsize = itemsize;
  121. return yasm_bc_create_common(&bc_reserve_callback, reserve, line);
  122. }
  123. const yasm_expr *
  124. yasm_bc_reserve_numitems(yasm_bytecode *bc, unsigned int *itemsize)
  125. {
  126. bytecode_reserve *reserve;
  127. if (bc->callback != &bc_reserve_callback)
  128. return NULL;
  129. reserve = (bytecode_reserve *)bc->contents;
  130. *itemsize = reserve->itemsize;
  131. return reserve->numitems;
  132. }