asm-glue.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Part of Scheme 48 1.9. See file COPYING for notices and license.
  3. *
  4. * Authors: Richard Kelsey, Jonathan Rees, Martin Gasbichler, Mike Sperber
  5. */
  6. #include <stdio.h>
  7. #include "scheme48.h"
  8. static s48_value s48_provide_asm_values(s48_value asm_vector);
  9. static s48_value s48_malloc_byte_vector(s48_value length);
  10. static s48_value s48_stob_start_address(s48_value stob);
  11. static s48_value s48_free_byte_vector(s48_value byte_vector);
  12. void
  13. s48_init_asm_glue(void)
  14. {
  15. S48_EXPORT_FUNCTION(s48_provide_asm_values);
  16. S48_EXPORT_FUNCTION(s48_malloc_byte_vector);
  17. S48_EXPORT_FUNCTION(s48_stob_start_address);
  18. S48_EXPORT_FUNCTION(s48_free_byte_vector);
  19. }
  20. static s48_value
  21. s48_provide_asm_values(s48_value asm_vector)
  22. {
  23. extern long s48_unknown_call();
  24. extern long s48_unknown_return();
  25. extern long s48_unknown_return_values();
  26. extern long s48_unknown_apply();
  27. extern long s48_interrupt_handler();
  28. extern long s48_restart_vm();
  29. extern long s48_ensure_space_for_native_code();
  30. extern long s48_native_add();
  31. extern long s48_native_sub();
  32. extern long s48_native_mul();
  33. extern long s48_native_E();
  34. extern long s48_native_L();
  35. extern long s48_native_G();
  36. extern long s48_native_LE();
  37. extern long s48_native_GE();
  38. extern long s48_native_remainder();
  39. extern long s48_native_quotient();
  40. extern long s48_native_divide();
  41. extern long s48_native_bitwise_not;
  42. extern long s48_native_bit_count;
  43. extern long s48_native_bitwise_and;
  44. extern long s48_native_bitwise_ior;
  45. extern long s48_native_bitwise_xor;
  46. extern long s48_restart_vm3_pop_0;
  47. extern long s48_restart_vm3_pop_1;
  48. extern long s48_restart_vm3_pop_2;
  49. extern long s48_restart_vm3_pop_3;
  50. extern long s48_gcSallocate_for_native_code;
  51. extern long s48_Sstack_limitS;
  52. extern long s48_ShpS;
  53. extern long s48_SlimitS;
  54. extern char* ScontS;
  55. /* The order of the vector has to match the enumatation asm-external in
  56. s48-compiler/assembler/asm-externals.scm */
  57. /* 0 *val* */
  58. S48_VECTOR_SET(asm_vector, 1, s48_enter_fixnum((long) &ScontS));
  59. /* 2 *stack* */
  60. S48_VECTOR_SET(asm_vector, 3, s48_enter_fixnum((long) &s48_Sstack_limitS));
  61. S48_VECTOR_SET(asm_vector, 4, s48_enter_fixnum((long) &s48_ShpS));
  62. S48_VECTOR_SET(asm_vector, 5, s48_enter_fixnum((long) &s48_SlimitS));
  63. S48_VECTOR_SET(asm_vector, 6, s48_enter_fixnum((long) &s48_unknown_call));
  64. S48_VECTOR_SET(asm_vector, 7, s48_enter_fixnum((long) &s48_unknown_return));
  65. S48_VECTOR_SET(asm_vector, 8, s48_enter_fixnum((long) &s48_unknown_return_values));
  66. S48_VECTOR_SET(asm_vector, 9, s48_enter_fixnum((long) &s48_interrupt_handler));
  67. S48_VECTOR_SET(asm_vector, 10, s48_enter_fixnum((long) &s48_restart_vm));
  68. S48_VECTOR_SET(asm_vector, 11, s48_enter_fixnum((long) &s48_ensure_space_for_native_code));
  69. S48_VECTOR_SET(asm_vector, 12, s48_enter_fixnum((long) &s48_native_add));
  70. S48_VECTOR_SET(asm_vector, 13, s48_enter_fixnum((long) &s48_native_sub));
  71. S48_VECTOR_SET(asm_vector, 14, s48_enter_fixnum((long) &s48_native_mul));
  72. S48_VECTOR_SET(asm_vector, 15, s48_enter_fixnum((long) &s48_native_E));
  73. S48_VECTOR_SET(asm_vector, 16, s48_enter_fixnum((long) &s48_native_L));
  74. S48_VECTOR_SET(asm_vector, 17, s48_enter_fixnum((long) &s48_native_G));
  75. S48_VECTOR_SET(asm_vector, 18, s48_enter_fixnum((long) &s48_native_LE));
  76. S48_VECTOR_SET(asm_vector, 19, s48_enter_fixnum((long) &s48_native_GE));
  77. S48_VECTOR_SET(asm_vector, 20, s48_enter_fixnum((long) &s48_native_remainder));
  78. S48_VECTOR_SET(asm_vector, 21, s48_enter_fixnum((long) &s48_native_quotient));
  79. S48_VECTOR_SET(asm_vector, 22, s48_enter_fixnum((long) &s48_native_divide));
  80. S48_VECTOR_SET(asm_vector, 23, s48_enter_fixnum((long) &s48_native_bitwise_not));
  81. S48_VECTOR_SET(asm_vector, 24, s48_enter_fixnum((long) &s48_native_bit_count));
  82. S48_VECTOR_SET(asm_vector, 25, s48_enter_fixnum((long) &s48_native_bitwise_and));
  83. S48_VECTOR_SET(asm_vector, 26, s48_enter_fixnum((long) &s48_native_bitwise_ior));
  84. S48_VECTOR_SET(asm_vector, 27, s48_enter_fixnum((long) &s48_native_bitwise_xor));
  85. S48_VECTOR_SET(asm_vector, 28, s48_enter_fixnum((long) &s48_restart_vm3_pop_0));
  86. S48_VECTOR_SET(asm_vector, 29, s48_enter_fixnum((long) &s48_restart_vm3_pop_1));
  87. S48_VECTOR_SET(asm_vector, 30, s48_enter_fixnum((long) &s48_restart_vm3_pop_2));
  88. S48_VECTOR_SET(asm_vector, 31, s48_enter_fixnum((long) &s48_restart_vm3_pop_3));
  89. S48_VECTOR_SET(asm_vector, 32, s48_enter_fixnum((long) &s48_gcSallocate_for_native_code));
  90. /* 34 current thread */
  91. S48_VECTOR_SET(asm_vector, 33, s48_enter_fixnum((long) &s48_unknown_apply));
  92. return S48_UNSPECIFIC;
  93. }
  94. int
  95. s48_is_integer_or_flonum(s48_value thing)
  96. {
  97. return (S48_FIXNUM_P (thing) || S48_BIGNUM_P (thing) || S48_DOUBLE_P (thing));
  98. }
  99. /*
  100. * Make a byte-vector that is outside the heap (and thus won't be moved (or
  101. * freed) by the GC).
  102. */
  103. static s48_value
  104. s48_malloc_byte_vector(s48_value length)
  105. {
  106. int c_length = s48_extract_fixnum(length);
  107. int bytes = (c_length + 4 + 3) & -4; /* space for header + round up */
  108. char *bv = (char *)malloc(bytes);
  109. if (bv == NULL)
  110. s48_out_of_memory_error();
  111. *((long *) bv) = (c_length << 8)
  112. | (S48_STOBTYPE_BYTE_VECTOR << 2)
  113. | S48_HEADER_TAG;
  114. return (s48_value) ((((long) bv) + 4) | S48_STOB_TAG);
  115. }
  116. /*
  117. * Free up a malloc'ed byte vector.
  118. */
  119. static s48_value
  120. s48_free_byte_vector(s48_value byte_vector)
  121. {
  122. if (!S48_BYTE_VECTOR_P(byte_vector))
  123. s48_assertion_violation("s48_free_byte_vector", "not a byte vector", 1, byte_vector);
  124. free((void *) ((byte_vector & -4)- 4));
  125. return S48_UNSPECIFIC;
  126. }
  127. /*
  128. * The assembler needs to be able to get the start address of a stored
  129. * object.
  130. */
  131. static s48_value
  132. s48_stob_start_address(s48_value stob)
  133. {
  134. if (!S48_STOB_P(stob))
  135. s48_assertion_violation("s48_stob_start_address", "not a stob", 1, stob);
  136. return s48_enter_integer((long) S48_ADDRESS_AFTER_HEADER(stob, void));
  137. }
  138. s48_value
  139. s48_is_integer_or_floanum(s48_value value)
  140. {
  141. return (S48_FIXNUM_P (value) || S48_BIGNUM_P (value) || S48_DOUBLE_P (value));
  142. }
  143. s48_value
  144. s48_are_integers_or_floanums(s48_value value1, s48_value value2)
  145. {
  146. return (((S48_FIXNUM_P (value1) || S48_BIGNUM_P (value1)) &&
  147. (S48_FIXNUM_P (value2) || S48_BIGNUM_P (value2))) ||
  148. ((S48_DOUBLE_P (value1) && S48_DOUBLE_P (value2))));
  149. }
  150. s48_value
  151. s48_is_integer(s48_value value)
  152. {
  153. return (S48_FIXNUM_P (value) || S48_BIGNUM_P (value));
  154. }
  155. s48_value
  156. s48_are_integers(s48_value value1, s48_value value2)
  157. {
  158. return (((S48_FIXNUM_P (value1) || S48_BIGNUM_P (value1)) &&
  159. (S48_FIXNUM_P (value2) || S48_BIGNUM_P (value2))));
  160. }
  161. s48_value
  162. s48_integer_divide_help(s48_value value1, s48_value value2)
  163. {
  164. s48_value quot,rem;
  165. s48_value div_by_zeroP;
  166. div_by_zeroP = s48_integer_divide (value1, value2, &quot, &rem);
  167. /* native code should check div_by_zeroP */
  168. if (rem == s48_enter_fixnum (0))
  169. return quot;
  170. else return S48_FALSE;
  171. }
  172. long ignore_values_native_protocol = 194; /* ignore-values-native-protocol */
  173. long jmp_count = 7; /* movl continue %ebx; jmp *ebx */
  174. long first_opcode_index = 15; /* from vm/package-defs.scm */
  175. extern long Snative_exception_contS;
  176. void
  177. s48_make_native_return_code(int n_stack_args)
  178. {
  179. long return_code, i,target;
  180. char frame_size;
  181. extern char* ScontS;
  182. extern char* SstackS;
  183. target = Snative_exception_contS;
  184. frame_size = ScontS - SstackS;
  185. frame_size = frame_size >> 2; /* bytes -> cells */
  186. frame_size -= n_stack_args;
  187. return_code = s48_make_blank_return_code(ignore_values_native_protocol, 0xffff, frame_size, jmp_count);
  188. S48_BYTE_VECTOR_SET(return_code,first_opcode_index,0xbb); /* movl %ebx */
  189. S48_BYTE_VECTOR_SET(return_code,first_opcode_index+1,target & 0xff);
  190. S48_BYTE_VECTOR_SET(return_code,first_opcode_index+2,(target >> 8) & 0xff);
  191. S48_BYTE_VECTOR_SET(return_code,first_opcode_index+3,(target >> 16) & 0xff);
  192. S48_BYTE_VECTOR_SET(return_code,first_opcode_index+4,(target >> 24) & 0xff);
  193. S48_BYTE_VECTOR_SET(return_code,first_opcode_index+5,0xff); /* jmp */
  194. S48_BYTE_VECTOR_SET(return_code,first_opcode_index+6,0xe3); /* ebx */
  195. Snative_exception_contS =
  196. (return_code - 3) /* remove stob tag */
  197. + first_opcode_index
  198. - 2; /* pointer to protocol instruction */
  199. }
  200. void
  201. s48_write_fatal_message(char* msg, int size, int bc_pc){
  202. fprintf(stderr, "s48_write_fatal_message called with bc-pc %d\n", bc_pc);
  203. write(2, msg, size);
  204. fprintf(stderr, "s48_write_fatal_message put out\n");
  205. exit(1);
  206. return;
  207. }