traps.nr 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. .bp
  2. .P1 "TRAPS AND INTERRUPTS"
  3. .PP
  4. EM provides a means for the user program to catch all traps
  5. generated by the program itself, the hardware, or external conditions.
  6. This mechanism uses five instructions: LIM, SIM, SIG, TRP and RTT.
  7. This section of the manual may be omitted on the first reading since it
  8. presupposes knowledge of the EM instruction set.
  9. .PP
  10. The action taken when a trap occurs is determined by the value
  11. of an internal EM trap register.
  12. This register contains a pointer to a procedure.
  13. Initially the pointer used is zero and all traps halt the
  14. program with, hopefully, a useful message to the outside world.
  15. The SIG instruction can be used to alter the trap register,
  16. it pops a procedure pointer from the
  17. stack into the trap register.
  18. When a trap occurs after storing a nonzero value in the trap
  19. register, the procedure pointed to by the trap register
  20. is called with the trap number
  21. as the only parameter (see below).
  22. SIG returns the previous value of the trap register on the
  23. stack.
  24. Two consecutive SIGs are a no-op.
  25. When a trap occurs, the trap register is reset to its initial
  26. condition, to prevent recursive traps from hanging the machine up,
  27. e.g. stack overflow in the stack overflow handling procedure.
  28. .PP
  29. The runtime systems for some languages need to ignore some EM
  30. traps.
  31. EM offers a feature called the ignore mask.
  32. It contains one bit for each of the lowest 16 trap numbers.
  33. The bits are numbered 0 to 15, with the least significant bit
  34. having number 0.
  35. If a certain bit is 1 the corresponding trap never
  36. occurs and processing simply continues.
  37. The actions performed by the offending instruction are
  38. described by the Pascal program in appendix A.
  39. .br
  40. If the bit is 0, traps are not ignored.
  41. The instructions LIM and SIM allow copying and replacement of
  42. the ignore mask.~
  43. .PP
  44. The TRP instruction generates a trap, the trap number being found on the
  45. stack.
  46. This is, among other things,
  47. useful for library procedures and runtime systems.
  48. It can also be used by a low level trap procedure to pass the trap to a
  49. higher level one (see example below).
  50. .PP
  51. The RTT instruction returns from the trap procedure and continues after the
  52. trap.
  53. In the list below all traps marked with an asterisk ('*') are
  54. considered to be fatal and it is explicitly undefined what happens when
  55. restarting after the trap.
  56. .PP
  57. The way a trap procedure is called is completely compatible
  58. with normal calling conventions. The only way a trap procedure
  59. differs from normal procedures is the return. It has to use RTT instead
  60. of RET. This is necessary because the complete runtime status is saved on the
  61. stack before calling the procedure and all this status has to be reloaded.
  62. Error numbers are in the range 0 to 252.
  63. The trap numbers are divided into three categories:
  64. .IP "\0\00\-\063" 12
  65. EM machine errors, e.g. illegal instruction.
  66. .RS
  67. .IP "\00\-15" 8
  68. maskable
  69. .IP "16\-63" 8
  70. not maskable
  71. .RE
  72. .IP "\064\-127" 12
  73. Reserved for use by compilers, run time systems, etc.
  74. .IP "128\-252" 12
  75. Available for user programs.
  76. .LP
  77. EM machine errors are numbered as follows:
  78. .TS
  79. tab(@);
  80. n l l.
  81. 0@EARRAY@Array bound error
  82. 1@ERANGE@Range bound error
  83. 2@ESET@Set bound error
  84. 3@EIOVFL@Integer overflow
  85. 4@EFOVFL@Floating overflow
  86. 5@EFUNFL@Floating underflow
  87. 6@EIDIVZ@Divide by 0
  88. 7@EFDIVZ@Divide by 0.0
  89. 8@EIUND@Undefined integer
  90. 9@EFUND@Undefined float
  91. 10@ECONV@Conversion error
  92. 16*@ESTACK@Stack overflow
  93. 17@EHEAP@Heap overflow
  94. 18*@EILLINS@Illegal instruction
  95. 19*@EODDZ@Illegal size argument
  96. 20*@ECASE@Case error
  97. 21*@EMEMFLT@Addressing non existent memory
  98. 22*@EBADPTR@Bad pointer used
  99. 23*@EBADPC@Program counter out of range
  100. 24@EBADLAE@Bad argument of LAE
  101. 25@EBADMON@Bad monitor call
  102. 26@EBADLIN@Argument of LIN too high
  103. 27@EBADGTO@GTO descriptor error
  104. .TE
  105. .PP
  106. As an example,
  107. suppose a subprocedure has to be written to do a numeric
  108. calculation.
  109. When an overflow occurs the computation has to be stopped and
  110. the higher level procedure must be resumed.
  111. This can be programmed as follows using the mechanism described above:
  112. .LP
  113. .KS
  114. .nf
  115. .ta 1n 24n
  116. mes 2,2,2 ; set sizes
  117. ersave
  118. bss 2,0,0 ; Room to save previous value of trap procedure
  119. msave
  120. bss 2,0,0 ; Room to save previous value of trap mask
  121. pro $calcule,0 ; entry point
  122. lxl 0 ; fill in non-local goto descriptor with LB
  123. ste jmpbuf+4
  124. lor 1 ; and SP
  125. ste jmpbuf+2
  126. lim ; get current ignore mask
  127. ste msave ; save it
  128. lim
  129. loc 16 ; bit for EFOVFL
  130. ior 2 ; set in mask
  131. sim ; ignore EFOVFL from now on
  132. lpi $catch ; load procedure identifier
  133. sig ; catch wil get all traps now
  134. ste ersave ; save previous trap procedure identifier
  135. ; perform calculation now, possibly generating overflow
  136. 1 ; label jumped to by catch procedure
  137. loe ersave ; get old trap procedure
  138. sig ; refer all following trap to old procedure
  139. asp 2 ; remove result of sig
  140. loe msave ; restore previous mask
  141. sim ; done now
  142. ; load result of calculation
  143. ret 2 ; return result
  144. jmpbuf
  145. con *1,0,0
  146. end
  147. .KE
  148. .KS
  149. .LP
  150. Example of catch procedure
  151. .LP
  152. .nf
  153. .ta 1n 24n
  154. pro $catch,0 ; Local procedure that must catch the overflow trap
  155. lol 2 ; Load trap number
  156. loc 4 ; check for overflow
  157. bne *1 ; if other trap, call higher trap procedure
  158. gto jmpbuf ; return to procedure calcule
  159. 1 ; other trap has occurred
  160. loe ersave ; previous trap procedure
  161. sig ; other procedure will get the traps now
  162. asp 2 ; remove the result of sig
  163. lol 2 ; stack trap number
  164. trp ; call other trap procedure
  165. rtt ; if other procedure returns, do the same
  166. end
  167. .KE
  168. .fi