home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / t / toaster.zip / Compat / STACKMACHINE < prev    next >
Text File  |  1991-12-31  |  4KB  |  140 lines

  1. THIS IS A DOCUMENT THAT DESCRIBES HOW A VIRTUAL STACK MACHINE HAS BEEN
  2. DEFINED, TO EXECUTE COMPILED LPC CODE.
  3.  
  4. There are two stacks:
  5.  
  6. 1.    The stack of values, used for evaluation, local variables and arguments.
  7. Note that arguments are treated as local variables. Every element on the
  8. value stack will have the format "struct svalue", as defined in interpret.h.
  9. The value stack is stored in an array, with limited size. The first push
  10. operation will store a value into element 0. Access of arguments and local
  11. variables are supposed to be fast, by simply indexing in the value stack
  12. using the frame pointer as offset.
  13.  
  14. Start of stack     -----
  15.         |
  16. Frame pointer->    |    Argument number 0
  17.         |    Argument number 1
  18.         |    etc.
  19.         |    Local variable number 0
  20.         |    Local variable number 1
  21.         |    etc.
  22.         |    Temporary stack values
  23.         |    etc
  24.         v
  25.  
  26. 2.    The control stack that contains return addresses, frame pointer etc.
  27.  
  28.  
  29. 1. CALLING LOCAL FUNCTIONS.
  30.  
  31. All arguments are evaluated and pushed to the value stack. The last argument
  32. is the last pushed. It is important that the called function gets exactly as
  33. many arguments as it wants. The number of arguments will be stored in the
  34. control stack, so that the return instruction not needs to know it
  35. explicitely.
  36.  
  37. Instruction format:
  38.  
  39. b0 b1 b2 b3
  40.  
  41. b0 = F_FUNCTION.
  42. b1, b2 = The number of the function to be called.
  43. b3 = Number of arguments sent.
  44.  
  45. The F_FUNCTION instruction will also initiate the frame pointer to point
  46. to the first argument.
  47.  
  48. The number of arguments are stored in the 'struct function' which is found
  49. using the number of the function and indexing in ob->prog->functions[];
  50. The number of arguments will be adjusted to fit the called function.
  51. This is done by either pushing zeroes, or poping excessive
  52. arguments. F_FUNCTION will also initiate local variables, by pusing a 0
  53. for each of them.
  54.  
  55. The called function must ensure that exactly one value remains on the
  56. stack when returning. The caller is responsible of deallocating the
  57. returned value.
  58.  
  59. When a function returns, it will use the instruction F_RETURN, which will
  60. deallocate all arguments and local variables, and only let the top of stack
  61. entry remain. The number of arguments and local variables are stored in the
  62. control stack, so that the evaluator knows hoh much to deallocate.
  63.  
  64. If flag 'extern_call' is set, then the evaluator should return. Otherwise,
  65. the evaluator will continue to execute the instruction at the returned
  66. address.
  67.  
  68. Format:
  69.  
  70. b0
  71.  
  72. b0 = F_RETURN.
  73.  
  74.  
  75. 2. CALLING PREDEFINED FUNCTIONS.
  76.  
  77. Arguments are pushed to the stack. A value is always returned (on the stack).
  78.  
  79. Instruction format:
  80.  
  81. b1
  82.  
  83. b1 = The F_ code of the called function.
  84.  
  85. If a variable number of arguments are allowed, then an extra byte will
  86. follow the instruction, that states number of actual arguments.
  87.  
  88. The execution unit will parse number of arguments immediately, regardless
  89. of which instruction it is when it is stated that a variable number of
  90. arguments are allowed. It will also check soem of the types of the
  91. arguments immediately, if it is possible. But never more than the types of
  92. the first two arguments.
  93.  
  94. 3. F_SSCANF
  95.  
  96. The function sscanf is special, in that arguments are passed by reference.
  97. This is done with a new type, T_LVALUE. The compiler will recognize
  98. sscanf() as a special function, pass the value of the two first arguments
  99. as normal rvalues and pass the rest as lvalues. The total number of arguments
  100. is given as a one byte code supplied to the F_SSCANF instruction.
  101.  
  102. 4. F_CALL_OTHER
  103.  
  104. This command takes one argument, a byte which gives the number of arguments.
  105.  
  106. b1, b2
  107.  
  108. b1 = F_CALL_OTHER, b2 = number of arguments.
  109.  
  110. 5. F_AGGREGATE
  111.  
  112. This command takes one argument, the size of the array. The elements of
  113. the array are picked from the top of stack.
  114.  
  115. b1, b2, b3
  116.  
  117. b1 = F_AGGREGATE, (b2,b3) = Size of the array (max 0xffff).
  118.  
  119. 6. F_CATCH
  120.  
  121. The compiler constructs a call to F_CATCH before the code to evaluate the
  122. argument of F_CATCH. After the code, a call to F_RETURN is made. Thus,
  123. it will look like a function call.
  124.  
  125. F_CATCH will when executed do setjmp() and call eval_instruction()
  126. recursively. That means that a new frame has to be set up.
  127.  
  128. F_THROW will do a longjmp().
  129.  
  130. format:
  131.  
  132. F_THROW, b1, b2, (instructions...), F_RETURN
  133.  
  134. Where b1,b2 is the address of the instruction after the return instruction.
  135.  
  136. 7. F_RETURN
  137.  
  138. Will deallocate the current frame, and restore the previous. If the flag
  139. extern_call is set, then a return from eval_instruction() will be done.
  140.