Compiler.st 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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: 'testing'!
  16. isValueNode
  17. ^false
  18. !
  19. isBlockNode
  20. ^false
  21. !
  22. isBlockSequenceNode
  23. ^false
  24. ! !
  25. !Node methodsFor: 'visiting'!
  26. accept: aVisitor
  27. aVisitor visitNode: self
  28. ! !
  29. Node subclass: #MethodNode
  30. instanceVariableNames: 'selector arguments source'
  31. category: 'Compiler'!
  32. !MethodNode methodsFor: 'accessing'!
  33. selector
  34. ^selector
  35. !
  36. selector: aString
  37. selector := aString
  38. !
  39. arguments
  40. ^arguments ifNil: [#()]
  41. !
  42. arguments: aCollection
  43. arguments := aCollection
  44. !
  45. source
  46. ^source
  47. !
  48. source: aString
  49. source := aString
  50. ! !
  51. !MethodNode methodsFor: 'visiting'!
  52. accept: aVisitor
  53. aVisitor visitMethodNode: self
  54. ! !
  55. Node subclass: #SendNode
  56. instanceVariableNames: 'selector arguments receiver'
  57. category: 'Compiler'!
  58. !SendNode methodsFor: 'accessing'!
  59. selector
  60. ^selector
  61. !
  62. selector: aString
  63. selector := aString
  64. !
  65. arguments
  66. ^arguments ifNil: [arguments := #()]
  67. !
  68. arguments: aCollection
  69. arguments := aCollection
  70. !
  71. receiver
  72. ^receiver
  73. !
  74. receiver: aNode
  75. receiver := aNode
  76. !
  77. valueForReceiver: anObject
  78. ^SendNode new
  79. receiver: (self receiver
  80. ifNil: [anObject]
  81. ifNotNil: [self receiver valueForReceiver: anObject]);
  82. selector: self selector;
  83. arguments: self arguments;
  84. yourself
  85. !
  86. cascadeNodeWithMessages: aCollection
  87. | first |
  88. first := SendNode new
  89. selector: self selector;
  90. arguments: self arguments;
  91. yourself.
  92. ^CascadeNode new
  93. receiver: self receiver;
  94. nodes: (Array with: first), aCollection;
  95. yourself
  96. ! !
  97. !SendNode methodsFor: 'visiting'!
  98. accept: aVisitor
  99. aVisitor visitSendNode: self
  100. ! !
  101. Node subclass: #CascadeNode
  102. instanceVariableNames: 'receiver'
  103. category: 'Compiler'!
  104. !CascadeNode methodsFor: 'accessing'!
  105. receiver
  106. ^receiver
  107. !
  108. receiver: aNode
  109. receiver := aNode
  110. ! !
  111. !CascadeNode methodsFor: 'visiting'!
  112. accept: aVisitor
  113. aVisitor visitCascadeNode: self
  114. ! !
  115. Node subclass: #AssignmentNode
  116. instanceVariableNames: 'left right'
  117. category: 'Compiler'!
  118. !AssignmentNode methodsFor: 'accessing'!
  119. left
  120. ^left
  121. !
  122. left: aNode
  123. left := aNode
  124. !
  125. right
  126. ^right
  127. !
  128. right: aNode
  129. right := aNode
  130. ! !
  131. !AssignmentNode methodsFor: 'visiting'!
  132. accept: aVisitor
  133. aVisitor visitAssignmentNode: self
  134. ! !
  135. Node subclass: #BlockNode
  136. instanceVariableNames: 'parameters inlined'
  137. category: 'Compiler'!
  138. !BlockNode methodsFor: 'accessing'!
  139. parameters
  140. ^parameters ifNil: [parameters := Array new]
  141. !
  142. parameters: aCollection
  143. parameters := aCollection
  144. !
  145. inlined
  146. ^inlined ifNil: [false]
  147. !
  148. inlined: aBoolean
  149. inlined := aBoolean
  150. ! !
  151. !BlockNode methodsFor: 'testing'!
  152. isBlockNode
  153. ^true
  154. ! !
  155. !BlockNode methodsFor: 'visiting'!
  156. accept: aVisitor
  157. aVisitor visitBlockNode: self
  158. ! !
  159. Node subclass: #SequenceNode
  160. instanceVariableNames: 'temps'
  161. category: 'Compiler'!
  162. !SequenceNode methodsFor: 'accessing'!
  163. temps
  164. ^temps ifNil: [#()]
  165. !
  166. temps: aCollection
  167. temps := aCollection
  168. ! !
  169. !SequenceNode methodsFor: 'testing'!
  170. asBlockSequenceNode
  171. ^BlockSequenceNode new
  172. nodes: self nodes;
  173. temps: self temps;
  174. yourself
  175. ! !
  176. !SequenceNode methodsFor: 'visiting'!
  177. accept: aVisitor
  178. aVisitor visitSequenceNode: self
  179. ! !
  180. SequenceNode subclass: #BlockSequenceNode
  181. instanceVariableNames: ''
  182. category: 'Compiler'!
  183. !BlockSequenceNode methodsFor: 'testing'!
  184. isBlockSequenceNode
  185. ^true
  186. ! !
  187. !BlockSequenceNode methodsFor: 'visiting'!
  188. accept: aVisitor
  189. aVisitor visitBlockSequenceNode: self
  190. ! !
  191. Node subclass: #ReturnNode
  192. instanceVariableNames: ''
  193. category: 'Compiler'!
  194. !ReturnNode methodsFor: 'visiting'!
  195. accept: aVisitor
  196. aVisitor visitReturnNode: self
  197. ! !
  198. Node subclass: #ValueNode
  199. instanceVariableNames: 'value'
  200. category: 'Compiler'!
  201. !ValueNode methodsFor: 'accessing'!
  202. value
  203. ^value
  204. !
  205. value: anObject
  206. value := anObject
  207. ! !
  208. !ValueNode methodsFor: 'testing'!
  209. isValueNode
  210. ^true
  211. ! !
  212. !ValueNode methodsFor: 'visiting'!
  213. accept: aVisitor
  214. aVisitor visitValueNode: self
  215. ! !
  216. ValueNode subclass: #VariableNode
  217. instanceVariableNames: ''
  218. category: 'Compiler'!
  219. !VariableNode methodsFor: 'visiting'!
  220. accept: aVisitor
  221. aVisitor visitVariableNode: self
  222. ! !
  223. VariableNode subclass: #ClassReferenceNode
  224. instanceVariableNames: ''
  225. category: 'Compiler'!
  226. !ClassReferenceNode methodsFor: 'visiting'!
  227. accept: aVisitor
  228. aVisitor visitClassReferenceNode: self
  229. ! !
  230. Node subclass: #JSStatementNode
  231. instanceVariableNames: 'source'
  232. category: 'Compiler'!
  233. !JSStatementNode methodsFor: 'accessing'!
  234. source
  235. ^source ifNil: ['']
  236. !
  237. source: aString
  238. source := aString
  239. ! !
  240. !JSStatementNode methodsFor: 'visiting'!
  241. accept: aVisitor
  242. aVisitor visitJSStatementNode: self
  243. ! !
  244. Object subclass: #NodeVisitor
  245. instanceVariableNames: ''
  246. category: 'Compiler'!
  247. !NodeVisitor methodsFor: 'visiting'!
  248. visit: aNode
  249. aNode accept: self
  250. !
  251. visitNode: aNode
  252. !
  253. visitMethodNode: aNode
  254. self visitNode: aNode
  255. !
  256. visitSequenceNode: aNode
  257. self visitNode: aNode
  258. !
  259. visitBlockSequenceNode: aNode
  260. self visitSequenceNode: aNode
  261. !
  262. visitBlockNode: aNode
  263. self visitNode: aNode
  264. !
  265. visitReturnNode: aNode
  266. self visitNode: aNode
  267. !
  268. visitSendNode: aNode
  269. self visitNode: aNode
  270. !
  271. visitCascadeNode: aNode
  272. self visitNode: aNode
  273. !
  274. visitValueNode: aNode
  275. self visitNode: aNode
  276. !
  277. visitVariableNode: aNode
  278. !
  279. visitAssignmentNode: aNode
  280. self visitNode: aNode
  281. !
  282. visitClassReferenceNode: aNode
  283. self
  284. nextPutAll: 'smalltalk.';
  285. nextPutAll: aNode value
  286. !
  287. visitJSStatementNode: aNode
  288. self
  289. nextPutAll: 'function(){';
  290. nextPutAll: aNode source;
  291. nextPutAll: '})()'
  292. ! !
  293. NodeVisitor subclass: #Compiler
  294. instanceVariableNames: 'stream nestedBlocks earlyReturn currentClass currentSelector unknownVariables tempVariables messageSends referencedClasses'
  295. category: 'Compiler'!
  296. !Compiler methodsFor: 'accessing'!
  297. parser
  298. ^SmalltalkParser new
  299. !
  300. currentClass
  301. ^currentClass
  302. !
  303. currentClass: aClass
  304. currentClass := aClass
  305. !
  306. unknownVariables
  307. ^unknownVariables copy
  308. !
  309. pseudoVariables
  310. ^#('self' 'super' 'true' 'false' 'nil' 'thisContext')
  311. !
  312. tempVariables
  313. ^tempVariables copy
  314. !
  315. knownVariables
  316. ^self pseudoVariables
  317. addAll: self tempVariables;
  318. yourself
  319. !
  320. classNameFor: aClass
  321. ^aClass isMetaclass
  322. ifTrue: [aClass instanceClass name, '.klass']
  323. ifFalse: [
  324. aClass isNil
  325. ifTrue: ['nil']
  326. ifFalse: [aClass name]]
  327. ! !
  328. !Compiler methodsFor: 'compiling'!
  329. loadExpression: aString
  330. DoIt addCompiledMethod: (self eval: (self compileExpression: aString)).
  331. ^DoIt new doIt
  332. !
  333. load: aString forClass: aClass
  334. | compiled |
  335. compiled := self eval: (self compile: aString forClass: aClass).
  336. self setupClass: aClass.
  337. ^compiled
  338. !
  339. compile: aString forClass: aClass
  340. self currentClass: aClass.
  341. ^self compile: aString
  342. !
  343. compileExpression: aString
  344. self currentClass: DoIt.
  345. ^self compileNode: (self parseExpression: aString)
  346. !
  347. eval: aString
  348. <return eval(aString)>
  349. !
  350. compile: aString
  351. ^self compileNode: (self parse: aString)
  352. !
  353. compileNode: aNode
  354. stream := '' writeStream.
  355. self visit: aNode.
  356. ^stream contents
  357. !
  358. parse: aString
  359. ^self parser parse: aString readStream
  360. !
  361. parseExpression: aString
  362. ^self parse: 'doIt ^[', aString, '] value'
  363. !
  364. recompile: aClass
  365. aClass methodDictionary do: [:each || method |
  366. method := self load: each source forClass: aClass.
  367. method category: each category.
  368. aClass addCompiledMethod: method].
  369. aClass isMetaclass ifFalse: [self recompile: aClass class]
  370. !
  371. recompileAll
  372. Smalltalk current classes do: [:each |
  373. Transcript show: each; cr.
  374. [self recompile: each] valueWithTimeout: 100]
  375. !
  376. setupClass: aClass
  377. <smalltalk.init(aClass)>
  378. ! !
  379. !Compiler methodsFor: 'initialization'!
  380. initialize
  381. super initialize.
  382. stream := '' writeStream.
  383. unknownVariables := #().
  384. tempVariables := #().
  385. messageSends := #().
  386. classReferenced := #()
  387. ! !
  388. !Compiler methodsFor: 'optimizations'!
  389. checkClass: aClassName for: receiver
  390. stream nextPutAll: '(($receiver = ', receiver, ').klass === smalltalk.', aClassName, ') ? '
  391. !
  392. inlineLiteral: aSelector receiverNode: anObject argumentNodes: aCollection
  393. | inlined |
  394. inlined := false.
  395. "-- BlockClosures --"
  396. (aSelector = 'whileTrue:') ifTrue: [
  397. (anObject isBlockNode and: [aCollection first isBlockNode]) ifTrue: [
  398. stream nextPutAll: '(function(){while('.
  399. self visit: anObject.
  400. stream nextPutAll: '()) {'.
  401. self visit: aCollection first.
  402. stream nextPutAll: '()}})()'.
  403. inlined := true]].
  404. (aSelector = 'whileFalse:') ifTrue: [
  405. (anObject isBlockNode and: [aCollection first isBlockNode]) ifTrue: [
  406. stream nextPutAll: '(function(){while(!!'.
  407. self visit: anObject.
  408. stream nextPutAll: '()) {'.
  409. self visit: aCollection first.
  410. stream nextPutAll: '()}})()'.
  411. inlined := true]].
  412. (aSelector = 'whileTrue') ifTrue: [
  413. anObject isBlockNode ifTrue: [
  414. stream nextPutAll: '(function(){while('.
  415. self visit: anObject.
  416. stream nextPutAll: '()) {}})()'.
  417. inlined := true]].
  418. (aSelector = 'whileFalse') ifTrue: [
  419. anObject isBlockNode ifTrue: [
  420. stream nextPutAll: '(function(){while(!!'.
  421. self visit: anObject.
  422. stream nextPutAll: '()) {}})()'.
  423. inlined := true]].
  424. "-- Numbers --"
  425. (aSelector = '+') ifTrue: [
  426. (self isNode: anObject ofClass: Number) ifTrue: [
  427. self visit: anObject.
  428. stream nextPutAll: ' + '.
  429. self visit: aCollection first.
  430. inlined := true]].
  431. (aSelector = '-') ifTrue: [
  432. (self isNode: anObject ofClass: Number) ifTrue: [
  433. self visit: anObject.
  434. stream nextPutAll: ' - '.
  435. self visit: aCollection first.
  436. inlined := true]].
  437. (aSelector = '*') ifTrue: [
  438. (self isNode: anObject ofClass: Number) ifTrue: [
  439. self visit: anObject.
  440. stream nextPutAll: ' * '.
  441. self visit: aCollection first.
  442. inlined := true]].
  443. (aSelector = '/') ifTrue: [
  444. (self isNode: anObject ofClass: Number) ifTrue: [
  445. self visit: anObject.
  446. stream nextPutAll: ' / '.
  447. self visit: aCollection first.
  448. inlined := true]].
  449. (aSelector = '<') ifTrue: [
  450. (self isNode: anObject ofClass: Number) ifTrue: [
  451. self visit: anObject.
  452. stream nextPutAll: ' < '.
  453. self visit: aCollection first.
  454. inlined := true]].
  455. (aSelector = '<=') ifTrue: [
  456. (self isNode: anObject ofClass: Number) ifTrue: [
  457. self visit: anObject.
  458. stream nextPutAll: ' <= '.
  459. self visit: aCollection first.
  460. inlined := true]].
  461. (aSelector = '=') ifTrue: [
  462. (self isNode: anObject ofClass: Number) ifTrue: [
  463. self visit: anObject.
  464. stream nextPutAll: ' = '.
  465. self visit: aCollection first.
  466. inlined := true]].
  467. (aSelector = '>') ifTrue: [
  468. (self isNode: anObject ofClass: Number) ifTrue: [
  469. self visit: anObject.
  470. stream nextPutAll: ' > '.
  471. self visit: aCollection first.
  472. inlined := true]].
  473. (aSelector = '>=') ifTrue: [
  474. (self isNode: anObject ofClass: Number) ifTrue: [
  475. self visit: anObject.
  476. stream nextPutAll: ' >= '.
  477. self visit: aCollection first.
  478. inlined := true]].
  479. "-- UndefinedObject --"
  480. (aSelector = 'ifNil:') ifTrue: [
  481. aCollection first isBlockNode ifTrue: [
  482. stream nextPutAll: '(($receiver = '.
  483. self visit: anObject.
  484. stream nextPutAll: ') == nil || $receiver == undefined) ? '.
  485. self visit: aCollection first.
  486. stream nextPutAll: '() : nil'.
  487. inlined := true]].
  488. (aSelector = 'ifNotNil:') ifTrue: [
  489. aCollection first isBlockNode ifTrue: [
  490. stream nextPutAll: '(($receiver = '.
  491. self visit: anObject.
  492. stream nextPutAll: ') !!= nil && $receiver !!= undefined) ? '.
  493. self visit: aCollection first.
  494. stream nextPutAll: '() : nil'.
  495. inlined := true]].
  496. (aSelector = 'ifNil:ifNotNil:') ifTrue: [
  497. (aCollection first isBlockNode and: [aCollection second isBlockNode]) ifTrue: [
  498. stream nextPutAll: '(($receiver = '.
  499. self visit: anObject.
  500. stream nextPutAll: ') == nil || $receiver == undefined) ? '.
  501. self visit: aCollection first.
  502. stream nextPutAll: '() : '.
  503. self visit: aCollection second.
  504. stream nextPutAll: '()'.
  505. inlined := true]].
  506. (aSelector = 'ifNotNil:ifNil:') ifTrue: [
  507. (aCollection first isBlockNode and: [aCollection second isBlockNode]) ifTrue: [
  508. stream nextPutAll: '(($receiver = '.
  509. self visit: anObject.
  510. stream nextPutAll: ') == nil || $receiver == undefined) ? '.
  511. self visit: aCollection second.
  512. stream nextPutAll: '() : '.
  513. self visit: aCollection first.
  514. stream nextPutAll: '()'.
  515. inlined := true]].
  516. ^inlined
  517. !
  518. isNode: aNode ofClass: aClass
  519. ^aNode isValueNode and: [
  520. aNode value class = aClass or: [
  521. aNode value = 'self' and: [self currentClass = aClass]]]
  522. !
  523. inline: aSelector receiver: receiver argumentNodes: aCollection
  524. | inlined |
  525. inlined := false.
  526. "-- Booleans --"
  527. (aSelector = 'ifFalse:') ifTrue: [
  528. aCollection first isBlockNode ifTrue: [
  529. self checkClass: 'Boolean' for: receiver.
  530. stream nextPutAll: '(!! $receiver ? '.
  531. self visit: aCollection first.
  532. stream nextPutAll: '() : nil)'.
  533. inlined := true]].
  534. (aSelector = 'ifTrue:') ifTrue: [
  535. aCollection first isBlockNode ifTrue: [
  536. self checkClass: 'Boolean' for: receiver.
  537. stream nextPutAll: '($receiver ? '.
  538. self visit: aCollection first.
  539. stream nextPutAll: '() : nil)'.
  540. inlined := true]].
  541. (aSelector = 'ifTrue:ifFalse:') ifTrue: [
  542. (aCollection first isBlockNode and: [aCollection second isBlockNode]) ifTrue: [
  543. self checkClass: 'Boolean' for: receiver.
  544. stream nextPutAll: '($receiver ? '.
  545. self visit: aCollection first.
  546. stream nextPutAll: '() : '.
  547. self visit: aCollection second.
  548. stream nextPutAll: '())'.
  549. inlined := true]].
  550. (aSelector = 'ifFalse:ifTrue:') ifTrue: [
  551. (aCollection first isBlockNode and: [aCollection second isBlockNode]) ifTrue: [
  552. self checkClass: 'Boolean' for: receiver.
  553. stream nextPutAll: '(!! $receiver ? '.
  554. self visit: aCollection first.
  555. stream nextPutAll: '() : '.
  556. self visit: aCollection second.
  557. stream nextPutAll: '())'.
  558. inlined := true]].
  559. "-- Numbers --"
  560. (aSelector = '<') ifTrue: [
  561. self checkClass: 'Number' for: receiver.
  562. stream nextPutAll: '$receiver <'.
  563. self visit: aCollection first.
  564. inlined := true].
  565. (aSelector = '<=') ifTrue: [
  566. self checkClass: 'Number' for: receiver.
  567. stream nextPutAll: '$receiver <='.
  568. self visit: aCollection first.
  569. inlined := true].
  570. (aSelector = '=') ifTrue: [
  571. self checkClass: 'Number' for: receiver.
  572. stream nextPutAll: '$receiver =='.
  573. self visit: aCollection first.
  574. inlined := true].
  575. (aSelector = '>') ifTrue: [
  576. self checkClass: 'Number' for: receiver.
  577. stream nextPutAll: '$receiver >'.
  578. self visit: aCollection first.
  579. inlined := true].
  580. (aSelector = '>=') ifTrue: [
  581. self checkClass: 'Number' for: receiver.
  582. stream nextPutAll: '$receiver >='.
  583. self visit: aCollection first.
  584. inlined := true].
  585. (aSelector = '+') ifTrue: [
  586. self checkClass: 'Number' for: receiver.
  587. stream nextPutAll: '$receiver +'.
  588. self visit: aCollection first.
  589. inlined := true].
  590. (aSelector = '-') ifTrue: [
  591. self checkClass: 'Number' for: receiver.
  592. stream nextPutAll: '$receiver -'.
  593. self visit: aCollection first.
  594. inlined := true].
  595. (aSelector = '*') ifTrue: [
  596. self checkClass: 'Number' for: receiver.
  597. stream nextPutAll: '$receiver *'.
  598. self visit: aCollection first.
  599. inlined := true].
  600. (aSelector = '/') ifTrue: [
  601. self checkClass: 'Number' for: receiver.
  602. stream nextPutAll: '$receiver /'.
  603. self visit: aCollection first.
  604. inlined := true].
  605. ^inlined
  606. ! !
  607. !Compiler methodsFor: 'visiting'!
  608. visit: aNode
  609. aNode accept: self
  610. !
  611. visitMethodNode: aNode
  612. | str currentSelector |
  613. currentSelector := aNode selector asSelector.
  614. nestedBlocks := 0.
  615. earlyReturn := false.
  616. messageSends := #().
  617. referencedClasses := #().
  618. unknownVariables := #().
  619. tempVariables := #().
  620. stream
  621. nextPutAll: 'smalltalk.method({'; lf;
  622. nextPutAll: 'selector: "', aNode selector, '",'; lf.
  623. stream nextPutAll: 'source: unescape("', aNode source escaped, '"),';lf.
  624. stream nextPutAll: 'fn: function('.
  625. aNode arguments
  626. do: [:each |
  627. tempVariables add: each.
  628. stream nextPutAll: each]
  629. separatedBy: [stream nextPutAll: ', '].
  630. stream
  631. nextPutAll: '){'; lf;
  632. nextPutAll: 'var self=this;'; lf.
  633. str := stream.
  634. stream := '' writeStream.
  635. aNode nodes do: [:each |
  636. self visit: each].
  637. earlyReturn ifTrue: [
  638. str nextPutAll: 'try{'].
  639. str nextPutAll: stream contents.
  640. stream := str.
  641. stream
  642. lf;
  643. nextPutAll: 'return self;'.
  644. earlyReturn ifTrue: [
  645. stream lf; nextPutAll: '} catch(e) {if(e.name === ''stReturn'' && e.selector === ', currentSelector printString, '){return e.fn()} throw(e)}'].
  646. stream nextPutAll: '}'.
  647. stream
  648. nextPutAll: ',', String lf, 'messageSends: ';
  649. nextPutAll: messageSends asJavascript, ','; lf;
  650. nextPutAll: 'referencedClasses: ['.
  651. referencedClasses
  652. do: [:each | stream nextPutAll: each]
  653. separatedBy: [stream nextPutAll: ','].
  654. stream nextPutAll: ']'.
  655. stream nextPutAll: '})'
  656. !
  657. visitBlockNode: aNode
  658. stream nextPutAll: '(function('.
  659. aNode parameters
  660. do: [:each |
  661. tempVariables add: each.
  662. stream nextPutAll: each]
  663. separatedBy: [stream nextPutAll: ', '].
  664. stream nextPutAll: '){'.
  665. aNode nodes do: [:each | self visit: each].
  666. stream nextPutAll: '})'
  667. !
  668. visitSequenceNode: aNode
  669. aNode temps do: [:each |
  670. tempVariables add: each.
  671. stream nextPutAll: 'var ', each, '=nil;'; lf].
  672. aNode nodes do: [:each |
  673. self visit: each.
  674. stream nextPutAll: ';']
  675. separatedBy: [stream lf]
  676. !
  677. visitBlockSequenceNode: aNode
  678. | index |
  679. nestedBlocks := nestedBlocks + 1.
  680. aNode nodes isEmpty
  681. ifTrue: [
  682. stream nextPutAll: 'return nil;']
  683. ifFalse: [
  684. aNode temps do: [:each |
  685. tempVariables add: each.
  686. stream nextPutAll: 'var ', each, '=nil;'; lf].
  687. index := 0.
  688. aNode nodes do: [:each |
  689. index := index + 1.
  690. index = aNode nodes size ifTrue: [
  691. stream nextPutAll: 'return '].
  692. self visit: each.
  693. stream nextPutAll: ';']].
  694. nestedBlocks := nestedBlocks - 1
  695. !
  696. visitReturnNode: aNode
  697. nestedBlocks > 0 ifTrue: [
  698. earlyReturn := true].
  699. earlyReturn
  700. ifTrue: [
  701. stream
  702. nextPutAll: '(function(){throw(';
  703. nextPutAll: '{name: ''stReturn'', selector: ';
  704. nextPutAll: currentSelector printString;
  705. nextPutAll: ', fn: function(){return ']
  706. ifFalse: [stream nextPutAll: 'return '].
  707. aNode nodes do: [:each |
  708. self visit: each].
  709. earlyReturn ifTrue: [
  710. stream nextPutAll: '}})})()']
  711. !
  712. visitSendNode: aNode
  713. | str receiver superSend inlined |
  714. str := stream.
  715. (messageSends includes: aNode selector) ifFalse: [
  716. messageSends add: aNode selector].
  717. stream := '' writeStream.
  718. self visit: aNode receiver.
  719. superSend := stream contents = 'super'.
  720. receiver := superSend ifTrue: ['self'] ifFalse: [stream contents].
  721. stream := str.
  722. (self inlineLiteral: aNode selector receiverNode: aNode receiver argumentNodes: aNode arguments) ifFalse: [
  723. (self inline: aNode selector receiver: receiver argumentNodes: aNode arguments)
  724. ifTrue: [stream nextPutAll: ' : ', (self send: aNode selector to: '$receiver' arguments: aNode arguments superSend: superSend)]
  725. ifFalse: [stream nextPutAll: (self send: aNode selector to: receiver arguments: aNode arguments superSend: superSend)]]
  726. !
  727. visitCascadeNode: aNode
  728. | index |
  729. index := 0.
  730. (tempVariables includes: '$rec') ifFalse: [
  731. tempVariables add: '$rec'].
  732. stream nextPutAll: '(function($rec){'.
  733. aNode nodes do: [:each |
  734. index := index + 1.
  735. index = aNode nodes size ifTrue: [
  736. stream nextPutAll: 'return '].
  737. each receiver: (VariableNode new value: '$rec').
  738. self visit: each.
  739. stream nextPutAll: ';'].
  740. stream nextPutAll: '})('.
  741. self visit: aNode receiver.
  742. stream nextPutAll: ')'
  743. !
  744. visitValueNode: aNode
  745. stream nextPutAll: aNode value asJavascript
  746. !
  747. visitAssignmentNode: aNode
  748. self visit: aNode left.
  749. stream nextPutAll: '='.
  750. self visit: aNode right
  751. !
  752. visitClassReferenceNode: aNode
  753. | klass |
  754. klass := 'smalltalk.', aNode value.
  755. (Smalltalk current at: aNode value) isClass ifTrue: [
  756. (referencedClasses includes: klass)
  757. ifFalse: [referencedClasses add: klass]].
  758. stream nextPutAll: klass
  759. !
  760. visitVariableNode: aNode
  761. (self currentClass allInstanceVariableNames includes: aNode value)
  762. ifTrue: [stream nextPutAll: 'self[''@', aNode value, ''']']
  763. ifFalse: [
  764. (self knownVariables includes: aNode value) ifFalse: [
  765. unknownVariables add: aNode value].
  766. stream nextPutAll: aNode value]
  767. !
  768. visitJSStatementNode: aNode
  769. stream nextPutAll: (aNode source replace: '>>' with: '>')
  770. !
  771. visitFailure: aFailure
  772. self error: aFailure asString
  773. !
  774. send: aSelector to: aReceiver arguments: aCollection superSend: aBoolean
  775. ^String streamContents: [:str || tmp |
  776. tmp := stream.
  777. str nextPutAll: 'smalltalk.send('.
  778. str nextPutAll: aReceiver.
  779. str nextPutAll: ', "', aSelector asSelector, '", ['.
  780. stream := str.
  781. aCollection
  782. do: [:each | self visit: each]
  783. separatedBy: [stream nextPutAll: ', '].
  784. stream := tmp.
  785. str nextPutAll: ']'.
  786. aBoolean ifTrue: [
  787. str nextPutAll: ', smalltalk.', (self classNameFor: self currentClass superclass)].
  788. str nextPutAll: ')']
  789. ! !
  790. !Compiler class methodsFor: 'compiling'!
  791. recompile: aClass
  792. aClass methodDictionary do: [:each || method |
  793. method := self new load: each source forClass: aClass.
  794. method category: each category.
  795. aClass addCompiledMethod: method].
  796. aClass isMetaclass ifFalse: [self recompile: aClass class]
  797. !
  798. recompileAll
  799. Smalltalk current classes do: [:each |
  800. self recompile: each]
  801. ! !
  802. Object subclass: #DoIt
  803. instanceVariableNames: ''
  804. category: 'Compiler'!
  805. !DoIt methodsFor: ''!
  806. doIt ^[Date millisecondsToRun: [Compiler new compile: (SmalltalkParser methodDictionary at: 'parser') source forClass: SmalltalkParser]] value
  807. ! !