Compiler.st 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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: '() : $receiver'.
  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: 'testing'!
  608. performOptimizations
  609. ^self class performOptimizations
  610. ! !
  611. !Compiler methodsFor: 'visiting'!
  612. visit: aNode
  613. aNode accept: self
  614. !
  615. visitMethodNode: aNode
  616. | str currentSelector |
  617. currentSelector := aNode selector asSelector.
  618. nestedBlocks := 0.
  619. earlyReturn := false.
  620. messageSends := #().
  621. referencedClasses := #().
  622. unknownVariables := #().
  623. tempVariables := #().
  624. stream
  625. nextPutAll: 'smalltalk.method({'; lf;
  626. nextPutAll: 'selector: "', aNode selector, '",'; lf.
  627. stream nextPutAll: 'source: unescape("', aNode source escaped, '"),';lf.
  628. stream nextPutAll: 'fn: function('.
  629. aNode arguments
  630. do: [:each |
  631. tempVariables add: each.
  632. stream nextPutAll: each]
  633. separatedBy: [stream nextPutAll: ', '].
  634. stream
  635. nextPutAll: '){'; lf;
  636. nextPutAll: 'var self=this;'; lf.
  637. str := stream.
  638. stream := '' writeStream.
  639. aNode nodes do: [:each |
  640. self visit: each].
  641. earlyReturn ifTrue: [
  642. str nextPutAll: 'try{'].
  643. str nextPutAll: stream contents.
  644. stream := str.
  645. stream
  646. lf;
  647. nextPutAll: 'return self;'.
  648. earlyReturn ifTrue: [
  649. stream lf; nextPutAll: '} catch(e) {if(e.name === ''stReturn'' && e.selector === ', currentSelector printString, '){return e.fn()} throw(e)}'].
  650. stream nextPutAll: '}'.
  651. stream
  652. nextPutAll: ',', String lf, 'messageSends: ';
  653. nextPutAll: messageSends asJavascript, ','; lf;
  654. nextPutAll: 'referencedClasses: ['.
  655. referencedClasses
  656. do: [:each | stream nextPutAll: each]
  657. separatedBy: [stream nextPutAll: ','].
  658. stream nextPutAll: ']'.
  659. stream nextPutAll: '})'
  660. !
  661. visitBlockNode: aNode
  662. stream nextPutAll: '(function('.
  663. aNode parameters
  664. do: [:each |
  665. tempVariables add: each.
  666. stream nextPutAll: each]
  667. separatedBy: [stream nextPutAll: ', '].
  668. stream nextPutAll: '){'.
  669. aNode nodes do: [:each | self visit: each].
  670. stream nextPutAll: '})'
  671. !
  672. visitSequenceNode: aNode
  673. aNode temps do: [:each |
  674. tempVariables add: each.
  675. stream nextPutAll: 'var ', each, '=nil;'; lf].
  676. aNode nodes do: [:each |
  677. self visit: each.
  678. stream nextPutAll: ';']
  679. separatedBy: [stream lf]
  680. !
  681. visitBlockSequenceNode: aNode
  682. | index |
  683. nestedBlocks := nestedBlocks + 1.
  684. aNode nodes isEmpty
  685. ifTrue: [
  686. stream nextPutAll: 'return nil;']
  687. ifFalse: [
  688. aNode temps do: [:each |
  689. tempVariables add: each.
  690. stream nextPutAll: 'var ', each, '=nil;'; lf].
  691. index := 0.
  692. aNode nodes do: [:each |
  693. index := index + 1.
  694. index = aNode nodes size ifTrue: [
  695. stream nextPutAll: 'return '].
  696. self visit: each.
  697. stream nextPutAll: ';']].
  698. nestedBlocks := nestedBlocks - 1
  699. !
  700. visitReturnNode: aNode
  701. nestedBlocks > 0 ifTrue: [
  702. earlyReturn := true].
  703. earlyReturn
  704. ifTrue: [
  705. stream
  706. nextPutAll: '(function(){throw(';
  707. nextPutAll: '{name: ''stReturn'', selector: ';
  708. nextPutAll: currentSelector printString;
  709. nextPutAll: ', fn: function(){return ']
  710. ifFalse: [stream nextPutAll: 'return '].
  711. aNode nodes do: [:each |
  712. self visit: each].
  713. earlyReturn ifTrue: [
  714. stream nextPutAll: '}})})()']
  715. !
  716. visitSendNode: aNode
  717. | str receiver superSend inlined |
  718. str := stream.
  719. (messageSends includes: aNode selector) ifFalse: [
  720. messageSends add: aNode selector].
  721. stream := '' writeStream.
  722. self visit: aNode receiver.
  723. superSend := stream contents = 'super'.
  724. receiver := superSend ifTrue: ['self'] ifFalse: [stream contents].
  725. stream := str.
  726. self performOptimizations
  727. ifTrue: [
  728. (self inlineLiteral: aNode selector receiverNode: aNode receiver argumentNodes: aNode arguments) ifFalse: [
  729. (self inline: aNode selector receiver: receiver argumentNodes: aNode arguments)
  730. ifTrue: [stream nextPutAll: ' : ', (self send: aNode selector to: '$receiver' arguments: aNode arguments superSend: superSend)]
  731. ifFalse: [stream nextPutAll: (self send: aNode selector to: receiver arguments: aNode arguments superSend: superSend)]]]
  732. ifFalse: [stream nextPutAll: (self send: aNode selector to: receiver arguments: aNode arguments superSend: superSend)]
  733. !
  734. visitCascadeNode: aNode
  735. | index |
  736. index := 0.
  737. (tempVariables includes: '$rec') ifFalse: [
  738. tempVariables add: '$rec'].
  739. stream nextPutAll: '(function($rec){'.
  740. aNode nodes do: [:each |
  741. index := index + 1.
  742. index = aNode nodes size ifTrue: [
  743. stream nextPutAll: 'return '].
  744. each receiver: (VariableNode new value: '$rec').
  745. self visit: each.
  746. stream nextPutAll: ';'].
  747. stream nextPutAll: '})('.
  748. self visit: aNode receiver.
  749. stream nextPutAll: ')'
  750. !
  751. visitValueNode: aNode
  752. stream nextPutAll: aNode value asJavascript
  753. !
  754. visitAssignmentNode: aNode
  755. self visit: aNode left.
  756. stream nextPutAll: '='.
  757. self visit: aNode right
  758. !
  759. visitClassReferenceNode: aNode
  760. | klass |
  761. klass := 'smalltalk.', aNode value.
  762. (Smalltalk current at: aNode value) isClass ifTrue: [
  763. (referencedClasses includes: klass)
  764. ifFalse: [referencedClasses add: klass]].
  765. stream nextPutAll: klass
  766. !
  767. visitVariableNode: aNode
  768. (self currentClass allInstanceVariableNames includes: aNode value)
  769. ifTrue: [stream nextPutAll: 'self[''@', aNode value, ''']']
  770. ifFalse: [
  771. (self knownVariables includes: aNode value) ifFalse: [
  772. unknownVariables add: aNode value].
  773. stream nextPutAll: aNode value]
  774. !
  775. visitJSStatementNode: aNode
  776. stream nextPutAll: (aNode source replace: '>>' with: '>')
  777. !
  778. visitFailure: aFailure
  779. self error: aFailure asString
  780. !
  781. send: aSelector to: aReceiver arguments: aCollection superSend: aBoolean
  782. ^String streamContents: [:str || tmp |
  783. tmp := stream.
  784. str nextPutAll: 'smalltalk.send('.
  785. str nextPutAll: aReceiver.
  786. str nextPutAll: ', "', aSelector asSelector, '", ['.
  787. stream := str.
  788. aCollection
  789. do: [:each | self visit: each]
  790. separatedBy: [stream nextPutAll: ', '].
  791. stream := tmp.
  792. str nextPutAll: ']'.
  793. aBoolean ifTrue: [
  794. str nextPutAll: ', smalltalk.', (self classNameFor: self currentClass superclass)].
  795. str nextPutAll: ')']
  796. ! !
  797. Compiler class instanceVariableNames: 'performOptimizations'!
  798. !Compiler class methodsFor: 'accessing'!
  799. performOptimizations
  800. ^performOptimizations ifNil: [true]
  801. !
  802. performOptimizations: aBoolean
  803. performOptimizations := aBoolean
  804. ! !
  805. !Compiler class methodsFor: 'compiling'!
  806. recompile: aClass
  807. aClass methodDictionary do: [:each || method |
  808. method := self new load: each source forClass: aClass.
  809. method category: each category.
  810. aClass addCompiledMethod: method].
  811. aClass isMetaclass ifFalse: [self recompile: aClass class]
  812. !
  813. recompileAll
  814. Smalltalk current classes do: [:each |
  815. self recompile: each]
  816. ! !
  817. Object subclass: #DoIt
  818. instanceVariableNames: ''
  819. category: 'Compiler'!
  820. !DoIt methodsFor: ''!
  821. doIt ^[Compiler performOptimizations: false.] value
  822. ! !