1
0

Compiler.st 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. Object subclass: #Node
  2. instanceVariableNames: 'nodes'
  3. category: 'Compiler'!
  4. !Node methodsFor: 'accessing'!
  5. nodes
  6. ^nodes ifNil: [nodes := Array new]
  7. !
  8. addNode: aNode
  9. self nodes add: aNode
  10. ! !
  11. !Node methodsFor: 'building'!
  12. nodes: aCollection
  13. nodes := aCollection
  14. ! !
  15. !Node methodsFor: 'visiting'!
  16. accept: aVisitor
  17. aVisitor visitNode: self
  18. ! !
  19. Node subclass: #MethodNode
  20. instanceVariableNames: 'selector arguments source'
  21. category: 'Compiler'!
  22. !MethodNode methodsFor: 'accessing'!
  23. selector
  24. ^selector
  25. !
  26. selector: aString
  27. selector := aString
  28. !
  29. arguments
  30. ^arguments ifNil: [#()]
  31. !
  32. arguments: aCollection
  33. arguments := aCollection
  34. !
  35. source
  36. ^source
  37. !
  38. source: aString
  39. source := aString
  40. ! !
  41. !MethodNode methodsFor: 'visiting'!
  42. accept: aVisitor
  43. aVisitor visitMethodNode: self
  44. ! !
  45. Node subclass: #SendNode
  46. instanceVariableNames: 'selector arguments receiver'
  47. category: 'Compiler'!
  48. !SendNode methodsFor: 'accessing'!
  49. selector
  50. ^selector
  51. !
  52. selector: aString
  53. selector := aString
  54. !
  55. arguments
  56. ^arguments ifNil: [arguments := #()]
  57. !
  58. arguments: aCollection
  59. arguments := aCollection
  60. !
  61. receiver
  62. ^receiver
  63. !
  64. receiver: aNode
  65. receiver := aNode
  66. !
  67. valueForReceiver: anObject
  68. ^SendNode new
  69. receiver: (self receiver
  70. ifNil: [anObject]
  71. ifNotNil: [self receiver valueForReceiver: anObject]);
  72. selector: self selector;
  73. arguments: self arguments;
  74. yourself
  75. !
  76. cascadeNodeWithMessages: aCollection
  77. | first |
  78. first := SendNode new
  79. selector: self selector;
  80. arguments: self arguments;
  81. yourself.
  82. ^CascadeNode new
  83. receiver: self receiver;
  84. nodes: (Array with: first), aCollection;
  85. yourself
  86. ! !
  87. !SendNode methodsFor: 'visiting'!
  88. accept: aVisitor
  89. aVisitor visitSendNode: self
  90. ! !
  91. Node subclass: #CascadeNode
  92. instanceVariableNames: 'receiver'
  93. category: 'Compiler'!
  94. !CascadeNode methodsFor: 'accessing'!
  95. receiver
  96. ^receiver
  97. !
  98. receiver: aNode
  99. receiver := aNode
  100. ! !
  101. !CascadeNode methodsFor: 'visiting'!
  102. accept: aVisitor
  103. aVisitor visitCascadeNode: self
  104. ! !
  105. Node subclass: #AssignmentNode
  106. instanceVariableNames: 'left right'
  107. category: 'Compiler'!
  108. !AssignmentNode methodsFor: 'accessing'!
  109. left
  110. ^left
  111. !
  112. left: aNode
  113. left := aNode
  114. !
  115. right
  116. ^right
  117. !
  118. right: aNode
  119. right := aNode
  120. ! !
  121. !AssignmentNode methodsFor: 'visiting'!
  122. accept: aVisitor
  123. aVisitor visitAssignmentNode: self
  124. ! !
  125. Node subclass: #BlockNode
  126. instanceVariableNames: 'parameters'
  127. category: 'Compiler'!
  128. !BlockNode methodsFor: 'accessing'!
  129. parameters
  130. ^parameters ifNil: [parameters := Array new]
  131. !
  132. parameters: aCollection
  133. parameters := aCollection
  134. ! !
  135. !BlockNode methodsFor: 'visiting'!
  136. accept: aVisitor
  137. aVisitor visitBlockNode: self
  138. ! !
  139. Node subclass: #SequenceNode
  140. instanceVariableNames: 'temps'
  141. category: 'Compiler'!
  142. !SequenceNode methodsFor: 'accessing'!
  143. temps
  144. ^temps ifNil: [#()]
  145. !
  146. temps: aCollection
  147. temps := aCollection
  148. ! !
  149. !SequenceNode methodsFor: 'testing'!
  150. asBlockSequenceNode
  151. ^BlockSequenceNode new
  152. nodes: self nodes;
  153. temps: self temps;
  154. yourself
  155. ! !
  156. !SequenceNode methodsFor: 'visiting'!
  157. accept: aVisitor
  158. aVisitor visitSequenceNode: self
  159. ! !
  160. SequenceNode subclass: #BlockSequenceNode
  161. instanceVariableNames: ''
  162. category: 'Compiler'!
  163. !BlockSequenceNode methodsFor: 'visiting'!
  164. accept: aVisitor
  165. aVisitor visitBlockSequenceNode: self
  166. ! !
  167. Node subclass: #ReturnNode
  168. instanceVariableNames: ''
  169. category: 'Compiler'!
  170. !ReturnNode methodsFor: 'visiting'!
  171. accept: aVisitor
  172. aVisitor visitReturnNode: self
  173. ! !
  174. Node subclass: #ValueNode
  175. instanceVariableNames: 'value'
  176. category: 'Compiler'!
  177. !ValueNode methodsFor: 'accessing'!
  178. value
  179. ^value
  180. !
  181. value: anObject
  182. value := anObject
  183. ! !
  184. !ValueNode methodsFor: 'visiting'!
  185. accept: aVisitor
  186. aVisitor visitValueNode: self
  187. ! !
  188. ValueNode subclass: #VariableNode
  189. instanceVariableNames: ''
  190. category: 'Compiler'!
  191. !VariableNode methodsFor: 'visiting'!
  192. accept: aVisitor
  193. aVisitor visitVariableNode: self
  194. ! !
  195. VariableNode subclass: #ClassReferenceNode
  196. instanceVariableNames: ''
  197. category: 'Compiler'!
  198. !ClassReferenceNode methodsFor: 'visiting'!
  199. accept: aVisitor
  200. aVisitor visitClassReferenceNode: self
  201. ! !
  202. Node subclass: #JSStatementNode
  203. instanceVariableNames: 'source'
  204. category: 'Compiler'!
  205. !JSStatementNode methodsFor: 'accessing'!
  206. source
  207. ^source ifNil: ['']
  208. !
  209. source: aString
  210. source := aString
  211. ! !
  212. !JSStatementNode methodsFor: 'visiting'!
  213. accept: aVisitor
  214. aVisitor visitJSStatementNode: self
  215. ! !
  216. Object subclass: #NodeVisitor
  217. instanceVariableNames: ''
  218. category: 'Compiler'!
  219. !NodeVisitor methodsFor: 'visiting'!
  220. visit: aNode
  221. aNode accept: self
  222. !
  223. visitNode: aNode
  224. !
  225. visitMethodNode: aNode
  226. self visitNode: aNode
  227. !
  228. visitSequenceNode: aNode
  229. self visitNode: aNode
  230. !
  231. visitBlockSequenceNode: aNode
  232. self visitSequenceNode: aNode
  233. !
  234. visitBlockNode: aNode
  235. self visitNode: aNode
  236. !
  237. visitReturnNode: aNode
  238. self visitNode: aNode
  239. !
  240. visitSendNode: aNode
  241. self visitNode: aNode
  242. !
  243. visitCascadeNode: aNode
  244. self visitNode: aNode
  245. !
  246. visitValueNode: aNode
  247. self visitNode: aNode
  248. !
  249. visitVariableNode: aNode
  250. !
  251. visitAssignmentNode: aNode
  252. self visitNode: aNode
  253. !
  254. visitClassReferenceNode: aNode
  255. self
  256. nextPutAll: 'smalltalk.';
  257. nextPutAll: aNode value
  258. !
  259. visitJSStatementNode: aNode
  260. self
  261. nextPutAll: 'function(){';
  262. nextPutAll: aNode source;
  263. nextPutAll: '})()'
  264. ! !
  265. NodeVisitor subclass: #Compiler
  266. instanceVariableNames: 'stream nestedBlocks earlyReturn currentClass currentSelector unknownVariables tempVariables messageSends referencedClasses'
  267. category: 'Compiler'!
  268. !Compiler methodsFor: 'accessing'!
  269. parser
  270. ^SmalltalkParser new
  271. !
  272. currentClass
  273. ^currentClass
  274. !
  275. currentClass: aClass
  276. currentClass := aClass
  277. !
  278. unknownVariables
  279. ^unknownVariables copy
  280. !
  281. pseudoVariables
  282. ^#('self' 'super' 'true' 'false' 'nil' 'thisContext')
  283. !
  284. tempVariables
  285. ^tempVariables copy
  286. !
  287. knownVariables
  288. ^self pseudoVariables
  289. addAll: self tempVariables;
  290. yourself
  291. !
  292. classNameFor: aClass
  293. ^aClass isMetaclass
  294. ifTrue: [aClass instanceClass name, '.klass']
  295. ifFalse: [
  296. aClass isNil
  297. ifTrue: ['nil']
  298. ifFalse: [aClass name]]
  299. ! !
  300. !Compiler methodsFor: 'compiling'!
  301. loadExpression: aString
  302. DoIt addCompiledMethod: (self eval: (self compileExpression: aString)).
  303. ^DoIt new doIt
  304. !
  305. load: aString forClass: aClass
  306. | compiled |
  307. compiled := self eval: (self compile: aString forClass: aClass).
  308. self setupClass: aClass.
  309. ^compiled
  310. !
  311. compile: aString forClass: aClass
  312. self currentClass: aClass.
  313. ^self compile: aString
  314. !
  315. compileExpression: aString
  316. self currentClass: DoIt.
  317. ^self compileNode: (self parseExpression: aString)
  318. !
  319. eval: aString
  320. <return eval(aString)>
  321. !
  322. compile: aString
  323. ^self compileNode: (self parse: aString)
  324. !
  325. compileNode: aNode
  326. stream := '' writeStream.
  327. self visit: aNode.
  328. ^stream contents
  329. !
  330. parse: aString
  331. ^self parser parse: aString readStream
  332. !
  333. parseExpression: aString
  334. ^self parse: 'doIt ^[', aString, '] value'
  335. !
  336. recompile: aClass
  337. aClass methodDictionary do: [:each || method |
  338. method := self load: each source forClass: aClass.
  339. method category: each category.
  340. aClass addCompiledMethod: method].
  341. aClass isMetaclass ifFalse: [self recompile: aClass class]
  342. !
  343. recompileAll
  344. Smalltalk current classes do: [:each |
  345. self recompile: each]
  346. !
  347. setupClass: aClass
  348. <smalltalk.init(aClass)>
  349. ! !
  350. !Compiler methodsFor: 'initialization'!
  351. initialize
  352. super initialize.
  353. stream := '' writeStream.
  354. unknownVariables := #().
  355. tempVariables := #().
  356. messageSends := #().
  357. classReferenced := #()
  358. ! !
  359. !Compiler methodsFor: 'visiting'!
  360. visit: aNode
  361. aNode accept: self
  362. !
  363. visitMethodNode: aNode
  364. | str currentSelector |
  365. currentSelector := aNode selector asSelector.
  366. nestedBlocks := 0.
  367. earlyReturn := false.
  368. messageSends := #().
  369. referencedClasses := #().
  370. unknownVariables := #().
  371. tempVariables := #().
  372. stream
  373. nextPutAll: 'smalltalk.method({'; lf;
  374. nextPutAll: 'selector: "', aNode selector, '",'; lf.
  375. Smalltalk current debugMode ifTrue: [
  376. stream nextPutAll: 'source: unescape("', aNode source escaped, '"),';lf].
  377. stream nextPutAll: 'fn: function('.
  378. aNode arguments
  379. do: [:each |
  380. tempVariables add: each.
  381. stream nextPutAll: each]
  382. separatedBy: [stream nextPutAll: ', '].
  383. stream
  384. nextPutAll: '){'; lf;
  385. nextPutAll: 'var self=this;'; lf.
  386. str := stream.
  387. stream := '' writeStream.
  388. aNode nodes do: [:each |
  389. self visit: each].
  390. earlyReturn ifTrue: [
  391. str nextPutAll: 'try{'].
  392. str nextPutAll: stream contents.
  393. stream := str.
  394. stream
  395. lf;
  396. nextPutAll: 'return self;'.
  397. earlyReturn ifTrue: [
  398. stream lf; nextPutAll: '} catch(e) {if(e.name === ''stReturn'' && e.selector === ', currentSelector printString, '){return e.fn()} throw(e)}'].
  399. stream nextPutAll: '}'.
  400. Smalltalk current debugMode ifTrue: [
  401. stream
  402. nextPutAll: ',', String lf, 'messageSends: ';
  403. nextPutAll: messageSends asJavascript, ','; lf;
  404. nextPutAll: 'referencedClasses: ['.
  405. referencedClasses
  406. do: [:each | stream nextPutAll: each]
  407. separatedBy: [stream nextPutAll: ','].
  408. stream nextPutAll: ']'].
  409. stream nextPutAll: '})'
  410. !
  411. visitBlockNode: aNode
  412. stream nextPutAll: '(function('.
  413. aNode parameters
  414. do: [:each |
  415. tempVariables add: each.
  416. stream nextPutAll: each]
  417. separatedBy: [stream nextPutAll: ', '].
  418. stream nextPutAll: '){'.
  419. aNode nodes do: [:each | self visit: each].
  420. stream nextPutAll: '})'
  421. !
  422. visitSequenceNode: aNode
  423. aNode temps do: [:each |
  424. tempVariables add: each.
  425. stream nextPutAll: 'var ', each, '=nil;'; lf].
  426. aNode nodes do: [:each |
  427. self visit: each.
  428. stream nextPutAll: ';']
  429. separatedBy: [stream lf]
  430. !
  431. visitBlockSequenceNode: aNode
  432. | index |
  433. nestedBlocks := nestedBlocks + 1.
  434. aNode nodes isEmpty
  435. ifTrue: [
  436. stream nextPutAll: 'return nil;']
  437. ifFalse: [
  438. aNode temps do: [:each |
  439. tempVariables add: each.
  440. stream nextPutAll: 'var ', each, '=nil;'; lf].
  441. index := 0.
  442. aNode nodes do: [:each |
  443. index := index + 1.
  444. index = aNode nodes size ifTrue: [
  445. stream nextPutAll: 'return '].
  446. self visit: each.
  447. stream nextPutAll: ';']].
  448. nestedBlocks := nestedBlocks - 1
  449. !
  450. visitReturnNode: aNode
  451. nestedBlocks > 0 ifTrue: [
  452. earlyReturn := true].
  453. earlyReturn
  454. ifTrue: [
  455. stream
  456. nextPutAll: '(function(){throw(';
  457. nextPutAll: '{name: ''stReturn'', selector: ';
  458. nextPutAll: currentSelector printString;
  459. nextPutAll: ', fn: function(){return ']
  460. ifFalse: [stream nextPutAll: 'return '].
  461. aNode nodes do: [:each |
  462. self visit: each].
  463. earlyReturn ifTrue: [
  464. stream nextPutAll: '}})})()']
  465. !
  466. visitSendNode: aNode
  467. | str receiver superSend |
  468. str := stream.
  469. (messageSends includes: aNode selector) ifFalse: [
  470. messageSends add: aNode selector].
  471. stream := '' writeStream.
  472. self visit: aNode receiver.
  473. superSend := stream contents = 'super'.
  474. receiver := superSend ifTrue: ['self'] ifFalse: [stream contents].
  475. str nextPutAll: 'smalltalk.send('.
  476. str nextPutAll: receiver.
  477. stream := str.
  478. stream nextPutAll: ', "', aNode selector asSelector, '", ['.
  479. aNode arguments
  480. do: [:each | self visit: each]
  481. separatedBy: [stream nextPutAll: ', '].
  482. stream nextPutAll: ']'.
  483. superSend ifTrue: [
  484. stream nextPutAll: ', smalltalk.', (self classNameFor: self currentClass superclass)].
  485. stream nextPutAll: ')'
  486. !
  487. visitCascadeNode: aNode
  488. | index |
  489. index := 0.
  490. (tempVariables includes: '$rec') ifFalse: [
  491. tempVariables add: '$rec'].
  492. stream nextPutAll: '(function($rec){'.
  493. aNode nodes do: [:each |
  494. index := index + 1.
  495. index = aNode nodes size ifTrue: [
  496. stream nextPutAll: 'return '].
  497. each receiver: (VariableNode new value: '$rec').
  498. self visit: each.
  499. stream nextPutAll: ';'].
  500. stream nextPutAll: '})('.
  501. self visit: aNode receiver.
  502. stream nextPutAll: ')'
  503. !
  504. visitValueNode: aNode
  505. stream nextPutAll: aNode value asJavascript
  506. !
  507. visitAssignmentNode: aNode
  508. self visit: aNode left.
  509. stream nextPutAll: '='.
  510. self visit: aNode right
  511. !
  512. visitClassReferenceNode: aNode
  513. | klass |
  514. klass := 'smalltalk.', aNode value.
  515. (Smalltalk current at: aNode value) isClass ifTrue: [
  516. (referencedClasses includes: klass)
  517. ifFalse: [referencedClasses add: klass]].
  518. stream nextPutAll: klass
  519. !
  520. visitVariableNode: aNode
  521. (self currentClass instanceVariableNames includes: aNode value)
  522. ifTrue: [stream nextPutAll: 'self[''@', aNode value, ''']']
  523. ifFalse: [
  524. (self knownVariables includes: aNode value) ifFalse: [
  525. unknownVariables add: aNode value].
  526. stream nextPutAll: aNode value]
  527. !
  528. visitJSStatementNode: aNode
  529. stream nextPutAll: (aNode source replace: '>>' with: '>')
  530. !
  531. visitFailure: aFailure
  532. self error: aFailure asString
  533. ! !
  534. !Compiler class methodsFor: 'compiling'!
  535. recompile: aClass
  536. aClass methodDictionary do: [:each || method |
  537. method := self new load: each source forClass: aClass.
  538. method category: each category.
  539. aClass addCompiledMethod: method].
  540. aClass isMetaclass ifFalse: [self recompile: aClass class]
  541. !
  542. recompileAll
  543. Smalltalk current classes do: [:each |
  544. self recompile: each]
  545. ! !
  546. Object subclass: #DoIt
  547. instanceVariableNames: ''
  548. category: 'Compiler'!
  549. !DoIt methodsFor: ''!
  550. doIt ^[<self["@"+'foo']>] value
  551. ! !