Compiler.st 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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. Transcript show: each; cr.
  346. [self recompile: each] valueWithTimeout: 100]
  347. !
  348. setupClass: aClass
  349. <smalltalk.init(aClass)>
  350. ! !
  351. !Compiler methodsFor: 'initialization'!
  352. initialize
  353. super initialize.
  354. stream := '' writeStream.
  355. unknownVariables := #().
  356. tempVariables := #().
  357. messageSends := #().
  358. classReferenced := #()
  359. ! !
  360. !Compiler methodsFor: 'visiting'!
  361. visit: aNode
  362. aNode accept: self
  363. !
  364. visitMethodNode: aNode
  365. | str currentSelector |
  366. currentSelector := aNode selector asSelector.
  367. nestedBlocks := 0.
  368. earlyReturn := false.
  369. messageSends := #().
  370. referencedClasses := #().
  371. unknownVariables := #().
  372. tempVariables := #().
  373. stream
  374. nextPutAll: 'smalltalk.method({'; lf;
  375. nextPutAll: 'selector: "', aNode selector, '",'; lf.
  376. Smalltalk current debugMode ifTrue: [
  377. stream nextPutAll: 'source: unescape("', aNode source escaped, '"),';lf].
  378. stream nextPutAll: 'fn: function('.
  379. aNode arguments
  380. do: [:each |
  381. tempVariables add: each.
  382. stream nextPutAll: each]
  383. separatedBy: [stream nextPutAll: ', '].
  384. stream
  385. nextPutAll: '){'; lf;
  386. nextPutAll: 'var self=this;'; lf.
  387. str := stream.
  388. stream := '' writeStream.
  389. aNode nodes do: [:each |
  390. self visit: each].
  391. earlyReturn ifTrue: [
  392. str nextPutAll: 'try{'].
  393. str nextPutAll: stream contents.
  394. stream := str.
  395. stream
  396. lf;
  397. nextPutAll: 'return self;'.
  398. earlyReturn ifTrue: [
  399. stream lf; nextPutAll: '} catch(e) {if(e.name === ''stReturn'' && e.selector === ', currentSelector printString, '){return e.fn()} throw(e)}'].
  400. stream nextPutAll: '}'.
  401. Smalltalk current debugMode ifTrue: [
  402. stream
  403. nextPutAll: ',', String lf, 'messageSends: ';
  404. nextPutAll: messageSends asJavascript, ','; lf;
  405. nextPutAll: 'referencedClasses: ['.
  406. referencedClasses
  407. do: [:each | stream nextPutAll: each]
  408. separatedBy: [stream nextPutAll: ','].
  409. stream nextPutAll: ']'].
  410. stream nextPutAll: '})'
  411. !
  412. visitBlockNode: aNode
  413. stream nextPutAll: '(function('.
  414. aNode parameters
  415. do: [:each |
  416. tempVariables add: each.
  417. stream nextPutAll: each]
  418. separatedBy: [stream nextPutAll: ', '].
  419. stream nextPutAll: '){'.
  420. aNode nodes do: [:each | self visit: each].
  421. stream nextPutAll: '})'
  422. !
  423. visitSequenceNode: aNode
  424. aNode temps do: [:each |
  425. tempVariables add: each.
  426. stream nextPutAll: 'var ', each, '=nil;'; lf].
  427. aNode nodes do: [:each |
  428. self visit: each.
  429. stream nextPutAll: ';']
  430. separatedBy: [stream lf]
  431. !
  432. visitBlockSequenceNode: aNode
  433. | index |
  434. nestedBlocks := nestedBlocks + 1.
  435. aNode nodes isEmpty
  436. ifTrue: [
  437. stream nextPutAll: 'return nil;']
  438. ifFalse: [
  439. aNode temps do: [:each |
  440. tempVariables add: each.
  441. stream nextPutAll: 'var ', each, '=nil;'; lf].
  442. index := 0.
  443. aNode nodes do: [:each |
  444. index := index + 1.
  445. index = aNode nodes size ifTrue: [
  446. stream nextPutAll: 'return '].
  447. self visit: each.
  448. stream nextPutAll: ';']].
  449. nestedBlocks := nestedBlocks - 1
  450. !
  451. visitReturnNode: aNode
  452. nestedBlocks > 0 ifTrue: [
  453. earlyReturn := true].
  454. earlyReturn
  455. ifTrue: [
  456. stream
  457. nextPutAll: '(function(){throw(';
  458. nextPutAll: '{name: ''stReturn'', selector: ';
  459. nextPutAll: currentSelector printString;
  460. nextPutAll: ', fn: function(){return ']
  461. ifFalse: [stream nextPutAll: 'return '].
  462. aNode nodes do: [:each |
  463. self visit: each].
  464. earlyReturn ifTrue: [
  465. stream nextPutAll: '}})})()']
  466. !
  467. visitSendNode: aNode
  468. | str receiver superSend |
  469. str := stream.
  470. (messageSends includes: aNode selector) ifFalse: [
  471. messageSends add: aNode selector].
  472. stream := '' writeStream.
  473. self visit: aNode receiver.
  474. superSend := stream contents = 'super'.
  475. receiver := superSend ifTrue: ['self'] ifFalse: [stream contents].
  476. str nextPutAll: 'smalltalk.send('.
  477. str nextPutAll: receiver.
  478. stream := str.
  479. stream nextPutAll: ', "', aNode selector asSelector, '", ['.
  480. aNode arguments
  481. do: [:each | self visit: each]
  482. separatedBy: [stream nextPutAll: ', '].
  483. stream nextPutAll: ']'.
  484. superSend ifTrue: [
  485. stream nextPutAll: ', smalltalk.', (self classNameFor: self currentClass superclass)].
  486. stream nextPutAll: ')'
  487. !
  488. visitCascadeNode: aNode
  489. | index |
  490. index := 0.
  491. (tempVariables includes: '$rec') ifFalse: [
  492. tempVariables add: '$rec'].
  493. stream nextPutAll: '(function($rec){'.
  494. aNode nodes do: [:each |
  495. index := index + 1.
  496. index = aNode nodes size ifTrue: [
  497. stream nextPutAll: 'return '].
  498. each receiver: (VariableNode new value: '$rec').
  499. self visit: each.
  500. stream nextPutAll: ';'].
  501. stream nextPutAll: '})('.
  502. self visit: aNode receiver.
  503. stream nextPutAll: ')'
  504. !
  505. visitValueNode: aNode
  506. stream nextPutAll: aNode value asJavascript
  507. !
  508. visitAssignmentNode: aNode
  509. self visit: aNode left.
  510. stream nextPutAll: '='.
  511. self visit: aNode right
  512. !
  513. visitClassReferenceNode: aNode
  514. | klass |
  515. klass := 'smalltalk.', aNode value.
  516. (Smalltalk current at: aNode value) isClass ifTrue: [
  517. (referencedClasses includes: klass)
  518. ifFalse: [referencedClasses add: klass]].
  519. stream nextPutAll: klass
  520. !
  521. visitVariableNode: aNode
  522. (self currentClass allInstanceVariableNames includes: aNode value)
  523. ifTrue: [stream nextPutAll: 'self[''@', aNode value, ''']']
  524. ifFalse: [
  525. (self knownVariables includes: aNode value) ifFalse: [
  526. unknownVariables add: aNode value].
  527. stream nextPutAll: aNode value]
  528. !
  529. visitJSStatementNode: aNode
  530. stream nextPutAll: (aNode source replace: '>>' with: '>')
  531. !
  532. visitFailure: aFailure
  533. self error: aFailure asString
  534. ! !
  535. !Compiler class methodsFor: 'compiling'!
  536. recompile: aClass
  537. aClass methodDictionary do: [:each || method |
  538. method := self new load: each source forClass: aClass.
  539. method category: each category.
  540. aClass addCompiledMethod: method].
  541. aClass isMetaclass ifFalse: [self recompile: aClass class]
  542. !
  543. recompileAll
  544. Smalltalk current classes do: [:each |
  545. self recompile: each]
  546. ! !
  547. Object subclass: #DoIt
  548. instanceVariableNames: ''
  549. category: 'Compiler'!
  550. !DoIt methodsFor: ''!
  551. doIt ^[ESUG2011Presentation new] value
  552. ! !