exprlib.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 1996-2017 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. * exprlib.c
  35. *
  36. * Library routines to manipulate expression data types.
  37. */
  38. #include "nasm.h"
  39. /*
  40. * Return true if the argument is a simple scalar. (Or a far-
  41. * absolute, which counts.)
  42. */
  43. bool is_simple(const expr *vect)
  44. {
  45. while (vect->type && !vect->value)
  46. vect++;
  47. if (!vect->type)
  48. return true;
  49. if (vect->type != EXPR_SIMPLE)
  50. return false;
  51. do {
  52. vect++;
  53. } while (vect->type && !vect->value);
  54. if (vect->type && vect->type < EXPR_SEGBASE + SEG_ABS)
  55. return false;
  56. return true;
  57. }
  58. /*
  59. * Return true if the argument is a simple scalar, _NOT_ a far-
  60. * absolute.
  61. */
  62. bool is_really_simple(const expr *vect)
  63. {
  64. while (vect->type && !vect->value)
  65. vect++;
  66. if (!vect->type)
  67. return true;
  68. if (vect->type != EXPR_SIMPLE)
  69. return false;
  70. do {
  71. vect++;
  72. } while (vect->type && !vect->value);
  73. if (vect->type)
  74. return false;
  75. return true;
  76. }
  77. /*
  78. * Return true if the argument is relocatable (i.e. a simple
  79. * scalar, plus at most one segment-base, possibly a subtraction
  80. * of the current segment base, plus possibly a WRT).
  81. */
  82. bool is_reloc(const expr *vect)
  83. {
  84. bool has_rel = false; /* Has a self-segment-subtract */
  85. bool has_seg = false; /* Has a segment base */
  86. for (; vect->type; vect++) {
  87. if (!vect->value) {
  88. /* skip value-0 terms */
  89. continue;
  90. } else if (vect->type < EXPR_SIMPLE) {
  91. /* false if a register is present */
  92. return false;
  93. } else if (vect->type == EXPR_SIMPLE) {
  94. /* skip over a pure number term... */
  95. continue;
  96. } else if (vect->type == EXPR_WRT) {
  97. /* skip over a WRT term... */
  98. continue;
  99. } else if (vect->type < EXPR_SEGBASE) {
  100. /* other special type -> problem */
  101. return false;
  102. } else if (vect->value == 1) {
  103. if (has_seg)
  104. return false; /* only one segbase allowed */
  105. has_seg = true;
  106. } else if (vect->value == -1) {
  107. if (vect->type != location.segment + EXPR_SEGBASE)
  108. return false; /* can only subtract current segment */
  109. if (has_rel)
  110. return false; /* already is relative */
  111. has_rel = true;
  112. }
  113. }
  114. return true;
  115. }
  116. /*
  117. * Return true if the argument contains an `unknown' part.
  118. */
  119. bool is_unknown(const expr *vect)
  120. {
  121. while (vect->type && vect->type < EXPR_UNKNOWN)
  122. vect++;
  123. return (vect->type == EXPR_UNKNOWN);
  124. }
  125. /*
  126. * Return true if the argument contains nothing but an `unknown'
  127. * part.
  128. */
  129. bool is_just_unknown(const expr *vect)
  130. {
  131. while (vect->type && !vect->value)
  132. vect++;
  133. return (vect->type == EXPR_UNKNOWN);
  134. }
  135. /*
  136. * Return the scalar part of a relocatable vector. (Including
  137. * simple scalar vectors - those qualify as relocatable.)
  138. */
  139. int64_t reloc_value(const expr *vect)
  140. {
  141. while (vect->type && !vect->value)
  142. vect++;
  143. if (!vect->type)
  144. return 0;
  145. if (vect->type == EXPR_SIMPLE)
  146. return vect->value;
  147. else
  148. return 0;
  149. }
  150. /*
  151. * Return the segment number of a relocatable vector, or NO_SEG for
  152. * simple scalars.
  153. */
  154. int32_t reloc_seg(const expr *vect)
  155. {
  156. for (; vect->type; vect++) {
  157. if (vect->type >= EXPR_SEGBASE && vect->value == 1)
  158. return vect->type - EXPR_SEGBASE;
  159. }
  160. return NO_SEG;
  161. }
  162. /*
  163. * Return the WRT segment number of a relocatable vector, or NO_SEG
  164. * if no WRT part is present.
  165. */
  166. int32_t reloc_wrt(const expr *vect)
  167. {
  168. while (vect->type && vect->type < EXPR_WRT)
  169. vect++;
  170. if (vect->type == EXPR_WRT) {
  171. return vect->value;
  172. } else
  173. return NO_SEG;
  174. }
  175. /*
  176. * Return true if this expression contains a subtraction of the location
  177. */
  178. bool is_self_relative(const expr *vect)
  179. {
  180. for (; vect->type; vect++) {
  181. if (vect->type == location.segment + EXPR_SEGBASE && vect->value == -1)
  182. return true;
  183. }
  184. return false;
  185. }