asm-glue.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include <stdio.h>
  2. #include "scheme48.h"
  3. static s48_value s48_provide_asm_values(s48_value asm_vector);
  4. static s48_value s48_malloc_byte_vector(s48_value length);
  5. static s48_value s48_stob_start_address(s48_value stob);
  6. static s48_value s48_free_byte_vector(s48_value byte_vector);
  7. void
  8. s48_init_asm_glue(void)
  9. {
  10. S48_EXPORT_FUNCTION(s48_provide_asm_values);
  11. S48_EXPORT_FUNCTION(s48_malloc_byte_vector);
  12. S48_EXPORT_FUNCTION(s48_stob_start_address);
  13. S48_EXPORT_FUNCTION(s48_free_byte_vector);
  14. }
  15. static s48_value
  16. s48_provide_asm_values(s48_value asm_vector)
  17. {
  18. extern void s48_stack_gc();
  19. extern long s48_unknown_call();
  20. extern long s48_unknown_return();
  21. extern long s48_interrupt_handler();
  22. extern long s48_exception_handler();
  23. extern long s48_restart_vm();
  24. extern long s48_gc_for_native_code();
  25. extern long s48_native_add();
  26. extern long s48_native_sub();
  27. extern long s48_native_mul();
  28. extern long s48_native_E();
  29. extern long s48_native_L();
  30. extern long s48_native_G();
  31. extern long s48_native_LE();
  32. extern long s48_native_GE();
  33. extern long s48_native_bitwise_not;
  34. extern long s48_native_bit_count;
  35. extern long s48_native_bitwise_and;
  36. extern long s48_native_bitwise_ior;
  37. extern long s48_native_bitwise_xor;
  38. extern long s48_restart_vm2();
  39. extern long s48_Sstack_limitS;
  40. extern long s48_ShpS;
  41. extern long s48_SlimitS;
  42. /* The order of the vector has to match the enumatation asm-external in
  43. s48-compiler/asm-externals.scm */
  44. /* 0 *val* */
  45. /* 1 *cont* */
  46. /* 2 *stack* */
  47. S48_VECTOR_SET(asm_vector, 3, s48_enter_fixnum((long) &s48_Sstack_limitS));
  48. S48_VECTOR_SET(asm_vector, 4, s48_enter_fixnum((long) &s48_stack_gc));
  49. S48_VECTOR_SET(asm_vector, 5, s48_enter_fixnum((long) &s48_ShpS));
  50. S48_VECTOR_SET(asm_vector, 6, s48_enter_fixnum((long) &s48_SlimitS));
  51. S48_VECTOR_SET(asm_vector, 7, s48_enter_fixnum((long) &s48_unknown_call));
  52. S48_VECTOR_SET(asm_vector, 8, s48_enter_fixnum((long) &s48_unknown_return));
  53. S48_VECTOR_SET(asm_vector, 9, s48_enter_fixnum((long) &s48_interrupt_handler));
  54. S48_VECTOR_SET(asm_vector, 10, s48_enter_fixnum((long) &s48_exception_handler));
  55. S48_VECTOR_SET(asm_vector, 11, s48_enter_fixnum((long) &s48_restart_vm));
  56. S48_VECTOR_SET(asm_vector, 12, s48_enter_fixnum((long) &s48_gc_for_native_code));
  57. S48_VECTOR_SET(asm_vector, 13, s48_enter_fixnum((long) &s48_native_add));
  58. S48_VECTOR_SET(asm_vector, 14, s48_enter_fixnum((long) &s48_native_sub));
  59. S48_VECTOR_SET(asm_vector, 15, s48_enter_fixnum((long) &s48_native_mul));
  60. S48_VECTOR_SET(asm_vector, 16, s48_enter_fixnum((long) &s48_native_E));
  61. S48_VECTOR_SET(asm_vector, 17, s48_enter_fixnum((long) &s48_native_L));
  62. S48_VECTOR_SET(asm_vector, 18, s48_enter_fixnum((long) &s48_native_G));
  63. S48_VECTOR_SET(asm_vector, 19, s48_enter_fixnum((long) &s48_native_LE));
  64. S48_VECTOR_SET(asm_vector, 20, s48_enter_fixnum((long) &s48_native_GE));
  65. S48_VECTOR_SET(asm_vector, 21, s48_enter_fixnum((long) &s48_native_bitwise_not));
  66. S48_VECTOR_SET(asm_vector, 22, s48_enter_fixnum((long) &s48_native_bit_count));
  67. S48_VECTOR_SET(asm_vector, 23, s48_enter_fixnum((long) &s48_native_bitwise_and));
  68. S48_VECTOR_SET(asm_vector, 24, s48_enter_fixnum((long) &s48_native_bitwise_ior));
  69. S48_VECTOR_SET(asm_vector, 25, s48_enter_fixnum((long) &s48_native_bitwise_xor));
  70. S48_VECTOR_SET(asm_vector, 26, s48_enter_fixnum((long) &s48_restart_vm2));
  71. return S48_UNSPECIFIC;
  72. }
  73. int
  74. s48_is_integer_or_flonum(s48_value thing)
  75. {
  76. return (S48_FIXNUM_P (thing) || S48_BIGNUM_P (thing) || S48_DOUBLE_P (thing));
  77. }
  78. /*
  79. * Make a byte-vector that is outside the heap (and thus won't be moved (or
  80. * freed) by the GC).
  81. */
  82. static s48_value
  83. s48_malloc_byte_vector(s48_value length)
  84. {
  85. int c_length = s48_extract_fixnum(length);
  86. int bytes = (c_length + 4 + 3) & -4; /* space for header + round up */
  87. char *bv = (char *)malloc(bytes);
  88. if (bv == NULL)
  89. s48_raise_out_of_memory_error();
  90. *((long *) bv) = (c_length << 8)
  91. | (S48_STOBTYPE_BYTE_VECTOR << 2)
  92. | S48_HEADER_TAG;
  93. return (s48_value) ((((long) bv) + 4) | S48_STOB_TAG);
  94. }
  95. /*
  96. * Free up a malloc'ed byte vector.
  97. */
  98. static s48_value
  99. s48_free_byte_vector(s48_value byte_vector)
  100. {
  101. if (!S48_BYTE_VECTOR_P(byte_vector))
  102. s48_raise_argument_type_error(byte_vector);
  103. free((void *) ((byte_vector & -4)- 4));
  104. return S48_UNSPECIFIC;
  105. }
  106. /*
  107. * The assembler needs to be able to get the start address of a stored
  108. * object.
  109. */
  110. static s48_value
  111. s48_stob_start_address(s48_value stob)
  112. {
  113. if (!S48_STOB_P(stob))
  114. s48_raise_argument_type_error(stob);
  115. return s48_enter_integer((long) S48_ADDRESS_AFTER_HEADER(stob, void));
  116. }
  117. s48_value
  118. s48_is_integer_or_floanum(s48_value value)
  119. {
  120. return (S48_FIXNUM_P (value) || S48_BIGNUM_P (value) || S48_DOUBLE_P (value));
  121. }
  122. s48_value
  123. s48_are_integers_or_floanums(s48_value value1, s48_value value2)
  124. {
  125. return (((S48_FIXNUM_P (value1) || S48_BIGNUM_P (value1)) &&
  126. (S48_FIXNUM_P (value2) || S48_BIGNUM_P (value2))) ||
  127. ((S48_DOUBLE_P (value1) && S48_DOUBLE_P (value2))));
  128. }
  129. s48_value
  130. s48_is_integer(s48_value value)
  131. {
  132. return (S48_FIXNUM_P (value) || S48_BIGNUM_P (value));
  133. }
  134. s48_value
  135. s48_are_integers(s48_value value1, s48_value value2)
  136. {
  137. return (((S48_FIXNUM_P (value1) || S48_BIGNUM_P (value1)) &&
  138. (S48_FIXNUM_P (value2) || S48_BIGNUM_P (value2))));
  139. }
  140. long ignore_values_native_protocol = 186;
  141. long jmp_count = 10; /* just a guess: jmp continue */
  142. long first_opcode_index = 13; /* from vm/package-defs.scm */
  143. long
  144. s48_make_native_return_code(char* jmp_to_continue, long frame_size)
  145. {
  146. long return_code, i;
  147. return_code = s48_make_blank_return_code(ignore_values_native_protocol, frame_size, jmp_count);
  148. for (i=0; i < jmp_count; i++)
  149. S48_BYTE_VECTOR_SET(return_code, i + first_opcode_index, jmp_to_continue[i]);
  150. fprintf (stderr, "Generated return code at %d (%x) from %d with fsize:%d\n", return_code, return_code, (long) jmp_to_continue, frame_size);
  151. return return_code;
  152. }