2015-10-15 14:40:08 +00:00
|
|
|
#class(#pointer) Context(Apex)
|
|
|
|
{
|
2016-06-22 03:23:14 +00:00
|
|
|
#dcl sender ip sp ntmprs.
|
2016-03-28 13:25:36 +00:00
|
|
|
|
|
|
|
#method sender
|
|
|
|
{
|
|
|
|
^self.sender
|
|
|
|
}
|
2016-04-29 16:40:25 +00:00
|
|
|
|
2016-06-23 15:35:28 +00:00
|
|
|
#method isDead
|
|
|
|
{
|
|
|
|
^self.ip < 0
|
|
|
|
}
|
2016-12-13 15:18:19 +00:00
|
|
|
|
|
|
|
#method temporaryCount
|
|
|
|
{
|
|
|
|
^self.ntmprs
|
|
|
|
}
|
|
|
|
|
|
|
|
(* ---------------------------------
|
|
|
|
#method varargCount
|
|
|
|
{
|
|
|
|
method context,
|
|
|
|
|
|
|
|
|
|
|
|
^do calculation...
|
|
|
|
|
|
|
|
for a block context, it must access homeContext first and call varargCount
|
|
|
|
|
|
|
|
^self.home varargCount...
|
|
|
|
}
|
|
|
|
|
|
|
|
#method varargAt: index
|
|
|
|
{
|
|
|
|
method context
|
|
|
|
^do calculation...
|
|
|
|
|
|
|
|
block context...
|
|
|
|
^self.home varargAt: index
|
|
|
|
}
|
|
|
|
|
|
|
|
---------------------------------- *)
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#class(#pointer) MethodContext(Context)
|
|
|
|
{
|
2016-03-28 13:25:36 +00:00
|
|
|
#dcl method receiver home origin.
|
2015-10-15 14:40:08 +00:00
|
|
|
|
|
|
|
#method pc
|
|
|
|
{
|
2016-05-03 10:10:28 +00:00
|
|
|
^self.ip
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 05:53:35 +00:00
|
|
|
#method pcplus1
|
|
|
|
{
|
|
|
|
^self.ip + 1
|
|
|
|
}
|
|
|
|
|
2016-05-18 11:10:54 +00:00
|
|
|
#method goto: anInteger
|
|
|
|
{
|
|
|
|
<primitive: #_context_goto>
|
|
|
|
self primitiveFailed. ## TODO: need to make this a hard failure?
|
|
|
|
}
|
|
|
|
|
2015-10-15 14:40:08 +00:00
|
|
|
#method pc: anInteger
|
|
|
|
{
|
2016-05-03 10:10:28 +00:00
|
|
|
self.ip := anInteger.
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method sp
|
|
|
|
{
|
2016-05-03 10:10:28 +00:00
|
|
|
^self.sp.
|
2015-10-15 14:40:08 +00:00
|
|
|
|
|
|
|
}
|
2016-06-23 15:35:28 +00:00
|
|
|
#method sp: new_sp
|
2015-10-15 14:40:08 +00:00
|
|
|
{
|
2016-06-23 15:35:28 +00:00
|
|
|
self.sp := new_sp
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
2016-06-23 15:35:28 +00:00
|
|
|
#method pc: new_pc sp: new_sp
|
2015-10-15 14:40:08 +00:00
|
|
|
{
|
2016-06-23 15:35:28 +00:00
|
|
|
self.ip := new_pc.
|
|
|
|
self.sp := new_sp.
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
2016-03-28 13:25:36 +00:00
|
|
|
|
2016-05-03 10:10:28 +00:00
|
|
|
#method method
|
|
|
|
{
|
|
|
|
^self.method
|
|
|
|
}
|
2016-12-13 15:18:19 +00:00
|
|
|
|
2016-12-14 07:18:01 +00:00
|
|
|
#method vargCount
|
2016-12-13 15:18:19 +00:00
|
|
|
{
|
|
|
|
^self basicSize - self class specNumInstVars - self.ntmprs
|
|
|
|
}
|
2016-12-14 07:18:01 +00:00
|
|
|
|
|
|
|
#method vargAt: index
|
|
|
|
{
|
|
|
|
^self basicAt: (index + self class specNumInstVars + self.ntmprs)
|
|
|
|
}
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#class(#pointer) BlockContext(Context)
|
|
|
|
{
|
2016-03-28 13:25:36 +00:00
|
|
|
#dcl nargs source home origin.
|
2015-10-15 14:40:08 +00:00
|
|
|
|
2016-12-14 07:18:01 +00:00
|
|
|
#method vargCount
|
|
|
|
{
|
|
|
|
^self.home vargCount
|
|
|
|
}
|
|
|
|
|
|
|
|
#method vargAt: index
|
2016-12-13 15:18:19 +00:00
|
|
|
{
|
2016-12-14 07:18:01 +00:00
|
|
|
^self.home vargAt: index
|
2016-12-13 15:18:19 +00:00
|
|
|
}
|
|
|
|
|
2016-06-22 03:23:14 +00:00
|
|
|
#method fork
|
2015-10-15 14:40:08 +00:00
|
|
|
{
|
|
|
|
"crate a new process in the runnable state"
|
2015-10-18 15:06:17 +00:00
|
|
|
^self newProcess resume.
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method newProcess
|
|
|
|
{
|
|
|
|
"create a new process in the suspended state"
|
2015-10-18 15:06:17 +00:00
|
|
|
<primitive: #_block_new_process>
|
|
|
|
self primitiveFailed.
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method newProcessWith: anArray
|
|
|
|
{
|
|
|
|
"create a new process in the suspended state passing the elements
|
|
|
|
of anArray as block arguments"
|
2015-10-22 02:47:25 +00:00
|
|
|
<primitive: #_block_new_process>
|
|
|
|
self primitiveFailed.
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method value
|
|
|
|
{
|
|
|
|
<primitive: #_block_value>
|
2016-01-29 04:04:39 +00:00
|
|
|
self primitiveFailed.
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
#method value: a
|
|
|
|
{
|
|
|
|
<primitive: #_block_value>
|
2016-01-29 04:04:39 +00:00
|
|
|
self primitiveFailed.
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
#method value: a value: b
|
|
|
|
{
|
|
|
|
<primitive: #_block_value>
|
2016-01-29 04:04:39 +00:00
|
|
|
self primitiveFailed.
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
#method value: a value: b value: c
|
|
|
|
{
|
|
|
|
<primitive: #_block_value>
|
2016-01-29 04:04:39 +00:00
|
|
|
self primitiveFailed.
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
2016-05-12 05:53:35 +00:00
|
|
|
#method value: a value: b value: c value: d
|
|
|
|
{
|
|
|
|
<primitive: #_block_value>
|
|
|
|
self primitiveFailed.
|
|
|
|
}
|
|
|
|
#method value: a value: b value: c value: d value: e
|
|
|
|
{
|
|
|
|
<primitive: #_block_value>
|
|
|
|
self primitiveFailed.
|
|
|
|
}
|
2015-10-15 14:40:08 +00:00
|
|
|
|
|
|
|
#method ifTrue: aBlock
|
|
|
|
{
|
|
|
|
^(self value) ifTrue: aBlock.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method ifFalse: aBlock
|
|
|
|
{
|
|
|
|
^(self value) ifFalse: aBlock.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method ifTrue: trueBlock ifFalse: falseBlock
|
|
|
|
{
|
|
|
|
^(self value) ifTrue: trueBlock ifFalse: falseBlock
|
|
|
|
}
|
|
|
|
|
|
|
|
#method whileTrue: aBlock
|
|
|
|
{
|
2016-05-12 05:53:35 +00:00
|
|
|
## --------------------------------------------------
|
|
|
|
## Naive implementation
|
|
|
|
## --------------------------------------------------
|
|
|
|
## (self value) ifFalse: [^nil].
|
|
|
|
## aBlock value.
|
|
|
|
## self whileTrue: aBlock.
|
|
|
|
## --------------------------------------------------
|
|
|
|
|
|
|
|
## --------------------------------------------------
|
2016-06-24 15:01:51 +00:00
|
|
|
## Non-recursive implementation
|
2016-05-12 05:53:35 +00:00
|
|
|
## --------------------------------------------------
|
2016-05-18 11:10:54 +00:00
|
|
|
| pc sp |
|
|
|
|
|
2016-05-12 05:53:35 +00:00
|
|
|
pc := thisContext pcplus1.
|
2016-05-18 11:10:54 +00:00
|
|
|
(self value) ifFalse: [ ^nil ].
|
2015-10-15 14:40:08 +00:00
|
|
|
aBlock value.
|
2016-05-18 11:10:54 +00:00
|
|
|
|
|
|
|
## the pc: method leaves thisContext and pc in the stack after
|
|
|
|
## having changes the instruction poointer.
|
|
|
|
## as a result, the stack keeps growing. the goto method
|
|
|
|
## clears thisContext and pc off the stack unlike normal methods.
|
|
|
|
##thisContext pc: pc.
|
|
|
|
thisContext goto: pc.
|
2016-05-12 05:53:35 +00:00
|
|
|
## --------------------------------------------------
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 13:25:36 +00:00
|
|
|
#method whileTrue
|
|
|
|
{
|
2016-05-12 05:53:35 +00:00
|
|
|
## (self value) ifFalse: [^nil].
|
|
|
|
## self whileTrue.
|
|
|
|
|
|
|
|
## --------------------------------------------------
|
2016-06-24 15:01:51 +00:00
|
|
|
## Non-recursive implementation
|
2016-05-12 05:53:35 +00:00
|
|
|
## --------------------------------------------------
|
|
|
|
| pc |
|
|
|
|
pc := thisContext pcplus1.
|
2016-05-18 11:10:54 +00:00
|
|
|
(self value) ifFalse: [ ^nil ].
|
|
|
|
thisContext goto: pc.
|
2016-05-12 05:53:35 +00:00
|
|
|
## --------------------------------------------------
|
2016-03-28 13:25:36 +00:00
|
|
|
}
|
|
|
|
|
2016-03-16 10:13:03 +00:00
|
|
|
#method whileFalse: aBlock
|
|
|
|
{
|
2016-05-12 05:53:35 +00:00
|
|
|
## --------------------------------------------------
|
|
|
|
## Naive implementation
|
|
|
|
## --------------------------------------------------
|
|
|
|
## (self value) ifTrue: [^nil].
|
|
|
|
## aBlock value.
|
|
|
|
## self whileFalse: aBlock.
|
|
|
|
## --------------------------------------------------
|
|
|
|
|
|
|
|
## --------------------------------------------------
|
2016-06-24 15:01:51 +00:00
|
|
|
## Non-recursive implementation
|
2016-05-12 05:53:35 +00:00
|
|
|
## --------------------------------------------------
|
|
|
|
## The assignment to 'pc' uses the POP_INTO_TEMPVAR_1.
|
|
|
|
## It pops a value off the stack top, stores it to the second
|
|
|
|
## temporary variable(aBlock is the first temporary variable).
|
|
|
|
## It is a single byte instruction. 'pc' returned by
|
|
|
|
## 'thisContext pcplus1'' should point to the instruction after
|
|
|
|
## the POP_INTO_TEMPVAR_0 instruction.
|
|
|
|
##
|
|
|
|
## It would need the increment of 2 if the pair of
|
|
|
|
## STORE_INTO_TEMPVAR_1 and POP_STACKTOP were used.
|
|
|
|
## This implementation is subject to the instructions chosen
|
|
|
|
## by the compiler.
|
|
|
|
##
|
|
|
|
| pc |
|
|
|
|
pc := thisContext pcplus1.
|
|
|
|
(self value) ifTrue: [ ^nil "^self" ].
|
|
|
|
aBlock value.
|
2016-05-18 11:10:54 +00:00
|
|
|
thisContext goto: pc.
|
2016-05-12 05:53:35 +00:00
|
|
|
## --------------------------------------------------
|
2016-03-16 10:13:03 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 13:25:36 +00:00
|
|
|
#method whileFalse
|
|
|
|
{
|
2016-05-12 05:53:35 +00:00
|
|
|
## (self value) ifTrue: [^nil].
|
|
|
|
## self whileFalse.
|
|
|
|
|
|
|
|
## --------------------------------------------------
|
2016-06-24 15:01:51 +00:00
|
|
|
## Non-recursive implementation
|
2016-05-12 05:53:35 +00:00
|
|
|
## --------------------------------------------------
|
|
|
|
| pc |
|
|
|
|
pc := thisContext pcplus1.
|
|
|
|
(self value) ifTrue: [ ^nil "^self" ].
|
2016-05-18 11:10:54 +00:00
|
|
|
thisContext goto: pc.
|
2016-05-12 05:53:35 +00:00
|
|
|
## --------------------------------------------------
|
2016-03-28 13:25:36 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 14:40:08 +00:00
|
|
|
#method pc
|
|
|
|
{
|
2016-05-12 05:53:35 +00:00
|
|
|
^self.ip
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method pc: anInteger
|
|
|
|
{
|
2016-05-12 05:53:35 +00:00
|
|
|
self.ip := anInteger.
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method sp
|
|
|
|
{
|
2016-05-12 05:53:35 +00:00
|
|
|
^self.sp
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method sp: anInteger
|
|
|
|
{
|
2016-05-12 05:53:35 +00:00
|
|
|
self.sp := anInteger.
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method restart
|
|
|
|
{
|
2016-06-26 15:03:12 +00:00
|
|
|
ip := self.source pc.
|
2016-06-20 15:42:51 +00:00
|
|
|
}
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|