Compiler.st 13 KB

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