Compiler-Interpreter.st 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. Smalltalk current createPackage: 'Compiler-Interpreter'!
  2. BlockClosure subclass: #AIBlockClosure
  3. instanceVariableNames: 'node outerContext'
  4. package: 'Compiler-Interpreter'!
  5. !AIBlockClosure commentStamp!
  6. I am a special `BlockClosure` subclass used by an interpreter to interpret a block node.
  7. While I am polymorphic with `BlockClosure`, some methods such as `#new` will raise interpretation errors. Unlike a `BlockClosure`, my instance are not JavaScript functions.
  8. Evaluating an instance will result in interpreting the `node` instance variable (instance of `BlockNode`).!
  9. !AIBlockClosure methodsFor: 'accessing'!
  10. compiledSource
  11. "Unlike blocks, the receiver doesn't represent a JS function"
  12. ^ '[ AST Block closure ]'
  13. !
  14. numArgs
  15. ^ node temps size
  16. ! !
  17. !AIBlockClosure methodsFor: 'converting'!
  18. currySelf
  19. self interpreterError
  20. ! !
  21. !AIBlockClosure methodsFor: 'error handling'!
  22. interpreterError
  23. ASTInterpreterError signal: 'Method cannot be interpreted by the interpreter.'
  24. ! !
  25. !AIBlockClosure methodsFor: 'evaluating'!
  26. applyTo: anObject arguments: aCollection
  27. self interpreterError
  28. !
  29. value
  30. ^ self valueWithPossibleArguments: #()
  31. !
  32. value: anArgument
  33. ^ self valueWithPossibleArguments: {anArgument}
  34. !
  35. value: firstArgument value: secondArgument
  36. ^ self valueWithPossibleArguments: {firstArgument . secondArgument}
  37. !
  38. value: firstArgument value: secondArgument value: thirdArgument
  39. ^ self valueWithPossibleArguments: {firstArgument . secondArgument . thirdArgument}
  40. !
  41. valueWithPossibleArguments: aCollection
  42. | context sequenceNode |
  43. context := outerContext newBlockContext.
  44. "Interpret a copy of the sequence node to avoid creating a new AIBlockClosure"
  45. sequenceNode := node nodes first copy
  46. parent: nil;
  47. yourself.
  48. "Populate the arguments into the context locals"
  49. node parameters withIndexDo: [ :each :index |
  50. context localAt: each put: (aCollection at: index ifAbsent: [ nil ]) ].
  51. "Interpret the first node of the BlockSequenceNode"
  52. context interpreter
  53. node: sequenceNode nextChild;
  54. proceed.
  55. outerContext interpreter
  56. setNonLocalReturnFromContext: context.
  57. ^ context interpreter pop
  58. ! !
  59. !AIBlockClosure methodsFor: 'initialization'!
  60. initializeWithContext: aContext node: aNode
  61. node := aNode.
  62. outerContext := aContext
  63. ! !
  64. !AIBlockClosure class methodsFor: 'instance creation'!
  65. forContext: aContext node: aNode
  66. ^ self new
  67. initializeWithContext: aContext node: aNode;
  68. yourself
  69. ! !
  70. MethodContext subclass: #AIContext
  71. instanceVariableNames: 'outerContext innerContext pc locals selector index ast interpreter'
  72. package: 'Compiler-Interpreter'!
  73. !AIContext commentStamp!
  74. I am like a `MethodContext`, used by the `ASTInterpreter`.
  75. Unlike a `MethodContext`, my instances are not read-only.
  76. When debugging, my instances are created by copying the current `MethodContext` (thisContext)!
  77. !AIContext methodsFor: 'accessing'!
  78. index
  79. ^ index ifNil: [ 0 ]
  80. !
  81. index: anInteger
  82. index := anInteger
  83. !
  84. innerContext
  85. ^ innerContext
  86. !
  87. innerContext: anAIContext
  88. innerContext := anAIContext
  89. !
  90. localAt: aString
  91. "Lookup the local value up to the method context"
  92. ^ self locals at: aString ifAbsent: [
  93. self outerContext ifNotNil: [ :context |
  94. context localAt: aString ] ]
  95. !
  96. localAt: aString ifAbsent: aBlock
  97. "Lookup the local value up to the method context"
  98. ^ self locals at: aString ifAbsent: [
  99. self outerContext
  100. ifNotNil: [ :context | context localAt: aString ifAbsent: aBlock ]
  101. ifNil: [aBlock value] ]
  102. !
  103. localAt: aString put: anObject
  104. self locals at: aString put: anObject
  105. !
  106. locals
  107. locals ifNil: [ self initializeLocals ].
  108. ^ locals
  109. !
  110. method
  111. ^ self methodContext ifNotNil: [
  112. self methodContext receiver class lookupSelector: self methodContext selector ]
  113. !
  114. outerContext
  115. ^ outerContext
  116. !
  117. outerContext: anAIContext
  118. outerContext := anAIContext.
  119. outerContext innerContext: self
  120. !
  121. selector
  122. ^ selector
  123. !
  124. selector: aString
  125. selector := aString
  126. ! !
  127. !AIContext methodsFor: 'factory'!
  128. newBlockContext
  129. ^ self class new
  130. outerContext: self;
  131. yourself
  132. ! !
  133. !AIContext methodsFor: 'initialization'!
  134. initializeAST
  135. ast := self method ast.
  136. (SemanticAnalyzer on: self method methodClass)
  137. visit: ast
  138. !
  139. initializeFromMethodContext: aMethodContext
  140. self
  141. index: aMethodContext index;
  142. receiver: aMethodContext receiver;
  143. selector: aMethodContext selector.
  144. aMethodContext outerContext ifNotNil: [ :outer |
  145. "If the method context is nil, the block was defined in JS, so ignore it"
  146. outer methodContext ifNotNil: [
  147. self outerContext: (self class fromMethodContext: aMethodContext outerContext) ].
  148. aMethodContext locals keysAndValuesDo: [ :key :value |
  149. self locals at: key put: value ] ]
  150. !
  151. initializeInterpreter
  152. interpreter := ASTInterpreter new
  153. context: self;
  154. node: self retrieveNode;
  155. yourself.
  156. (self innerContext notNil and: [
  157. self innerContext isBlockContext not ]) ifTrue: [
  158. self setupInterpreter: interpreter ]
  159. !
  160. initializeLocals
  161. locals := Dictionary new.
  162. locals at: 'thisContext' put: self.
  163. ! !
  164. !AIContext methodsFor: 'interpreting'!
  165. arguments
  166. ^ self ast arguments collect: [ :each |
  167. self localAt: each ]
  168. !
  169. ast
  170. self isBlockContext ifTrue: [
  171. ^ self outerContext ifNotNil: [ :context | context ast ] ].
  172. ast ifNil: [ self initializeAST ].
  173. ^ ast
  174. !
  175. interpreter
  176. interpreter ifNil: [ self initializeInterpreter ].
  177. ^ interpreter
  178. !
  179. interpreter: anInterpreter
  180. interpreter := anInterpreter
  181. !
  182. receiver
  183. ^ self localAt: 'self'
  184. !
  185. receiver: anObject
  186. self localAt: 'self' put: anObject
  187. !
  188. retrieveNode
  189. ^ self ast ifNotNil: [
  190. ASTPCNodeVisitor new
  191. context: self;
  192. visit: self ast;
  193. currentNode ]
  194. !
  195. setupInterpreter: anInterpreter
  196. "Push the send args and receiver to the interpreter stack"
  197. self innerContext arguments reversed do: [ :each |
  198. anInterpreter push: each ].
  199. anInterpreter push: (self innerContext receiver)
  200. ! !
  201. !AIContext class methodsFor: 'instance creation'!
  202. fromMethodContext: aMethodContext
  203. ^ self new
  204. initializeFromMethodContext: aMethodContext;
  205. yourself
  206. ! !
  207. Object subclass: #ASTDebugger
  208. instanceVariableNames: 'interpreter context'
  209. package: 'Compiler-Interpreter'!
  210. !ASTDebugger commentStamp!
  211. I am a stepping debugger interface for Amber code.
  212. I internally use an instance of `ASTSteppingInterpreter` to actually step through node and interpret them.
  213. My instances are created from a `MethodContext` with `ASTDebugger class >> context:`.
  214. They hold an `AIContext` instance internally, recursive copy of the `MethodContext`.
  215. ## API
  216. Use the methods of the `'stepping'` protocol to do stepping.!
  217. !ASTDebugger methodsFor: 'accessing'!
  218. context
  219. ^ context
  220. !
  221. context: aContext
  222. context := aContext
  223. !
  224. interpreter
  225. ^ interpreter ifNil: [ interpreter := self defaultInterpreterClass new ]
  226. !
  227. interpreter: anInterpreter
  228. interpreter := anInterpreter
  229. !
  230. method
  231. ^ self context method
  232. !
  233. nextNode
  234. ^ self interpreter nextNode
  235. ! !
  236. !ASTDebugger methodsFor: 'defaults'!
  237. defaultInterpreterClass
  238. ^ ASTInterpreter
  239. ! !
  240. !ASTDebugger methodsFor: 'initialization'!
  241. buildAST
  242. "Build the AST tree from the method source code.
  243. The AST is annotated with a SemanticAnalyzer,
  244. to know the semantics and bindings of each node needed for later debugging"
  245. | ast |
  246. ast := Smalltalk current parse: self method source.
  247. (SemanticAnalyzer on: self context receiver class)
  248. visit: ast.
  249. ^ ast
  250. !
  251. initializeInterpreter
  252. | ast next |
  253. ast := self buildAST.
  254. next := ASTPCNodeVisitor new
  255. context: self context;
  256. visit: ast;
  257. currentNode.
  258. self interpreter node: next
  259. !
  260. initializeWithContext: aContext
  261. "TODO: do we need to handle block contexts?"
  262. self context: aContext.
  263. self initializeInterpreter
  264. ! !
  265. !ASTDebugger methodsFor: 'stepping'!
  266. proceed
  267. self shouldBeImplemented
  268. !
  269. restart
  270. self interpreter restart
  271. !
  272. stepInto
  273. self shouldBeImplemented
  274. !
  275. stepOver
  276. self interpreter stepOver
  277. ! !
  278. !ASTDebugger methodsFor: 'testing'!
  279. atEnd
  280. ^ self interpreter atEnd
  281. ! !
  282. !ASTDebugger class methodsFor: 'instance creation'!
  283. context: aContext
  284. ^ self new
  285. initializeWithContext: aContext;
  286. yourself
  287. ! !
  288. NodeVisitor subclass: #ASTInterpreter
  289. instanceVariableNames: 'node context stack returnValue returned'
  290. package: 'Compiler-Interpreter'!
  291. !ASTInterpreter commentStamp!
  292. I visit an AST, interpreting (evaluating) nodes one after the other, using a small stack machine.
  293. ## API
  294. While my instances should be used from within an `ASTDebugger`, which provides a more high level interface,
  295. you can use methods from the `interpreting` protocol:
  296. - `#step` evaluates the current `node` only
  297. - `#stepOver` evaluates the AST from the current `node` up to the next stepping node (most likely the next send node)
  298. - `#proceed` evaluates eagerly the AST
  299. - `#restart` select the first node of the AST
  300. - `#skip` skips the current node, moving to the next one if any!
  301. !ASTInterpreter methodsFor: 'accessing'!
  302. context
  303. ^ context
  304. !
  305. context: aContext
  306. context := aContext
  307. !
  308. node
  309. "Answer the next node, ie the node to be evaluated in the next step"
  310. ^ node
  311. !
  312. node: aNode
  313. node := aNode
  314. !
  315. result
  316. ^ self hasReturned
  317. ifTrue: [ self returnValue ]
  318. ifFalse: [ self context receiver ]
  319. !
  320. returnValue
  321. ^ returnValue
  322. !
  323. returnValue: anObject
  324. returnValue := anObject
  325. !
  326. stack
  327. ^ stack ifNil: [ stack := OrderedCollection new ]
  328. ! !
  329. !ASTInterpreter methodsFor: 'interpreting'!
  330. interpret
  331. "Interpret the next node to be evaluated"
  332. self visit: self node
  333. !
  334. interpret: aNode
  335. self node: aNode.
  336. self interpret
  337. !
  338. next
  339. self node: self node nextNode
  340. !
  341. proceed
  342. "Eagerly evaluate the ast"
  343. [ self atEnd ] whileFalse: [
  344. self step ]
  345. !
  346. restart
  347. self node: self context ast nextChild
  348. !
  349. setNonLocalReturnFromContext: aContext
  350. aContext interpreter hasReturned ifTrue: [
  351. returned := true.
  352. self returnValue: aContext interpreter returnValue ]
  353. !
  354. skip
  355. self next
  356. !
  357. step
  358. self
  359. interpret;
  360. next
  361. !
  362. stepOver
  363. self step.
  364. [ self node isSteppingNode ] whileFalse: [
  365. self step ]
  366. ! !
  367. !ASTInterpreter methodsFor: 'private'!
  368. assign: aNode to: anObject
  369. aNode binding isInstanceVar
  370. ifTrue: [ self context receiver instVarAt: aNode value put: anObject ]
  371. ifFalse: [ self context localAt: aNode value put: anObject ]
  372. !
  373. eval: aString
  374. "Evaluate aString as JS source inside an JS function.
  375. aString is not sandboxed."
  376. | source function |
  377. source := String streamContents: [ :str |
  378. str nextPutAll: '(function('.
  379. self context locals keys
  380. do: [ :each | str nextPutAll: each ]
  381. separatedBy: [ str nextPutAll: ',' ].
  382. str
  383. nextPutAll: '){ return (function() {';
  384. nextPutAll: aString;
  385. nextPutAll: '})() })' ].
  386. function := Compiler new eval: source.
  387. ^ function valueWithPossibleArguments: self context locals values
  388. !
  389. messageFromSendNode: aSendNode arguments: aCollection
  390. ^ Message new
  391. selector: aSendNode selector;
  392. arguments: aCollection;
  393. yourself
  394. !
  395. messageNotUnderstood: aMessage receiver: anObject
  396. MessageNotUnderstood new
  397. meesage: aMessage;
  398. receiver: anObject;
  399. signal
  400. !
  401. sendMessage: aMessage to: anObject superSend: aBoolean
  402. | method |
  403. aBoolean ifFalse: [ ^ aMessage sendTo: anObject ].
  404. anObject class superclass ifNil: [ ^ self messageNotUnderstood: aMessage receiver: anObject ].
  405. method := anObject class superclass methodDictionary
  406. at: aMessage selector
  407. ifAbsent: [ ^ self messageNotUnderstood: aMessage receiver: anObject ].
  408. ^ method sendTo: anObject arguments: aMessage arguments
  409. ! !
  410. !ASTInterpreter methodsFor: 'stack'!
  411. peek
  412. "Peek the top object of the context stack"
  413. self stack ifEmpty: [ ^ nil ].
  414. ^ self stack last
  415. !
  416. pop
  417. "Pop an object from the context stack"
  418. | peekedValue |
  419. peekedValue := self peek.
  420. self stack removeLast.
  421. ^ peekedValue
  422. !
  423. push: anObject
  424. "Push an object to the context stack"
  425. ^ self stack add: anObject
  426. ! !
  427. !ASTInterpreter methodsFor: 'testing'!
  428. atEnd
  429. ^ self hasReturned or: [ self node isNil ]
  430. !
  431. hasReturned
  432. ^ returned ifNil: [ false ]
  433. ! !
  434. !ASTInterpreter methodsFor: 'visiting'!
  435. visit: aNode
  436. self hasReturned ifFalse: [ super visit: aNode ]
  437. !
  438. visitAssignmentNode: aNode
  439. | poppedValue |
  440. poppedValue := self pop.
  441. "Pop the left side of the assignment.
  442. It already has been visited, and we don't need its value."
  443. self pop.
  444. self push: poppedValue.
  445. self assign: aNode left to: poppedValue
  446. !
  447. visitBlockNode: aNode
  448. "Do not evaluate the block node.
  449. Instead, put all instructions into a block that we push to the stack for later evaluation"
  450. | block |
  451. block := AIBlockClosure forContext: self context node: aNode.
  452. self push: block
  453. !
  454. visitDynamicArrayNode: aNode
  455. | array |
  456. array := #().
  457. aNode nodes do: [ :each |
  458. array addFirst: self pop ].
  459. self push: array
  460. !
  461. visitDynamicDictionaryNode: aNode
  462. | associations hashedCollection |
  463. associations := OrderedCollection new.
  464. hashedCollection := HashedCollection new.
  465. aNode nodes do: [ :each |
  466. associations add: self pop ].
  467. associations reversed do: [ :each |
  468. hashedCollection add: each ].
  469. self push: hashedCollection
  470. !
  471. visitJSStatementNode: aNode
  472. returned := true.
  473. self returnValue: (self eval: aNode source)
  474. !
  475. visitNode: aNode
  476. "Do nothing by default. Especially, do not visit children recursively."
  477. !
  478. visitReturnNode: aNode
  479. returned := true.
  480. self returnValue: self pop
  481. !
  482. visitSendNode: aNode
  483. | receiver args message result |
  484. args := aNode arguments collect: [ :each | self pop ].
  485. receiver := self pop.
  486. message := self
  487. messageFromSendNode: aNode
  488. arguments: args reversed.
  489. result := self sendMessage: message to: receiver superSend: aNode superSend.
  490. "For cascade sends, push the reciever if the send is not the last one"
  491. (aNode isCascadeSendNode and: [ aNode isLastChild not ])
  492. ifTrue: [ self push: receiver ]
  493. ifFalse: [ self push: result ]
  494. !
  495. visitValueNode: aNode
  496. self push: aNode value
  497. !
  498. visitVariableNode: aNode
  499. aNode binding isUnknownVar ifTrue: [
  500. ^ self push: (PlatformInterface globals at: aNode value ifAbsent: [ self error: 'Unknown variable' ]) ].
  501. self push: (aNode binding isInstanceVar
  502. ifTrue: [ self context receiver instVarAt: aNode value ]
  503. ifFalse: [ self context
  504. localAt: aNode value
  505. ifAbsent: [
  506. aNode value isCapitalized
  507. ifTrue: [
  508. Smalltalk current
  509. at: aNode value
  510. ifAbsent: [ PlatformInterface globals at: aNode value ]]]])
  511. ! !
  512. Error subclass: #ASTInterpreterError
  513. instanceVariableNames: ''
  514. package: 'Compiler-Interpreter'!
  515. !ASTInterpreterError commentStamp!
  516. I get signaled when an AST interpreter is unable to interpret a node.!
  517. NodeVisitor subclass: #ASTPCNodeVisitor
  518. instanceVariableNames: 'useInlinings pc context blockIndex currentNode'
  519. package: 'Compiler-Interpreter'!
  520. !ASTPCNodeVisitor commentStamp!
  521. I visit an AST until I get to the current pc node and answer it.
  522. ## API
  523. My instances must be filled with a context object using `#context:`.
  524. After visiting the AST the current node corresponding to the `pc` is answered by `#currentNode`!
  525. !ASTPCNodeVisitor methodsFor: 'accessing'!
  526. blockIndex
  527. ^ blockIndex ifNil: [ blockIndex := 0 ]
  528. !
  529. context
  530. ^ context
  531. !
  532. context: aContext
  533. context := aContext
  534. !
  535. currentNode
  536. ^ currentNode
  537. !
  538. increaseBlockIndex
  539. blockIndex := self blockIndex + 1
  540. !
  541. pc
  542. ^ pc ifNil: [ 0 ]
  543. !
  544. pc: anInteger
  545. pc := anInteger
  546. !
  547. useInlinings
  548. ^ useInlinings ifNil: [ true ]
  549. !
  550. useInlinings: aBoolean
  551. useInlinings := aBoolean
  552. ! !
  553. !ASTPCNodeVisitor methodsFor: 'visiting'!
  554. visitBlockNode: aNode
  555. "Inlined send node. Assume that the block is inlined"
  556. (aNode parent isSendNode and: [ aNode parent shouldBeInlined ])
  557. ifFalse: [
  558. self blockIndex >= self context index ifFalse: [
  559. self increaseBlockIndex.
  560. super visitBlockNode: aNode ] ]
  561. ifTrue: [ super visitBlockNode: aNode ]
  562. !
  563. visitJSStatementNode: aNode
  564. currentNode := aNode
  565. !
  566. visitSendNode: aNode
  567. super visitSendNode: aNode.
  568. self pc = self context pc ifFalse: [
  569. aNode shouldBeInlined ifFalse: [
  570. self blockIndex = self context index ifTrue: [
  571. self pc: self pc + 1.
  572. currentNode := aNode ] ] ]
  573. ! !
  574. !Node methodsFor: '*Compiler-Interpreter'!
  575. isSteppingNode
  576. ^ false
  577. ! !
  578. !AssignmentNode methodsFor: '*Compiler-Interpreter'!
  579. isSteppingNode
  580. ^ true
  581. ! !
  582. !BlockNode methodsFor: '*Compiler-Interpreter'!
  583. isSteppingNode
  584. ^ true
  585. ! !
  586. !DynamicArrayNode methodsFor: '*Compiler-Interpreter'!
  587. isSteppingNode
  588. ^ true
  589. ! !
  590. !DynamicDictionaryNode methodsFor: '*Compiler-Interpreter'!
  591. isSteppingNode
  592. ^ true
  593. ! !
  594. !JSStatementNode methodsFor: '*Compiler-Interpreter'!
  595. isSteppingNode
  596. ^ true
  597. ! !
  598. !SendNode methodsFor: '*Compiler-Interpreter'!
  599. isSteppingNode
  600. ^ true
  601. ! !