Compiler.st 13 KB

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