assembler.scm 120 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194
  1. ;;; Guile bytecode assembler
  2. ;;; Copyright (C) 2001, 2009-2023 Free Software Foundation, Inc.
  3. ;;;
  4. ;;; This library is free software; you can redistribute it and/or
  5. ;;; modify it under the terms of the GNU Lesser General Public
  6. ;;; License as published by the Free Software Foundation; either
  7. ;;; version 3 of the License, or (at your option) any later version.
  8. ;;;
  9. ;;; This library is distributed in the hope that it will be useful,
  10. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;; Lesser General Public License for more details.
  13. ;;;
  14. ;;; You should have received a copy of the GNU Lesser General Public
  15. ;;; License along with this library; if not, write to the Free Software
  16. ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;; Commentary:
  18. ;;;
  19. ;;; This module implements an assembler that creates an ELF image from
  20. ;;; bytecode assembly and macro-assembly. The input can be given in
  21. ;;; s-expression form, like ((OP ARG ...) ...). Internally there is a
  22. ;;; procedural interface, the emit-OP procedures, but that is not
  23. ;;; currently exported.
  24. ;;;
  25. ;;; "Primitive instructions" correspond to VM operations. Assemblers
  26. ;;; for primitive instructions are generated programmatically from
  27. ;;; (instruction-list), which itself is derived from the VM sources.
  28. ;;; There are also "macro-instructions" like "label" or "load-constant"
  29. ;;; that expand to 0 or more primitive instructions.
  30. ;;;
  31. ;;; The assembler also handles some higher-level tasks, like creating
  32. ;;; the symbol table, other metadata sections, creating a constant table
  33. ;;; for the whole compilation unit, and writing the dynamic section of
  34. ;;; the ELF file along with the appropriate initialization routines.
  35. ;;;
  36. ;;; Most compilers will want to use the trio of make-assembler,
  37. ;;; emit-text, and link-assembly. That will result in the creation of
  38. ;;; an ELF image as a bytevector, which can then be loaded using
  39. ;;; load-thunk-from-memory, or written to disk as a .go file.
  40. ;;;
  41. ;;; Code:
  42. (define-module (system vm assembler)
  43. #:use-module (system base target)
  44. #:use-module (system base types internal)
  45. #:use-module (system vm dwarf)
  46. #:use-module (system vm elf)
  47. #:use-module (system vm linker)
  48. #:use-module (system syntax internal)
  49. #:use-module (language bytecode)
  50. #:use-module (rnrs bytevectors)
  51. #:use-module (ice-9 binary-ports)
  52. #:use-module (ice-9 vlist)
  53. #:use-module (ice-9 match)
  54. #:use-module (srfi srfi-1)
  55. #:use-module (srfi srfi-9)
  56. #:use-module (srfi srfi-11)
  57. #:export (make-assembler
  58. (emit-receive* . emit-receive)
  59. (emit-mov* . emit-mov)
  60. (emit-fmov* . emit-fmov)
  61. emit-u64=?
  62. emit-u64<?
  63. emit-u64-imm<?
  64. emit-imm-u64<?
  65. emit-s64-imm=?
  66. emit-s64<?
  67. emit-s64-imm<?
  68. emit-imm-s64<?
  69. emit-f64=?
  70. emit-f64<?
  71. emit-=?
  72. emit-<?
  73. emit-arguments<=?
  74. emit-positional-arguments<=?
  75. emit-immediate-tag=?
  76. emit-heap-tag=?
  77. emit-eq?
  78. emit-eq-immediate?
  79. emit-heap-numbers-equal?
  80. emit-j
  81. emit-jl
  82. emit-je
  83. emit-jnl
  84. emit-jne
  85. emit-jge
  86. emit-jnge
  87. emit-jtable
  88. emit-fixnum?
  89. emit-heap-object?
  90. emit-char?
  91. emit-undefined?
  92. emit-null?
  93. emit-false?
  94. emit-nil?
  95. emit-untag-fixnum
  96. emit-tag-fixnum
  97. emit-untag-char
  98. emit-tag-char
  99. emit-s64->f64
  100. emit-throw
  101. (emit-throw/value* . emit-throw/value)
  102. (emit-throw/value+data* . emit-throw/value+data)
  103. emit-unreachable
  104. emit-pair?
  105. emit-struct?
  106. emit-symbol?
  107. emit-variable?
  108. emit-vector?
  109. emit-mutable-vector?
  110. emit-immutable-vector?
  111. emit-weak-vector?
  112. emit-string?
  113. emit-heap-number?
  114. emit-hash-table?
  115. emit-pointer?
  116. emit-fluid?
  117. emit-stringbuf?
  118. emit-dynamic-state?
  119. emit-frame?
  120. emit-keyword?
  121. emit-syntax?
  122. emit-program?
  123. emit-vm-continuation?
  124. emit-bytevector?
  125. emit-weak-set?
  126. emit-weak-table?
  127. emit-array?
  128. emit-bitvector?
  129. emit-port?
  130. emit-smob?
  131. emit-bignum?
  132. emit-flonum?
  133. emit-compnum?
  134. emit-fracnum?
  135. emit-allocate-words
  136. emit-allocate-words/immediate
  137. emit-allocate-pointerless-words
  138. emit-allocate-pointerless-words/immediate
  139. emit-scm-ref
  140. emit-scm-set!
  141. emit-scm-ref/tag
  142. emit-scm-set!/tag
  143. emit-scm-ref/immediate
  144. emit-scm-set!/immediate
  145. emit-word-ref
  146. emit-word-set!
  147. emit-word-ref/immediate
  148. emit-word-set!/immediate
  149. emit-pointer-ref/immediate
  150. emit-pointer-set!/immediate
  151. emit-tail-pointer-ref/immediate
  152. emit-u8-ref
  153. emit-s8-ref
  154. emit-u16-ref
  155. emit-s16-ref
  156. emit-u32-ref
  157. emit-s32-ref
  158. emit-u64-ref
  159. emit-s64-ref
  160. emit-f32-ref
  161. emit-f64-ref
  162. emit-u8-set!
  163. emit-s8-set!
  164. emit-u16-set!
  165. emit-s16-set!
  166. emit-u32-set!
  167. emit-s32-set!
  168. emit-u64-set!
  169. emit-s64-set!
  170. emit-f32-set!
  171. emit-f64-set!
  172. emit-atomic-scm-ref/immediate
  173. emit-atomic-scm-set!/immediate
  174. emit-atomic-scm-swap!/immediate
  175. emit-atomic-scm-compare-and-swap!/immediate
  176. ;; Intrinsics.
  177. emit-add
  178. emit-add/immediate
  179. emit-sub
  180. emit-sub/immediate
  181. emit-mul
  182. emit-div
  183. emit-quo
  184. emit-rem
  185. emit-mod
  186. emit-inexact
  187. emit-abs
  188. emit-sqrt
  189. emit-floor
  190. emit-ceiling
  191. emit-sin
  192. emit-cos
  193. emit-tan
  194. emit-asin
  195. emit-acos
  196. emit-atan
  197. emit-atan2
  198. emit-fabs
  199. emit-fsqrt
  200. emit-ffloor
  201. emit-fceiling
  202. emit-fsin
  203. emit-fcos
  204. emit-ftan
  205. emit-fasin
  206. emit-facos
  207. emit-fatan
  208. emit-fatan2
  209. emit-logand
  210. emit-logior
  211. emit-logxor
  212. emit-logsub
  213. emit-string-set!
  214. emit-string->number
  215. emit-string->symbol
  216. emit-symbol->keyword
  217. emit-class-of
  218. emit-scm->f64
  219. emit-scm->u64
  220. emit-scm->u64/truncate
  221. emit-scm->s64
  222. emit-u64->scm
  223. emit-s64->scm
  224. emit-wind
  225. emit-unwind
  226. emit-push-fluid
  227. emit-pop-fluid
  228. emit-fluid-ref
  229. emit-fluid-set!
  230. emit-push-dynamic-state
  231. emit-pop-dynamic-state
  232. emit-lsh
  233. emit-rsh
  234. emit-lsh/immediate
  235. emit-rsh/immediate
  236. emit-resolve-module
  237. emit-module-variable
  238. emit-lookup
  239. emit-lookup-bound
  240. emit-lookup-bound-public
  241. emit-lookup-bound-private
  242. emit-define!
  243. emit-current-module
  244. ;; Intrinsics for use by the baseline compiler.
  245. emit-$car
  246. emit-$cdr
  247. emit-$set-car!
  248. emit-$set-cdr!
  249. emit-$variable-ref
  250. emit-$variable-set!
  251. emit-$vector-length
  252. emit-$vector-ref
  253. emit-$vector-set!
  254. emit-$vector-ref/immediate
  255. emit-$vector-set!/immediate
  256. emit-$allocate-struct
  257. emit-$struct-vtable
  258. emit-$struct-ref
  259. emit-$struct-set!
  260. emit-$struct-ref/immediate
  261. emit-$struct-set!/immediate
  262. emit-cache-ref
  263. emit-cache-set!
  264. emit-call
  265. emit-call-label
  266. emit-tail-call
  267. emit-tail-call-label
  268. (emit-instrument-entry* . emit-instrument-entry)
  269. (emit-instrument-loop* . emit-instrument-loop)
  270. emit-receive-values
  271. emit-return-values
  272. emit-shuffle-down
  273. emit-call/cc
  274. emit-abort
  275. emit-builtin-ref
  276. emit-assert-nargs-ee
  277. emit-assert-nargs-ge
  278. emit-assert-nargs-le
  279. emit-reset-frame
  280. emit-assert-nargs-ee/locals
  281. emit-bind-kwargs
  282. emit-bind-rest
  283. emit-load-label
  284. emit-resolve
  285. emit-prompt
  286. emit-current-thread
  287. emit-fadd
  288. emit-fsub
  289. emit-fmul
  290. emit-fdiv
  291. emit-uadd
  292. emit-usub
  293. emit-umul
  294. emit-uadd/immediate
  295. emit-usub/immediate
  296. emit-umul/immediate
  297. emit-ulogand
  298. emit-ulogior
  299. emit-ulogxor
  300. emit-ulogsub
  301. emit-ursh
  302. emit-srsh
  303. emit-ulsh
  304. emit-ursh/immediate
  305. emit-srsh/immediate
  306. emit-ulsh/immediate
  307. emit-make-array
  308. emit-load-f64
  309. emit-load-u64
  310. emit-load-s64
  311. emit-handle-interrupts
  312. emit-text
  313. link-assembly))
  314. ;; Like define-inlinable, but only for first-order uses of the defined
  315. ;; routine. Should residualize less code.
  316. (eval-when (expand)
  317. (define-syntax define-inline
  318. (lambda (x)
  319. (syntax-case x ()
  320. ((_ (name arg ...) body ...)
  321. (with-syntax (((temp ...) (generate-temporaries #'(arg ...))))
  322. #`(eval-when (expand)
  323. (define-syntax-rule (name temp ...)
  324. (let ((arg temp) ...)
  325. body ...)))))))))
  326. ;;; Bytecode consists of 32-bit units, often subdivided in some way.
  327. ;;; These helpers create one 32-bit unit from multiple components.
  328. (define-inline (check-urange x mask)
  329. (unless (and (exact-integer? x) (= x (logand x mask)))
  330. (error "out of range" x))
  331. x)
  332. (define-inline (check-srange x mask)
  333. (let ((x* (logand x mask)))
  334. (unless (if (negative? x)
  335. (= (+ x mask 1) x*)
  336. (= x x*))
  337. (error "out of range" x))
  338. x*))
  339. (define-inline (pack-u8-u24 x y)
  340. (let ((x (check-urange x #xff))
  341. (y (check-urange y #xffffff)))
  342. (logior x (ash y 8))))
  343. (define-inline (pack-u8-s24 x y)
  344. (let ((x (check-urange x #xff))
  345. (y (check-srange y #xffffff)))
  346. (logior x (ash y 8))))
  347. (define-inline (pack-u16-u16 x y)
  348. (let ((x (check-urange x #xffff))
  349. (y (check-urange y #xffff)))
  350. (logior x (ash y 16))))
  351. (define-inline (pack-u1-u7-u24 x y z)
  352. (let ((x (check-urange x #x1))
  353. (y (check-urange y #x7f))
  354. (z (check-urange z #xffffff)))
  355. (logior x (ash y 1) (ash z 8))))
  356. (define-inline (pack-u8-u12-u12 x y z)
  357. (let ((x (check-urange x #xff))
  358. (y (check-urange y #xfff))
  359. (z (check-urange z #xfff)))
  360. (logior x (ash y 8) (ash z 20))))
  361. (define-inline (pack-u8-u12-s12 x y z)
  362. (let ((x (check-urange x #xff))
  363. (y (check-urange y #xfff))
  364. (z (check-srange z #xfff)))
  365. (logior x (ash y 8) (ash z 20))))
  366. (define-inline (pack-u8-u8-u16 x y z)
  367. (let ((x (check-urange x #xff))
  368. (y (check-urange y #xff))
  369. (z (check-urange z #xffff)))
  370. (logior x (ash y 8) (ash z 16))))
  371. (define-inline (pack-u8-u8-u8-u8 x y z w)
  372. (let ((x (check-urange x #xff))
  373. (y (check-urange y #xff))
  374. (z (check-urange z #xff))
  375. (w (check-urange w #xff)))
  376. (logior x (ash y 8) (ash z 16) (ash w 24))))
  377. (eval-when (expand)
  378. (define-syntax pack-flags
  379. (syntax-rules ()
  380. ;; Add clauses as needed.
  381. ((pack-flags f1 f2) (logior (if f1 (ash 1 0) 0)
  382. (if f2 (ash 1 1) 0))))))
  383. (define-syntax-rule (define-byte-order-swapper name size ref set)
  384. (define* (name buf #:optional (start 0) (end (bytevector-length buf)))
  385. "Patch up the text buffer @var{buf}, swapping the endianness of each
  386. N-byte unit."
  387. (unless (zero? (modulo (- end start) size))
  388. (error "unexpected length"))
  389. (let lp ((pos start))
  390. (when (< pos end)
  391. (set buf pos (ref buf pos (endianness big)) (endianness little))
  392. (lp (+ pos size))))))
  393. (define-byte-order-swapper byte-swap/2!
  394. 2 bytevector-u16-ref bytevector-u16-set!)
  395. (define-byte-order-swapper byte-swap/4!
  396. 4 bytevector-u32-ref bytevector-u32-set!)
  397. (define-byte-order-swapper byte-swap/8!
  398. 8 bytevector-u64-ref bytevector-u64-set!)
  399. ;;; A <meta> entry collects metadata for one procedure. Procedures are
  400. ;;; written as contiguous ranges of bytecode.
  401. ;;;
  402. (eval-when (expand)
  403. (define-syntax-rule (assert-match arg pattern kind)
  404. (let ((x arg))
  405. (unless (match x (pattern #t) (_ #f))
  406. (error (string-append "expected " kind) x)))))
  407. (define-record-type <jit-data>
  408. (make-jit-data low-pc high-pc)
  409. jit-data?
  410. (low-pc jit-data-low-pc)
  411. (high-pc jit-data-high-pc))
  412. (define-record-type <meta>
  413. (%make-meta label properties low-pc high-pc arities jit-data-label)
  414. meta?
  415. (label meta-label)
  416. (properties meta-properties set-meta-properties!)
  417. (low-pc meta-low-pc)
  418. (high-pc meta-high-pc set-meta-high-pc!)
  419. (arities meta-arities set-meta-arities!)
  420. (jit-data-label meta-jit-data-label))
  421. (define (make-meta label properties low-pc)
  422. (assert-match label (or (? exact-integer?) (? symbol?)) "symbol")
  423. (assert-match properties (((? symbol?) . _) ...) "alist with symbolic keys")
  424. (%make-meta label properties low-pc #f '() (gensym "jit-data")))
  425. (define (meta-name meta)
  426. (assq-ref (meta-properties meta) 'name))
  427. ;; Metadata for one <lambda-case>.
  428. (define-record-type <arity>
  429. (make-arity req opt rest kw-indices allow-other-keys? has-closure?
  430. low-pc high-pc definitions)
  431. arity?
  432. (req arity-req)
  433. (opt arity-opt)
  434. (rest arity-rest)
  435. (kw-indices arity-kw-indices)
  436. (allow-other-keys? arity-allow-other-keys?)
  437. (has-closure? arity-has-closure?)
  438. (low-pc arity-low-pc)
  439. (high-pc arity-high-pc set-arity-high-pc!)
  440. (definitions arity-definitions set-arity-definitions!))
  441. ;;; An assembler collects all of the words emitted during assembly, and
  442. ;;; also maintains ancillary information such as the constant table, a
  443. ;;; relocation list, and so on.
  444. ;;;
  445. ;;; Bytecode consists of 32-bit units. We emit bytecode using native
  446. ;;; endianness. If we're targeting a foreign endianness, we byte-swap
  447. ;;; the bytevector as a whole instead of conditionalizing each access.
  448. ;;;
  449. (define-record-type <asm>
  450. (make-asm buf pos start
  451. labels relocs
  452. word-size endianness
  453. constants inits
  454. shstrtab next-section-number
  455. meta sources
  456. slot-maps)
  457. asm?
  458. ;; We write bytecode into a bytevector, growing the bytevector as
  459. ;; needed. asm-cur is that bytevector, and asm-pos is the byte offset
  460. ;; into the vector at which the next word should be written.
  461. ;;
  462. (buf asm-buf set-asm-buf!)
  463. (pos asm-pos set-asm-pos!)
  464. ;; asm-start is an absolute position, indicating the byte offset of
  465. ;; the beginning of an instruction. It is updated after writing all
  466. ;; the words for one primitive instruction. It models the position of
  467. ;; the instruction pointer during execution, given that the VM updates
  468. ;; the IP only at the end of executing the instruction, and is thus
  469. ;; useful for computing offsets between two points in a program.
  470. ;;
  471. (start asm-start set-asm-start!)
  472. ;; An alist of symbol -> position pairs, indicating the labels defined
  473. ;; in this compilation unit.
  474. ;;
  475. (labels asm-labels set-asm-labels!)
  476. ;; A list of relocations needed by the program text. We use an
  477. ;; internal representation for relocations, and handle textual
  478. ;; relative relocations in the assembler. Other kinds of relocations
  479. ;; are later reified as linker relocations and resolved by the linker.
  480. ;;
  481. (relocs asm-relocs set-asm-relocs!)
  482. ;; Target information.
  483. ;;
  484. (word-size asm-word-size)
  485. (endianness asm-endianness)
  486. ;; The constant table, as a vhash of object -> label. All constants
  487. ;; get de-duplicated and written into separate sections -- either the
  488. ;; .rodata section, for read-only data, or .data, for constants that
  489. ;; need initialization at load-time (like symbols). Constants can
  490. ;; depend on other constants (e.g. a symbol depending on a stringbuf),
  491. ;; so order in this table is important.
  492. ;;
  493. (constants asm-constants set-asm-constants!)
  494. ;; A vhash of label to init descriptors, where an init descriptor is
  495. ;; #(EMIT-INIT STATIC? PATCHES). EMIT-INIT, if present, is a
  496. ;; procedure taking the asm and the label as arguments. Unless the
  497. ;; object is statically allocatable, in which case it can be loaded
  498. ;; via make-non-immediate rather than static-ref, EMIT-INIT should
  499. ;; also initialize the corresponding cell for any later static-ref.
  500. ;; If STATIC? is true, the value can be loaded with
  501. ;; emit-make-non-immediate, otherwise it's emit-static-ref. A bit
  502. ;; confusing but that's how it is. PATCHES is a list of (DEST-LABEL
  503. ;; . FIELD) pairs, indicating locations to which to patch the value.
  504. ;; Like asm-constants, order is important.
  505. ;;
  506. (inits asm-inits set-asm-inits!)
  507. ;; The shstrtab, for section names.
  508. ;;
  509. (shstrtab asm-shstrtab set-asm-shstrtab!)
  510. ;; The section number for the next section to be written.
  511. ;;
  512. (next-section-number asm-next-section-number set-asm-next-section-number!)
  513. ;; A list of <meta>, corresponding to procedure metadata.
  514. ;;
  515. (meta asm-meta set-asm-meta!)
  516. ;; A list of (pos . source) pairs, indicating source information. POS
  517. ;; is relative to the beginning of the text section, and SOURCE is in
  518. ;; the same format that source-properties returns.
  519. ;;
  520. (sources asm-sources set-asm-sources!)
  521. ;; A list of (pos . slot-map) pairs, indicating slot maps. POS is
  522. ;; relative to the beginning of the text section. SLOT-MAP is a
  523. ;; bitfield describing the stack at call sites, as an integer.
  524. ;;
  525. (slot-maps asm-slot-maps set-asm-slot-maps!))
  526. (define* (make-assembler #:key (word-size (target-word-size))
  527. (endianness (target-endianness)))
  528. "Create an assembler for a given target @var{word-size} and
  529. @var{endianness}, falling back to appropriate values for the configured
  530. target."
  531. (make-asm (make-u32vector 1000) 0 0
  532. (make-hash-table) '()
  533. word-size endianness
  534. vlist-null vlist-null
  535. (make-string-table) 1
  536. '() '() '()))
  537. (define (intern-section-name! asm string)
  538. "Add a string to the section name table (shstrtab)."
  539. (string-table-intern! (asm-shstrtab asm) string))
  540. (define (grow-buffer! asm)
  541. "Grow the code buffer of the asm."
  542. (let* ((buf (asm-buf asm))
  543. (len (bytevector-length buf))
  544. (new (make-u32vector (ash len -1) 0)))
  545. (bytevector-copy! buf 0 new 0 len)
  546. (set-asm-buf! asm new)
  547. #f))
  548. (define-inline (emit asm u32)
  549. "Emit one 32-bit word into the instruction stream. Assumes that there
  550. is space for the word."
  551. (bytevector-u32-native-set! (asm-buf asm) (asm-pos asm) u32)
  552. (set-asm-pos! asm (+ (asm-pos asm) 4)))
  553. (define-inline (make-reloc type label base word)
  554. "Make an internal relocation of type @var{type} referencing symbol
  555. @var{label}, @var{word} words after position @var{start}. @var{type}
  556. may be x8-s24, indicating a 24-bit relative label reference that can be
  557. fixed up by the assembler, or s32, indicating a 32-bit relative
  558. reference that needs to be fixed up by the linker."
  559. (list type label base word))
  560. (define-inline (reset-asm-start! asm)
  561. "Reset the asm-start after writing the words for one instruction."
  562. (set-asm-start! asm (asm-pos asm)))
  563. (define (record-label-reference asm label)
  564. "Record an x8-s24 local label reference. This value will get patched
  565. up later by the assembler."
  566. (let* ((start (asm-start asm))
  567. (pos (asm-pos asm))
  568. (reloc (make-reloc 'x8-s24 label start (- pos start))))
  569. (set-asm-relocs! asm (cons reloc (asm-relocs asm)))))
  570. (define* (record-far-label-reference asm label #:optional (offset 0))
  571. "Record an s32 far label reference. This value will get patched up
  572. later by the linker."
  573. (let* ((start (- (asm-start asm) offset))
  574. (pos (asm-pos asm))
  575. (reloc (make-reloc 's32 label start (- pos start))))
  576. (set-asm-relocs! asm (cons reloc (asm-relocs asm)))))
  577. (define (immediate-bits asm x)
  578. (let ((bits (scm->immediate-bits x)))
  579. (and bits (truncate-bits bits (* 8 (asm-word-size asm)) #t))))
  580. ;;;
  581. ;;; Primitive assemblers are defined by expanding `assembler' for each
  582. ;;; opcode in `(instruction-list)'.
  583. ;;;
  584. (eval-when (expand)
  585. (define (id-append ctx a b)
  586. (datum->syntax ctx (symbol-append (syntax->datum a) (syntax->datum b))))
  587. (define-syntax encoder
  588. (lambda (x)
  589. (define-syntax op-case
  590. (lambda (x)
  591. (syntax-case x ()
  592. ((_ asm name ((type arg ...) code ...) clause ...)
  593. #`(if (eq? name 'type)
  594. (with-syntax (((arg ...) (generate-temporaries #'(arg ...))))
  595. #'((arg ...)
  596. code ...))
  597. (op-case asm name clause ...)))
  598. ((_ asm name)
  599. #'(error "unmatched name" name)))))
  600. (define (pack-first-word asm opcode type)
  601. (with-syntax ((opcode opcode))
  602. (op-case
  603. asm type
  604. ((X32)
  605. (emit asm opcode))
  606. ((X8_S24 arg)
  607. (emit asm (pack-u8-u24 opcode arg)))
  608. ((X8_F24 arg)
  609. (emit asm (pack-u8-u24 opcode arg)))
  610. ((X8_C24 arg)
  611. (emit asm (pack-u8-u24 opcode arg)))
  612. ((X8_L24 label)
  613. (record-label-reference asm label)
  614. (emit asm opcode))
  615. ((X8_S8_I16 a imm)
  616. (let ((bits (truncate-bits (scm->immediate-bits imm) 16 #f)))
  617. (emit asm (pack-u8-u8-u16 opcode a bits))))
  618. ((X8_S8_ZI16 a imm)
  619. (let ((bits (truncate-bits (scm->immediate-bits imm) 16 #t)))
  620. (emit asm (pack-u8-u8-u16 opcode a bits))))
  621. ((X8_S12_S12 a b)
  622. (emit asm (pack-u8-u12-u12 opcode a b)))
  623. ((X8_S12_C12 a b)
  624. (emit asm (pack-u8-u12-u12 opcode a b)))
  625. ((X8_S12_Z12 a b)
  626. (emit asm (pack-u8-u12-s12 opcode a b)))
  627. ((X8_C12_C12 a b)
  628. (emit asm (pack-u8-u12-u12 opcode a b)))
  629. ((X8_F12_F12 a b)
  630. (emit asm (pack-u8-u12-u12 opcode a b)))
  631. ((X8_S8_S8_S8 a b c)
  632. (emit asm (pack-u8-u8-u8-u8 opcode a b c)))
  633. ((X8_S8_S8_C8 a b c)
  634. (emit asm (pack-u8-u8-u8-u8 opcode a b c)))
  635. ((X8_S8_C8_S8 a b c)
  636. (emit asm (pack-u8-u8-u8-u8 opcode a b c))))))
  637. (define (pack-tail-word asm type)
  638. (op-case
  639. asm type
  640. ((C32 a)
  641. (emit asm a))
  642. ((I32 imm)
  643. (let ((val (immediate-bits asm imm)))
  644. (emit asm val)))
  645. ((A32 imm)
  646. (unless (= (asm-word-size asm) 8)
  647. (error "make-long-immediate unavailable for this target"))
  648. (let ((bits (immediate-bits asm imm)))
  649. (emit asm (ash bits -32))
  650. (emit asm (logand bits (1- (ash 1 32))))))
  651. ((AF32 f64)
  652. (let ((u64 (u64vector-ref (f64vector f64) 0)))
  653. (emit asm (ash u64 -32))
  654. (emit asm (logand u64 (1- (ash 1 32))))))
  655. ((AU32 u64)
  656. (emit asm (ash u64 -32))
  657. (emit asm (logand u64 (1- (ash 1 32)))))
  658. ((AS32 s64)
  659. (let ((u64 (u64vector-ref (s64vector s64) 0)))
  660. (emit asm (ash u64 -32))
  661. (emit asm (logand u64 (1- (ash 1 32))))))
  662. ((B32))
  663. ((BU32))
  664. ((BS32))
  665. ((BF32))
  666. ((N32 label)
  667. (record-far-label-reference asm label)
  668. (emit asm 0))
  669. ((R32 label)
  670. (record-far-label-reference asm label)
  671. (emit asm 0))
  672. ((L32 label)
  673. (record-far-label-reference asm label)
  674. (emit asm 0))
  675. ((LO32 label offset)
  676. (record-far-label-reference asm label
  677. (* offset (asm-word-size asm)))
  678. (emit asm 0))
  679. ((C8_C24 a b)
  680. (emit asm (pack-u8-u24 a b)))
  681. ((C8_S24 a b)
  682. (emit asm (pack-u8-u24 a b)))
  683. ((C16_C16 a b)
  684. (emit asm (pack-u16-u16 a b)))
  685. ((V32_X8_L24 labels)
  686. (let ((len (vector-length labels)))
  687. (emit asm len)
  688. (let lp ()
  689. (unless (<= (+ (asm-pos asm) (* 4 len))
  690. (bytevector-length (asm-buf asm)))
  691. (grow-buffer! asm)
  692. (lp)))
  693. (let lp ((n 0))
  694. (when (< n len)
  695. (record-label-reference asm (vector-ref labels n))
  696. (emit asm 0)
  697. (lp (1+ n))))))
  698. ((B1_X7_L24 a label)
  699. (record-label-reference asm label)
  700. (emit asm (pack-u1-u7-u24 (if a 1 0) 0 0)))
  701. ((B1_C7_L24 a b label)
  702. (record-label-reference asm label)
  703. (emit asm (pack-u1-u7-u24 (if a 1 0) b 0)))
  704. ((B1_X31 a)
  705. (emit asm (pack-u1-u7-u24 (if a 1 0) 0 0)))
  706. ((B1_X7_S24 a b)
  707. (emit asm (pack-u1-u7-u24 (if a 1 0) 0 b)))
  708. ((B1_X7_F24 a b)
  709. (emit asm (pack-u1-u7-u24 (if a 1 0) 0 b)))
  710. ((B1_X7_C24 a b)
  711. (emit asm (pack-u1-u7-u24 (if a 1 0) 0 b)))
  712. ((X8_S24 a)
  713. (emit asm (pack-u8-u24 0 a)))
  714. ((X8_F24 a)
  715. (emit asm (pack-u8-u24 0 a)))
  716. ((X8_C24 a)
  717. (emit asm (pack-u8-u24 0 a)))
  718. ((X8_L24 label)
  719. (record-label-reference asm label)
  720. (emit asm 0))))
  721. (syntax-case x ()
  722. ((_ word0 word* ...)
  723. (with-syntax ((((formal0 ...)
  724. code0 ...)
  725. (pack-first-word #'asm #'opcode
  726. (syntax->datum #'word0)))
  727. ((((formal* ...)
  728. code* ...) ...)
  729. (map (lambda (word) (pack-tail-word #'asm word))
  730. (syntax->datum #'(word* ...)))))
  731. ;; The opcode is the last argument, so that assemblers don't
  732. ;; have to shuffle their arguments before tail-calling an
  733. ;; encoder.
  734. #'(lambda (asm formal0 ... formal* ... ... opcode)
  735. (let lp ()
  736. (let ((words (length '(word0 word* ...))))
  737. (unless (<= (+ (asm-pos asm) (* 4 words))
  738. (bytevector-length (asm-buf asm)))
  739. (grow-buffer! asm)
  740. (lp))))
  741. code0 ...
  742. code* ... ...
  743. (reset-asm-start! asm)))))))
  744. (define (encoder-name operands)
  745. (let lp ((operands operands) (out #'encode))
  746. (syntax-case operands ()
  747. (() out)
  748. ((operand . operands)
  749. (lp #'operands
  750. (id-append #'operand (id-append out out #'-) #'operand))))))
  751. (define-syntax define-encoder
  752. (lambda (x)
  753. (syntax-case x ()
  754. ((_ operand ...)
  755. (with-syntax ((encode (encoder-name #'(operand ...))))
  756. #'(define encode (encoder operand ...)))))))
  757. (define-syntax visit-instruction-kinds
  758. (lambda (x)
  759. (syntax-case x ()
  760. ((visit-instruction-kinds macro arg ...)
  761. (with-syntax (((operands ...)
  762. (delete-duplicates
  763. (map (match-lambda
  764. ((name opcode kind . operands)
  765. (datum->syntax #'macro operands)))
  766. (instruction-list)))))
  767. #'(begin
  768. (macro arg ... . operands)
  769. ...)))))))
  770. (visit-instruction-kinds define-encoder)
  771. ;; In Guile's VM, locals are usually addressed via the stack pointer
  772. ;; (SP). There can be up to 2^24 slots for local variables in a
  773. ;; frame. Some instructions encode their operands using a restricted
  774. ;; subset of the full 24-bit local address space, in order to make the
  775. ;; bytecode more dense in the usual case that a function needs few
  776. ;; local slots. To allow these instructions to be used when there are
  777. ;; many local slots, we can temporarily push the values on the stack,
  778. ;; operate on them there, and then store back any result as we pop the
  779. ;; SP to its original position.
  780. ;;
  781. ;; We implement this shuffling via wrapper encoders that have the same
  782. ;; arity as the encoder they wrap, e.g. encode-X8_S12_S12/shuffle that
  783. ;; wraps encode-X8_S12_S12. We make the emit-cons public interface
  784. ;; use the shuffling encoder. That way we solve the problem fully and
  785. ;; in just one place.
  786. (define (encode-X8_S12_S12!/shuffle asm a b opcode)
  787. (cond
  788. ((< (logior a b) (ash 1 12))
  789. (encode-X8_S12_S12 asm a b opcode))
  790. (else
  791. (emit-push asm a)
  792. (emit-push asm (1+ b))
  793. (encode-X8_S12_S12 asm 1 0 opcode)
  794. (emit-drop asm 2))))
  795. (define (encode-X8_S12_S12<-/shuffle asm dst a opcode)
  796. (cond
  797. ((< (logior dst a) (ash 1 12))
  798. (encode-X8_S12_S12 asm dst a opcode))
  799. (else
  800. (emit-push asm a)
  801. (encode-X8_S12_S12 asm 0 0 opcode)
  802. (emit-pop asm dst))))
  803. (define (encode-X8_S12_C12!/shuffle asm a const opcode)
  804. (cond
  805. ((< a (ash 1 12))
  806. (encode-X8_S12_C12 asm a const opcode))
  807. (else
  808. (emit-push asm a)
  809. (encode-X8_S12_C12 asm 0 const opcode)
  810. (emit-drop asm 1))))
  811. (define (encode-X8_S12_C12<-/shuffle asm dst const opcode)
  812. (cond
  813. ((< dst (ash 1 12))
  814. (encode-X8_S12_C12 asm dst const opcode))
  815. (else
  816. ;; Push garbage value to make space for dst.
  817. (emit-push asm dst)
  818. (encode-X8_S12_C12 asm 0 const opcode)
  819. (emit-pop asm dst))))
  820. (define (encode-X8_S12_Z12!/shuffle asm a const opcode)
  821. (cond
  822. ((< a (ash 1 12))
  823. (encode-X8_S12_Z12 asm a const opcode))
  824. (else
  825. (emit-push asm a)
  826. (encode-X8_S12_Z12 asm 0 const opcode)
  827. (emit-drop asm 1))))
  828. (define (encode-X8_S8_I16<-/shuffle asm dst imm opcode)
  829. (cond
  830. ((< dst (ash 1 8))
  831. (encode-X8_S8_I16 asm dst imm opcode))
  832. (else
  833. ;; Push garbage value to make space for dst.
  834. (emit-push asm dst)
  835. (encode-X8_S8_I16 asm 0 imm opcode)
  836. (emit-pop asm dst))))
  837. (define (encode-X8_S8_ZI16!/shuffle asm a imm opcode)
  838. (cond
  839. ((< a (ash 1 8))
  840. (encode-X8_S8_ZI16 asm a imm opcode))
  841. (else
  842. (emit-push asm a)
  843. (encode-X8_S8_ZI16 asm 0 imm opcode)
  844. (emit-drop asm 1))))
  845. (define (encode-X8_S8_ZI16<-/shuffle asm dst imm opcode)
  846. (cond
  847. ((< dst (ash 1 8))
  848. (encode-X8_S8_ZI16 asm dst imm opcode))
  849. (else
  850. ;; Push garbage value to make space for dst.
  851. (emit-push asm dst)
  852. (encode-X8_S8_ZI16 asm 0 imm opcode)
  853. (emit-pop asm dst))))
  854. (define (encode-X8_S8_S8_S8!/shuffle asm a b c opcode)
  855. (cond
  856. ((< (logior a b c) (ash 1 8))
  857. (encode-X8_S8_S8_S8 asm a b c opcode))
  858. (else
  859. (emit-push asm a)
  860. (emit-push asm (+ b 1))
  861. (emit-push asm (+ c 2))
  862. (encode-X8_S8_S8_S8 asm 2 1 0 opcode)
  863. (emit-drop asm 3))))
  864. (define (encode-X8_S8_S8_S8<-/shuffle asm dst a b opcode)
  865. (cond
  866. ((< (logior dst a b) (ash 1 8))
  867. (encode-X8_S8_S8_S8 asm dst a b opcode))
  868. (else
  869. (emit-push asm a)
  870. (emit-push asm (1+ b))
  871. (encode-X8_S8_S8_S8 asm 1 1 0 opcode)
  872. (emit-drop asm 1)
  873. (emit-pop asm dst))))
  874. (define (encode-X8_S8_S8_C8<-/shuffle asm dst a const opcode)
  875. (cond
  876. ((< (logior dst a) (ash 1 8))
  877. (encode-X8_S8_S8_C8 asm dst a const opcode))
  878. (else
  879. (emit-push asm a)
  880. (encode-X8_S8_S8_C8 asm 0 0 const opcode)
  881. (emit-pop asm dst))))
  882. (define (encode-X8_S8_C8_S8!/shuffle asm a const b opcode)
  883. (cond
  884. ((< (logior a b) (ash 1 8))
  885. (encode-X8_S8_C8_S8 asm a const b opcode))
  886. (else
  887. (emit-push asm a)
  888. (emit-push asm (1+ b))
  889. (encode-X8_S8_C8_S8 asm 1 const 0 opcode)
  890. (emit-drop asm 2))))
  891. (define (encode-X8_S8_C8_S8<-/shuffle asm dst const a opcode)
  892. (cond
  893. ((< (logior dst a) (ash 1 8))
  894. (encode-X8_S8_C8_S8 asm dst const a opcode))
  895. (else
  896. (emit-push asm a)
  897. (encode-X8_S8_C8_S8 asm 0 const 0 opcode)
  898. (emit-pop asm dst))))
  899. (define (encode-X8_S8_S8_S8-C32!/shuffle asm a b c const opcode)
  900. (cond
  901. ((< (logior a b c) (ash 1 8))
  902. (encode-X8_S8_S8_S8-C32 asm a b c const opcode))
  903. (else
  904. (emit-push asm a)
  905. (emit-push asm (+ b 1))
  906. (emit-push asm (+ c 2))
  907. (encode-X8_S8_S8_S8-C32 asm 2 1 0 const opcode)
  908. (emit-drop asm 3))))
  909. (define (encode-X8_S8_S8_S8-C32<-/shuffle asm dst a b c32 opcode)
  910. (cond
  911. ((< (logior dst a b) (ash 1 8))
  912. (encode-X8_S8_S8_S8-C32 asm dst a b c32 opcode))
  913. (else
  914. (emit-push asm a)
  915. (emit-push asm (1+ b))
  916. (encode-X8_S8_S8_S8-C32 asm 1 1 0 c32 opcode)
  917. (emit-drop asm 1)
  918. (emit-pop asm dst))))
  919. (define (encode-X8_S8_S8_C8-C32<-/shuffle asm dst a const c32 opcode)
  920. (cond
  921. ((< (logior dst a) (ash 1 8))
  922. (encode-X8_S8_S8_C8-C32 asm dst a const c32 opcode))
  923. (else
  924. (emit-push asm a)
  925. (encode-X8_S8_S8_C8-C32 asm 0 0 const c32 opcode)
  926. (emit-pop asm dst))))
  927. (define (encode-X8_S8_S8_S8-C32!/shuffle asm a b c c32 opcode)
  928. (cond
  929. ((< (logior a b c) (ash 1 8))
  930. (encode-X8_S8_S8_S8-C32 asm a b c c32 opcode))
  931. (else
  932. (emit-push asm a)
  933. (emit-push asm (+ b 1))
  934. (emit-push asm (+ c 2))
  935. (encode-X8_S8_S8_S8-C32 asm 2 1 0 c32 opcode)
  936. (emit-drop asm 3))))
  937. (define (encode-X8_S8_C8_S8-C32!/shuffle asm a const b c32 opcode)
  938. (cond
  939. ((< (logior a b) (ash 1 8))
  940. (encode-X8_S8_C8_S8-C32 asm a const b c32 opcode))
  941. (else
  942. (emit-push asm a)
  943. (emit-push asm (+ b 1))
  944. (encode-X8_S8_C8_S8-C32 asm 1 const 0 c32 opcode)
  945. (emit-drop asm 2))))
  946. (define (encode-X8_S12_S12-C32<-/shuffle asm dst src c32 opcode)
  947. (cond
  948. ((< (logior dst src) (ash 1 12))
  949. (encode-X8_S12_S12-C32 asm dst src c32 opcode))
  950. (else
  951. (emit-push asm src)
  952. (encode-X8_S12_S12-C32 asm 0 0 c32 opcode)
  953. (emit-pop asm dst))))
  954. (define (encode-X8_S12_S12-C32!/shuffle asm a b c32 opcode)
  955. (cond
  956. ((< (logior a b) (ash 1 12))
  957. (encode-X8_S12_S12-C32 asm a b c32 opcode))
  958. (else
  959. (emit-push asm a)
  960. (emit-push asm b)
  961. (encode-X8_S12_S12-C32 asm 1 0 c32 opcode)
  962. (emit-drop asm 2))))
  963. (eval-when (expand)
  964. (define (id-append ctx a b)
  965. (datum->syntax ctx (symbol-append (syntax->datum a) (syntax->datum b))))
  966. (define (shuffling-encoder-name kind operands)
  967. (match (cons (syntax->datum kind) (syntax->datum operands))
  968. (('! 'X8_S12_S12) #'encode-X8_S12_S12!/shuffle)
  969. (('<- 'X8_S12_S12) #'encode-X8_S12_S12<-/shuffle)
  970. (('! 'X8_S12_S12 'X8_C24) #'encode-X8_S12_S12-X8_C24!/shuffle)
  971. (('<- 'X8_S12_S12 'X8_C24) #'encode-X8_S12_S12-X8_C24<-/shuffle)
  972. (('! 'X8_S12_C12) #'encode-X8_S12_C12!/shuffle)
  973. (('<- 'X8_S12_C12) #'encode-X8_S12_C12<-/shuffle)
  974. (('! 'X8_S12_Z12) #'encode-X8_S12_Z12!/shuffle)
  975. (('<- 'X8_S8_I16) #'encode-X8_S8_I16<-/shuffle)
  976. (('! 'X8_S8_ZI16) #'encode-X8_S8_ZI16!/shuffle)
  977. (('<- 'X8_S8_ZI16) #'encode-X8_S8_ZI16<-/shuffle)
  978. (('! 'X8_S8_S8_S8) #'encode-X8_S8_S8_S8!/shuffle)
  979. (('<- 'X8_S8_S8_S8) #'encode-X8_S8_S8_S8<-/shuffle)
  980. (('<- 'X8_S8_S8_C8) #'encode-X8_S8_S8_C8<-/shuffle)
  981. (('! 'X8_S8_S8_S8 'C32) #'encode-X8_S8_S8_S8-C32!/shuffle)
  982. (('! 'X8_S8_C8_S8 'C32) #'encode-X8_S8_C8_S8-C32!/shuffle)
  983. (('<- 'X8_S8_S8_S8 'C32) #'encode-X8_S8_S8_S8-C32<-/shuffle)
  984. (('<- 'X8_S8_S8_C8 'C32) #'encode-X8_S8_S8_C8-C32<-/shuffle)
  985. (('<- 'X8_S12_S12 'C32) #'encode-X8_S12_S12-C32<-/shuffle)
  986. (('! 'X8_S12_S12 'C32) #'encode-X8_S12_S12-C32!/shuffle)
  987. (('! 'X8_S8_C8_S8) #'encode-X8_S8_C8_S8!/shuffle)
  988. (('<- 'X8_S8_C8_S8) #'encode-X8_S8_C8_S8<-/shuffle)
  989. (else (encoder-name operands))))
  990. (define-syntax assembler
  991. (lambda (x)
  992. (define (word-args word)
  993. (match word
  994. ('C32 #'(a))
  995. ('I32 #'(imm))
  996. ('A32 #'(imm))
  997. ('AF32 #'(f64))
  998. ('AU32 #'(u64))
  999. ('AS32 #'(s64))
  1000. ('B32 #'())
  1001. ('BU32 #'())
  1002. ('BS32 #'())
  1003. ('BF32 #'())
  1004. ('N32 #'(label))
  1005. ('R32 #'(label))
  1006. ('L32 #'(label))
  1007. ('LO32 #'(label offset))
  1008. ('C8_C24 #'(a b))
  1009. ('C8_S24 #'(a b))
  1010. ('C16_C16 #'(a b))
  1011. ('V32_X8_L24 #'(labels))
  1012. ('B1_X7_L24 #'(a label))
  1013. ('B1_C7_L24 #'(a b label))
  1014. ('B1_X31 #'(a))
  1015. ('B1_X7_S24 #'(a b))
  1016. ('B1_X7_F24 #'(a b))
  1017. ('B1_X7_C24 #'(a b))
  1018. ('X8_S24 #'(arg))
  1019. ('X8_F24 #'(arg))
  1020. ('X8_C24 #'(arg))
  1021. ('X8_L24 #'(label))
  1022. ('X8_S8_I16 #'(a imm))
  1023. ('X8_S8_ZI16 #'(a imm))
  1024. ('X8_S12_S12 #'(a b))
  1025. ('X8_S12_C12 #'(a b))
  1026. ('X8_S12_Z12 #'(a b))
  1027. ('X8_C12_C12 #'(a b))
  1028. ('X8_F12_F12 #'(a b))
  1029. ('X8_S8_S8_S8 #'(a b c))
  1030. ('X8_S8_S8_C8 #'(a b c))
  1031. ('X8_S8_C8_S8 #'(a b c))
  1032. ('X32 #'())))
  1033. (syntax-case x ()
  1034. ((_ name opcode kind word ...)
  1035. (with-syntax (((formal ...)
  1036. (generate-temporaries
  1037. (append-map word-args (syntax->datum #'(word ...)))))
  1038. (encode (shuffling-encoder-name #'kind #'(word ...))))
  1039. #'(lambda (asm formal ...)
  1040. (encode asm formal ... opcode))))))))
  1041. (define assemblers (make-hash-table))
  1042. (eval-when (expand)
  1043. (define-syntax define-assembler
  1044. (lambda (x)
  1045. (syntax-case x ()
  1046. ((_ name opcode kind arg ...)
  1047. (with-syntax ((emit (id-append #'name #'emit- #'name)))
  1048. #'(define emit
  1049. (let ((emit (assembler name opcode kind arg ...)))
  1050. (hashq-set! assemblers 'name emit)
  1051. emit)))))))
  1052. (define-syntax visit-opcodes
  1053. (lambda (x)
  1054. (syntax-case x ()
  1055. ((visit-opcodes macro arg ...)
  1056. (with-syntax (((inst ...)
  1057. (map (lambda (x) (datum->syntax #'macro x))
  1058. (instruction-list))))
  1059. #'(begin
  1060. (macro arg ... . inst)
  1061. ...)))))))
  1062. (visit-opcodes define-assembler)
  1063. ;; Shuffling is a general mechanism to get around address space
  1064. ;; limitations for SP-relative variable references. FP-relative
  1065. ;; variables need special support. Also, some instructions like `mov'
  1066. ;; have multiple variations with different addressing limits.
  1067. (define (emit-mov* asm dst src)
  1068. (if (and (< dst (ash 1 12)) (< src (ash 1 12)))
  1069. (emit-mov asm dst src)
  1070. (emit-long-mov asm dst src)))
  1071. (define (emit-fmov* asm dst src)
  1072. (emit-long-fmov asm dst src))
  1073. (define (emit-receive* asm dst proc nlocals)
  1074. (if (and (< dst (ash 1 12)) (< proc (ash 1 12)))
  1075. (emit-receive asm dst proc nlocals)
  1076. (begin
  1077. (emit-receive-values asm proc #t 1)
  1078. (emit-fmov* asm dst (1+ proc))
  1079. (emit-reset-frame asm nlocals))))
  1080. (define (emit-throw/value* asm val param)
  1081. (emit-throw/value asm val (intern-non-immediate asm param)))
  1082. (define (emit-throw/value+data* asm val param)
  1083. (emit-throw/value+data asm val (intern-non-immediate asm param)))
  1084. (define (emit-instrument-entry* asm)
  1085. (let ((meta (car (asm-meta asm))))
  1086. (emit-instrument-entry asm (meta-jit-data-label meta))))
  1087. (define (emit-instrument-loop* asm)
  1088. (let ((meta (car (asm-meta asm))))
  1089. (emit-instrument-loop asm (meta-jit-data-label meta))))
  1090. (define (emit-text asm instructions)
  1091. "Assemble @var{instructions} using the assembler @var{asm}.
  1092. @var{instructions} is a sequence of instructions, expressed as a list of
  1093. lists. This procedure can be called many times before calling
  1094. @code{link-assembly}."
  1095. (for-each (lambda (inst)
  1096. (apply (or (hashq-ref assemblers (car inst))
  1097. (error 'bad-instruction inst))
  1098. asm
  1099. (cdr inst)))
  1100. instructions))
  1101. ;;;
  1102. ;;; The constant table records a topologically sorted set of literal
  1103. ;;; constants used by a program. For example, a pair uses its car and
  1104. ;;; cdr, a string uses its stringbuf, etc.
  1105. ;;;
  1106. ;;; Some things we want to add to the constant table are not actually
  1107. ;;; Scheme objects: for example, stringbufs, cache cells for toplevel
  1108. ;;; references, or cache cells for non-closure procedures. For these we
  1109. ;;; define special record types and add instances of those record types
  1110. ;;; to the table.
  1111. ;;;
  1112. (define-record-type <stringbuf>
  1113. (make-stringbuf string)
  1114. stringbuf?
  1115. (string stringbuf-string))
  1116. (define-record-type <static-procedure>
  1117. (make-static-procedure code)
  1118. static-procedure?
  1119. (code static-procedure-code))
  1120. (define-record-type <uniform-vector-backing-store>
  1121. (make-uniform-vector-backing-store bytes element-size)
  1122. uniform-vector-backing-store?
  1123. (bytes uniform-vector-backing-store-bytes)
  1124. (element-size uniform-vector-backing-store-element-size))
  1125. (define-record-type <cache-cell>
  1126. (make-cache-cell key)
  1127. cache-cell?
  1128. (key cache-cell-key))
  1129. (define (simple-vector? obj)
  1130. (and (vector? obj)
  1131. (equal? (array-shape obj) (list (list 0 (1- (vector-length obj)))))))
  1132. (define (simple-uniform-vector? obj)
  1133. (and (array? obj)
  1134. (symbol? (array-type obj))
  1135. (match (array-shape obj)
  1136. (((0 n)) #t)
  1137. (else #f))))
  1138. (define (statically-allocatable? x)
  1139. "Return @code{#t} if a non-immediate constant can be allocated
  1140. statically, and @code{#f} if it would need some kind of runtime
  1141. allocation."
  1142. (or (pair? x) (string? x) (stringbuf? x) (static-procedure? x)
  1143. (array? x) (syntax? x)))
  1144. (define (intern-constant asm obj)
  1145. "Add an object to the constant table, and return a label that can be
  1146. used to reference it. If the object is already present in the constant
  1147. table, its existing label is used directly."
  1148. (define (recur obj)
  1149. (intern-constant asm obj))
  1150. (define (add-desc! label desc)
  1151. (set-asm-inits! asm (vhash-consq label desc (asm-inits asm))))
  1152. (define (init-descriptor obj)
  1153. (let ((label (recur obj)))
  1154. (cond
  1155. ((not label) #f)
  1156. ((vhash-assq label (asm-inits asm)) => cdr)
  1157. (else
  1158. (let ((desc (vector #f #t '())))
  1159. (add-desc! label desc)
  1160. desc)))))
  1161. (define (add-patch! dst field obj)
  1162. (match (init-descriptor obj)
  1163. (#f #f)
  1164. ((and desc #(emit-init emit-load patches))
  1165. (vector-set! desc 2 (acons dst field patches)))))
  1166. (define (add-init! dst init)
  1167. (add-desc! dst (vector init #f '())))
  1168. (define (intern! obj label)
  1169. (define (patch! field obj) (add-patch! label field obj))
  1170. (define (init! emit-init) (add-init! label emit-init))
  1171. (cond
  1172. ((pair? obj)
  1173. (patch! 0 (car obj))
  1174. (patch! 1 (cdr obj)))
  1175. ((simple-vector? obj)
  1176. (let lp ((i 0))
  1177. (when (< i (vector-length obj))
  1178. (patch! (1+ i) (vector-ref obj i))
  1179. (lp (1+ i)))))
  1180. ((syntax? obj)
  1181. (patch! 1 (syntax-expression obj))
  1182. (patch! 2 (syntax-wrap obj))
  1183. (patch! 3 (syntax-module obj))
  1184. (patch! 4 (syntax-sourcev obj)))
  1185. ((stringbuf? obj))
  1186. ((static-procedure? obj)
  1187. ;; Special case, as we can't load the procedure's code using
  1188. ;; make-non-immediate.
  1189. (let* ((code (static-procedure-code obj))
  1190. (init (lambda (asm label)
  1191. (emit-static-patch! asm label 1 code)
  1192. #f)))
  1193. (add-desc! label (vector init #t '()))))
  1194. ((cache-cell? obj))
  1195. ((symbol? obj)
  1196. (unless (symbol-interned? obj)
  1197. (error "uninterned symbol cannot be saved to object file" obj))
  1198. (let ((str-label (recur (symbol->string obj))))
  1199. (init! (lambda (asm label)
  1200. (emit-make-non-immediate asm 1 str-label)
  1201. (emit-string->symbol asm 1 1)
  1202. (emit-static-set! asm 1 label 0)
  1203. 1))))
  1204. ((string? obj)
  1205. (patch! 1 (make-stringbuf obj)))
  1206. ((keyword? obj)
  1207. (let ((sym-label (recur (keyword->symbol obj))))
  1208. (init! (lambda (asm label)
  1209. (emit-static-ref asm 1 sym-label)
  1210. (emit-symbol->keyword asm 1 1)
  1211. (emit-static-set! asm 1 label 0)
  1212. 1))))
  1213. ((number? obj)
  1214. (let ((str-label (recur (number->string obj))))
  1215. (init! (lambda (asm label)
  1216. (emit-make-non-immediate asm 1 str-label)
  1217. (emit-string->number asm 1 1)
  1218. (emit-static-set! asm 1 label 0)
  1219. 1))))
  1220. ((uniform-vector-backing-store? obj))
  1221. ((simple-uniform-vector? obj)
  1222. (let ((width (case (array-type obj)
  1223. ((vu8 u8 s8) 1)
  1224. ((u16 s16) 2)
  1225. ;; Bitvectors are addressed in 32-bit units.
  1226. ;; Although a complex number is 8 or 16 bytes wide,
  1227. ;; it should be byteswapped in 4 or 8 byte units.
  1228. ((u32 s32 f32 c32 b) 4)
  1229. ((u64 s64 f64 c64) 8)
  1230. (else
  1231. (error "unhandled array type" obj)))))
  1232. (patch! 2
  1233. (make-uniform-vector-backing-store
  1234. (uniform-array->bytevector obj)
  1235. width))))
  1236. ((array? obj)
  1237. (patch! 1 (shared-array-root obj)))
  1238. (else
  1239. (error "don't know how to intern" obj))))
  1240. (cond
  1241. ((immediate-bits asm obj) #f)
  1242. ((vhash-assoc obj (asm-constants asm)) => cdr)
  1243. (else
  1244. (let ((label (gensym "constant")))
  1245. ;; Note that calling intern may mutate asm-constants and asm-inits.
  1246. (intern! obj label)
  1247. (set-asm-constants! asm (vhash-cons obj label (asm-constants asm)))
  1248. label))))
  1249. (define (intern-non-immediate asm obj)
  1250. "Intern a non-immediate into the constant table, and return its
  1251. label."
  1252. (when (immediate-bits asm obj)
  1253. (error "expected a non-immediate" obj))
  1254. (intern-constant asm obj))
  1255. (define (intern-cache-cell asm key)
  1256. "Intern a cache cell into the constant table, and return its label.
  1257. If there is already a cache cell with the given scope and key, it is
  1258. returned instead."
  1259. (intern-constant asm (make-cache-cell key)))
  1260. ;;;
  1261. ;;; Macro assemblers bridge the gap between primitive instructions and
  1262. ;;; some higher-level operations.
  1263. ;;;
  1264. (eval-when (expand)
  1265. (define-syntax define-macro-assembler
  1266. (lambda (x)
  1267. (syntax-case x ()
  1268. ((_ (name arg ...) body body* ...)
  1269. (with-syntax ((emit (id-append #'name #'emit- #'name)))
  1270. #'(begin
  1271. (define emit
  1272. (let ((emit (lambda (arg ...) body body* ...)))
  1273. (hashq-set! assemblers 'name emit)
  1274. emit))
  1275. (export emit))))))))
  1276. (define-macro-assembler (load-constant asm dst obj)
  1277. (cond
  1278. ((scm->immediate-bits obj)
  1279. => (lambda (bits)
  1280. (cond
  1281. ((and (< dst 256) (truncate-bits bits 16 #t))
  1282. (emit-make-immediate asm dst obj))
  1283. ((and (< dst 256) (truncate-bits bits 16 #f))
  1284. (emit-make-short-immediate asm dst obj))
  1285. ((truncate-bits bits 32 (eqv? (asm-word-size asm) 4))
  1286. (emit-make-long-immediate asm dst obj))
  1287. ((and (eqv? (asm-word-size asm) 8)
  1288. (truncate-bits bits 64 #t))
  1289. (emit-make-long-long-immediate asm dst obj))
  1290. (else
  1291. (emit-static-ref asm dst (intern-non-immediate asm obj))))))
  1292. ((statically-allocatable? obj)
  1293. (emit-make-non-immediate asm dst (intern-non-immediate asm obj)))
  1294. (else
  1295. (emit-static-ref asm dst (intern-non-immediate asm obj)))))
  1296. (define-macro-assembler (load-static-procedure asm dst label)
  1297. (let ((loc (intern-constant asm (make-static-procedure label))))
  1298. (emit-make-non-immediate asm dst loc)))
  1299. (define-syntax define-immediate-tag=?-macro-assembler
  1300. (syntax-rules ()
  1301. ((_ name #f mask tag) #f)
  1302. ((_ name pred mask tag)
  1303. (define-macro-assembler (pred asm slot)
  1304. (emit-immediate-tag=? asm slot mask tag)))))
  1305. (visit-immediate-tags define-immediate-tag=?-macro-assembler)
  1306. (define-syntax-rule (define-heap-tag=?-macro-assembler name pred mask tag)
  1307. (define-macro-assembler (pred asm slot)
  1308. (emit-heap-tag=? asm slot mask tag)))
  1309. (visit-heap-tags define-heap-tag=?-macro-assembler)
  1310. (define-syntax-rule (define-scm<-scm-scm-intrinsic name)
  1311. (define-macro-assembler (name asm dst a b)
  1312. (emit-call-scm<-scm-scm asm dst a b (intrinsic-name->index 'name))))
  1313. (define-syntax-rule (define-scm<-scm-uimm-intrinsic name)
  1314. (define-macro-assembler (name asm dst a b)
  1315. (emit-call-scm<-scm-uimm asm dst a b (intrinsic-name->index 'name))))
  1316. (define-syntax-rule (define-scm-sz-u32-intrinsic name)
  1317. (define-macro-assembler (name asm a b c)
  1318. (emit-call-scm-sz-u32 asm a b c (intrinsic-name->index 'name))))
  1319. (define-syntax-rule (define-scm<-scm-intrinsic name)
  1320. (define-macro-assembler (name asm dst src)
  1321. (emit-call-scm<-scm asm dst src (intrinsic-name->index 'name))))
  1322. (define-syntax-rule (define-f64<-scm-intrinsic name)
  1323. (define-macro-assembler (name asm dst src)
  1324. (emit-call-f64<-scm asm dst src (intrinsic-name->index 'name))))
  1325. (define-syntax-rule (define-f64<-f64-intrinsic name)
  1326. (define-macro-assembler (name asm dst src)
  1327. (emit-call-f64<-f64 asm dst src (intrinsic-name->index 'name))))
  1328. (define-syntax-rule (define-f64<-f64-f64-intrinsic name)
  1329. (define-macro-assembler (name asm dst a b)
  1330. (emit-call-f64<-f64-f64 asm dst a b (intrinsic-name->index 'name))))
  1331. (define-syntax-rule (define-u64<-scm-intrinsic name)
  1332. (define-macro-assembler (name asm dst src)
  1333. (emit-call-u64<-scm asm dst src (intrinsic-name->index 'name))))
  1334. (define-syntax-rule (define-s64<-scm-intrinsic name)
  1335. (define-macro-assembler (name asm dst src)
  1336. (emit-call-s64<-scm asm dst src (intrinsic-name->index 'name))))
  1337. (define-syntax-rule (define-scm<-u64-intrinsic name)
  1338. (define-macro-assembler (name asm dst src)
  1339. (emit-call-scm<-u64 asm dst src (intrinsic-name->index 'name))))
  1340. (define-syntax-rule (define-scm<-s64-intrinsic name)
  1341. (define-macro-assembler (name asm dst src)
  1342. (emit-call-scm<-s64 asm dst src (intrinsic-name->index 'name))))
  1343. (define-syntax-rule (define-thread-intrinsic name)
  1344. (define-macro-assembler (name asm)
  1345. (emit-call-thread asm (intrinsic-name->index 'name))))
  1346. (define-syntax-rule (define-thread-scm-intrinsic name)
  1347. (define-macro-assembler (name asm a)
  1348. (emit-call-thread-scm asm a (intrinsic-name->index 'name))))
  1349. (define-syntax-rule (define-thread-scm-scm-intrinsic name)
  1350. (define-macro-assembler (name asm a b)
  1351. (emit-call-thread-scm-scm asm a b (intrinsic-name->index 'name))))
  1352. (define-syntax-rule (define-scm<-thread-scm-intrinsic name)
  1353. (define-macro-assembler (name asm dst src)
  1354. (emit-call-scm<-thread-scm asm dst src (intrinsic-name->index 'name))))
  1355. (define-syntax-rule (define-scm<-scm-u64-intrinsic name)
  1356. (define-macro-assembler (name asm dst a b)
  1357. (emit-call-scm<-scm-u64 asm dst a b (intrinsic-name->index 'name))))
  1358. (define-syntax-rule (define-scm<-scm-bool-intrinsic name)
  1359. (define-macro-assembler (name asm dst a b)
  1360. (emit-call-scm<-scm-uimm asm dst a (if b 1 0) (intrinsic-name->index 'name))))
  1361. (define-syntax-rule (define-scm<-thread-intrinsic name)
  1362. (define-macro-assembler (name asm dst)
  1363. (emit-call-scm<-thread asm dst (intrinsic-name->index 'name))))
  1364. (define-syntax-rule (define-scm-scm-intrinsic name)
  1365. (define-macro-assembler (name asm a b)
  1366. (emit-call-scm-scm asm a b (intrinsic-name->index 'name))))
  1367. (define-syntax-rule (define-scm-uimm-scm-intrinsic name)
  1368. (define-macro-assembler (name asm a b c)
  1369. (emit-call-scm-uimm-scm asm a b c (intrinsic-name->index 'name))))
  1370. (define-syntax-rule (define-scm-scm-scm-intrinsic name)
  1371. (define-macro-assembler (name asm a b c)
  1372. (emit-call-scm-scm-scm asm a b c (intrinsic-name->index 'name))))
  1373. (define-syntax-rule (define-scm<-scmn-scmn-intrinsic name)
  1374. (define-macro-assembler (name asm dst a b)
  1375. (unless (statically-allocatable? a) (error "not statically allocatable" a))
  1376. (unless (statically-allocatable? b) (error "not statically allocatable" b))
  1377. (let ((a (intern-constant asm a))
  1378. (b (intern-constant asm b)))
  1379. (emit-call-scm<-scmn-scmn asm dst a b (intrinsic-name->index 'name)))))
  1380. (define-scm<-scm-scm-intrinsic add)
  1381. (define-scm<-scm-uimm-intrinsic add/immediate)
  1382. (define-scm<-scm-scm-intrinsic sub)
  1383. (define-scm<-scm-uimm-intrinsic sub/immediate)
  1384. (define-scm<-scm-scm-intrinsic mul)
  1385. (define-scm<-scm-scm-intrinsic div)
  1386. (define-scm<-scm-scm-intrinsic quo)
  1387. (define-scm<-scm-scm-intrinsic rem)
  1388. (define-scm<-scm-scm-intrinsic mod)
  1389. (define-scm<-scm-intrinsic inexact)
  1390. (define-scm<-scm-intrinsic abs)
  1391. (define-scm<-scm-intrinsic sqrt)
  1392. (define-scm<-scm-intrinsic floor)
  1393. (define-scm<-scm-intrinsic ceiling)
  1394. (define-scm<-scm-intrinsic sin)
  1395. (define-scm<-scm-intrinsic cos)
  1396. (define-scm<-scm-intrinsic tan)
  1397. (define-scm<-scm-intrinsic asin)
  1398. (define-scm<-scm-intrinsic acos)
  1399. (define-scm<-scm-intrinsic atan)
  1400. (define-scm<-scm-scm-intrinsic atan2)
  1401. (define-f64<-f64-intrinsic fabs)
  1402. (define-f64<-f64-intrinsic fsqrt)
  1403. (define-f64<-f64-intrinsic ffloor)
  1404. (define-f64<-f64-intrinsic fceiling)
  1405. (define-f64<-f64-intrinsic fsin)
  1406. (define-f64<-f64-intrinsic fcos)
  1407. (define-f64<-f64-intrinsic ftan)
  1408. (define-f64<-f64-intrinsic fasin)
  1409. (define-f64<-f64-intrinsic facos)
  1410. (define-f64<-f64-intrinsic fatan)
  1411. (define-f64<-f64-f64-intrinsic fatan2)
  1412. (define-scm<-scm-scm-intrinsic logand)
  1413. (define-scm<-scm-scm-intrinsic logior)
  1414. (define-scm<-scm-scm-intrinsic logxor)
  1415. (define-scm<-scm-scm-intrinsic logsub)
  1416. (define-scm-sz-u32-intrinsic string-set!)
  1417. (define-scm<-scm-intrinsic string->number)
  1418. (define-scm<-scm-intrinsic string->symbol)
  1419. (define-scm<-scm-intrinsic symbol->keyword)
  1420. (define-scm<-scm-intrinsic class-of)
  1421. (define-f64<-scm-intrinsic scm->f64)
  1422. (define-u64<-scm-intrinsic scm->u64)
  1423. (define-u64<-scm-intrinsic scm->u64/truncate)
  1424. (define-s64<-scm-intrinsic scm->s64)
  1425. (define-scm<-u64-intrinsic u64->scm)
  1426. (define-scm<-s64-intrinsic s64->scm)
  1427. (define-thread-scm-scm-intrinsic wind)
  1428. (define-thread-intrinsic unwind)
  1429. (define-thread-scm-scm-intrinsic push-fluid)
  1430. (define-thread-intrinsic pop-fluid)
  1431. (define-scm<-thread-scm-intrinsic fluid-ref)
  1432. (define-thread-scm-scm-intrinsic fluid-set!)
  1433. (define-thread-scm-intrinsic push-dynamic-state)
  1434. (define-thread-intrinsic pop-dynamic-state)
  1435. (define-scm<-scm-u64-intrinsic lsh)
  1436. (define-scm<-scm-u64-intrinsic rsh)
  1437. (define-scm<-scm-uimm-intrinsic lsh/immediate)
  1438. (define-scm<-scm-uimm-intrinsic rsh/immediate)
  1439. (define-scm<-scm-bool-intrinsic resolve-module)
  1440. (define-scm<-scm-scm-intrinsic module-variable)
  1441. (define-scm<-scm-scm-intrinsic lookup)
  1442. (define-scm<-scm-scm-intrinsic lookup-bound)
  1443. (define-scm<-scmn-scmn-intrinsic lookup-bound-public)
  1444. (define-scm<-scmn-scmn-intrinsic lookup-bound-private)
  1445. (define-scm<-scm-scm-intrinsic define!)
  1446. (define-scm<-thread-intrinsic current-module)
  1447. (define-scm<-scm-intrinsic $car)
  1448. (define-scm<-scm-intrinsic $cdr)
  1449. (define-scm-scm-intrinsic $set-car!)
  1450. (define-scm-scm-intrinsic $set-cdr!)
  1451. (define-scm<-scm-intrinsic $variable-ref)
  1452. (define-scm-scm-intrinsic $variable-set!)
  1453. (define-scm<-scm-intrinsic $vector-length)
  1454. (define-scm<-scm-scm-intrinsic $vector-ref)
  1455. (define-scm-scm-scm-intrinsic $vector-set!)
  1456. (define-scm<-scm-uimm-intrinsic $vector-ref/immediate)
  1457. (define-scm-uimm-scm-intrinsic $vector-set!/immediate)
  1458. (define-scm<-scm-scm-intrinsic $allocate-struct)
  1459. (define-scm<-scm-intrinsic $struct-vtable)
  1460. (define-scm<-scm-scm-intrinsic $struct-ref)
  1461. (define-scm-scm-scm-intrinsic $struct-set!)
  1462. (define-scm<-scm-uimm-intrinsic $struct-ref/immediate)
  1463. (define-scm-uimm-scm-intrinsic $struct-set!/immediate)
  1464. (define-macro-assembler (begin-program asm label properties)
  1465. (emit-label asm label)
  1466. (let ((meta (make-meta label properties (asm-start asm))))
  1467. (set-asm-meta! asm (cons meta (asm-meta asm))))
  1468. (emit-instrument-entry* asm))
  1469. (define-macro-assembler (end-program asm)
  1470. (let ((meta (car (asm-meta asm))))
  1471. (set-meta-high-pc! meta (asm-start asm))
  1472. (set-meta-arities! meta (reverse (meta-arities meta)))
  1473. (set-asm-constants!
  1474. asm
  1475. (vhash-cons (make-jit-data (meta-low-pc meta) (meta-high-pc meta))
  1476. (meta-jit-data-label meta)
  1477. (asm-constants asm)))))
  1478. (define-macro-assembler (begin-standard-arity asm has-closure? req nlocals
  1479. alternate)
  1480. (emit-begin-opt-arity asm has-closure? req '() #f nlocals alternate))
  1481. (define-macro-assembler (begin-opt-arity asm has-closure? req opt rest nlocals
  1482. alternate)
  1483. (emit-begin-kw-arity asm has-closure? req opt rest '() #f nlocals alternate))
  1484. (define-macro-assembler (begin-kw-arity asm has-closure? req opt rest kw-indices
  1485. allow-other-keys? nlocals alternate)
  1486. (assert-match req ((? symbol?) ...) "list of symbols")
  1487. (assert-match opt ((? symbol?) ...) "list of symbols")
  1488. (assert-match rest (or #f (? symbol?)) "#f or symbol")
  1489. (assert-match kw-indices (((? keyword?) . (? integer?)) ...)
  1490. "alist of keyword -> integer")
  1491. (assert-match allow-other-keys? (? boolean?) "boolean")
  1492. (assert-match nlocals (? integer?) "integer")
  1493. (assert-match alternate (or #f (? exact-integer?) (? symbol?)) "#f or symbol")
  1494. (let* ((meta (car (asm-meta asm)))
  1495. (arity (make-arity req opt rest kw-indices allow-other-keys?
  1496. has-closure?
  1497. ;; Include the initial instrument-entry in
  1498. ;; the first arity.
  1499. (if (null? (meta-arities meta))
  1500. (meta-low-pc meta)
  1501. (asm-start asm))
  1502. #f '()))
  1503. ;; The procedure itself is in slot 0, in the standard calling
  1504. ;; convention. For procedure prologues, nreq includes the
  1505. ;; procedure, so here we add 1.
  1506. (nclosure (if has-closure? 1 0))
  1507. (nreq (+ nclosure (length req)))
  1508. (nopt (length opt))
  1509. (rest? (->bool rest)))
  1510. (set-meta-arities! meta (cons arity (meta-arities meta)))
  1511. (cond
  1512. ((or allow-other-keys? (pair? kw-indices))
  1513. (emit-kw-prelude asm nreq nopt rest? kw-indices allow-other-keys?
  1514. nlocals alternate))
  1515. ((or rest? (pair? opt))
  1516. (emit-opt-prelude asm nreq nopt rest? nlocals alternate))
  1517. (else
  1518. (emit-standard-prelude asm nreq nlocals alternate)))))
  1519. (define-macro-assembler (begin-unchecked-arity asm has-closure? req nlocals)
  1520. (assert-match req ((? symbol?) ...) "list of symbols")
  1521. (assert-match nlocals (? integer?) "integer")
  1522. (let* ((meta (car (asm-meta asm)))
  1523. (arity (make-arity req '() #f '() #f has-closure?
  1524. (meta-low-pc meta) #f '()))
  1525. (nclosure (if has-closure? 1 0))
  1526. (nreq (+ nclosure (length req))))
  1527. (set-meta-arities! meta (cons arity (meta-arities meta)))
  1528. (emit-unchecked-prelude asm nreq nlocals)))
  1529. (define-macro-assembler (end-arity asm)
  1530. (let ((arity (car (meta-arities (car (asm-meta asm))))))
  1531. (set-arity-definitions! arity (reverse (arity-definitions arity)))
  1532. (set-arity-high-pc! arity (asm-start asm))))
  1533. (define-macro-assembler (unchecked-prelude asm nreq nlocals)
  1534. (unless (= nlocals nreq)
  1535. (emit-alloc-frame asm nlocals)))
  1536. (define-macro-assembler (standard-prelude asm nreq nlocals alternate)
  1537. (cond
  1538. (alternate
  1539. (emit-arguments<=? asm nreq)
  1540. (emit-jne asm alternate)
  1541. (emit-alloc-frame asm nlocals))
  1542. ((and (< nreq (ash 1 12)) (< (- nlocals nreq) (ash 1 12)))
  1543. (emit-assert-nargs-ee/locals asm nreq (- nlocals nreq)))
  1544. (else
  1545. (emit-assert-nargs-ee asm nreq)
  1546. (emit-alloc-frame asm nlocals))))
  1547. (define-macro-assembler (opt-prelude asm nreq nopt rest? nlocals alternate)
  1548. (if alternate
  1549. (begin
  1550. (emit-arguments<=? asm nreq)
  1551. (emit-jl asm alternate))
  1552. (emit-assert-nargs-ge asm nreq))
  1553. (cond
  1554. (rest?
  1555. (unless (zero? nopt)
  1556. (emit-bind-optionals asm (+ nreq nopt)))
  1557. (emit-bind-rest asm (+ nreq nopt)))
  1558. (alternate
  1559. (emit-arguments<=? asm (+ nreq nopt))
  1560. ;; The arguments<=? instruction sets NONE to indicate greater-than,
  1561. ;; whereas for <, NONE usually indicates greater-than-or-equal,
  1562. ;; hence the name jge. Perhaps we just need to rename jge to
  1563. ;; br-if-none.
  1564. (emit-jge asm alternate)
  1565. (unless (zero? nopt)
  1566. (emit-bind-optionals asm (+ nreq nopt))))
  1567. (else
  1568. (emit-assert-nargs-le asm (+ nreq nopt))
  1569. (unless (zero? nopt)
  1570. (emit-bind-optionals asm (+ nreq nopt)))))
  1571. (emit-alloc-frame asm nlocals))
  1572. (define-macro-assembler (kw-prelude asm nreq nopt rest? kw-indices
  1573. allow-other-keys? nlocals alternate)
  1574. (if alternate
  1575. (begin
  1576. (emit-arguments<=? asm nreq)
  1577. (emit-jl asm alternate)
  1578. (unless rest?
  1579. (emit-positional-arguments<=? asm nreq (+ nreq nopt))
  1580. (emit-jge asm alternate)))
  1581. (emit-assert-nargs-ge asm nreq))
  1582. (let ((ntotal (fold (lambda (kw ntotal)
  1583. (match kw
  1584. (((? keyword?) . idx)
  1585. (max (1+ idx) ntotal))))
  1586. (+ nreq nopt) kw-indices)))
  1587. ;; FIXME: port 581f410f
  1588. (emit-bind-kwargs asm nreq
  1589. (pack-flags allow-other-keys? rest?)
  1590. (+ nreq nopt)
  1591. ntotal
  1592. (intern-constant asm kw-indices))
  1593. (emit-alloc-frame asm nlocals)))
  1594. (define-macro-assembler (label asm sym)
  1595. (hashq-set! (asm-labels asm) sym (asm-start asm)))
  1596. (define-macro-assembler (source asm source)
  1597. (set-asm-sources! asm (acons (asm-start asm) source (asm-sources asm))))
  1598. (define-macro-assembler (definition asm name slot representation)
  1599. (let* ((arity (car (meta-arities (car (asm-meta asm)))))
  1600. (def (vector name slot representation
  1601. (- (asm-start asm) (arity-low-pc arity)))))
  1602. (set-arity-definitions! arity (cons def (arity-definitions arity)))))
  1603. (define-macro-assembler (cache-ref asm dst key)
  1604. (emit-static-ref asm dst (intern-cache-cell asm key)))
  1605. (define-macro-assembler (cache-set! asm key val)
  1606. (emit-static-set! asm val (intern-cache-cell asm key) 0))
  1607. (define-macro-assembler (slot-map asm proc-slot slot-map)
  1608. (unless (zero? slot-map)
  1609. (set-asm-slot-maps! asm (cons
  1610. (cons* (asm-start asm) proc-slot slot-map)
  1611. (asm-slot-maps asm)))))
  1612. ;;;
  1613. ;;; Helper for linking objects.
  1614. ;;;
  1615. (define (make-object asm name size writer relocs labels . kwargs)
  1616. "Make a linker object. This helper handles interning the name in the
  1617. shstrtab, assigning the size, allocating a fresh index, and defining a
  1618. corresponding linker symbol for the start of the section."
  1619. (let ((name-idx (intern-section-name! asm (symbol->string name)))
  1620. (index (asm-next-section-number asm)))
  1621. (set-asm-next-section-number! asm (1+ index))
  1622. (make-linker-object (symbol->string name)
  1623. (apply make-elf-section
  1624. #:index index
  1625. #:name name-idx
  1626. #:size size
  1627. kwargs)
  1628. size writer relocs
  1629. (cons (make-linker-symbol name 0) labels))))
  1630. ;;;
  1631. ;;; Linking the constant table. This code is somewhat intertwingled
  1632. ;;; with the intern-constant code above, as that procedure also
  1633. ;;; residualizes instructions to initialize constants at load time.
  1634. ;;;
  1635. (define (write-immediate asm buf pos bits)
  1636. (let ((endianness (asm-endianness asm)))
  1637. (case (asm-word-size asm)
  1638. ((4) (bytevector-u32-set! buf pos bits endianness))
  1639. ((8) (bytevector-u64-set! buf pos bits endianness))
  1640. (else (error "bad word size" asm)))))
  1641. (define (write-placeholder asm buf pos)
  1642. (write-immediate asm buf pos (immediate-bits asm #f)))
  1643. (define (emit-init-constants asm)
  1644. "If there is writable data that needs initialization at runtime, emit
  1645. a procedure to do that and return its label. Otherwise return
  1646. @code{#f}."
  1647. (let* ((inits (asm-inits asm)))
  1648. (and (not (vlist-null? inits))
  1649. (let ((label (gensym "init-constants")))
  1650. (emit-begin-program asm label '())
  1651. (emit-assert-nargs-ee/locals asm 1 1)
  1652. (let lp ((n (1- (vlist-length inits))))
  1653. (match (vlist-ref inits n)
  1654. ((label . #(#f #t ((dst . field))))
  1655. ;; Special case in which emit-static-patch is actually
  1656. ;; an optimization.
  1657. (emit-static-patch! asm dst field label))
  1658. ((label . #(emit-init static? patches))
  1659. (let ((slot-from-init (and emit-init (emit-init asm label))))
  1660. (unless (null? patches)
  1661. (let ((slot (or slot-from-init
  1662. (begin
  1663. (if static?
  1664. (emit-make-non-immediate asm 1 label)
  1665. (emit-static-ref asm 1 label))
  1666. 1))))
  1667. (for-each (match-lambda
  1668. ((dst . offset)
  1669. (emit-static-set! asm slot dst offset)))
  1670. patches))))))
  1671. (unless (zero? n)
  1672. (lp (1- n))))
  1673. (emit-reset-frame asm 1)
  1674. (emit-load-constant asm 0 *unspecified*)
  1675. (emit-return-values asm)
  1676. (emit-end-program asm)
  1677. label))))
  1678. (define (link-data asm data name)
  1679. "Link the static data for a program into the @var{name} section (which
  1680. should be .data or .rodata), and return the resulting linker object.
  1681. @var{data} should be a vhash mapping objects to labels."
  1682. (define (align address alignment)
  1683. (+ address
  1684. (modulo (- alignment (modulo address alignment)) alignment)))
  1685. (define tc7-vector #x0d)
  1686. (define vector-immutable-flag #x80)
  1687. (define tc7-string #x15)
  1688. (define string-read-only-flag #x200)
  1689. (define tc7-stringbuf #x27)
  1690. (define stringbuf-wide-flag #x400)
  1691. (define tc7-syntax #x3d)
  1692. (define syntax-has-source-flag #x100)
  1693. (define tc7-program #x45)
  1694. (define tc7-bytevector #x4d)
  1695. ;; This flag is intended to be left-shifted by 7 bits.
  1696. (define bytevector-immutable-flag #x200)
  1697. (define tc7-array #x5d)
  1698. (define tc7-bitvector #x5f)
  1699. (define bitvector-immutable-flag #x80)
  1700. (let ((word-size (asm-word-size asm))
  1701. (endianness (asm-endianness asm)))
  1702. (define (byte-length x)
  1703. (cond
  1704. ((stringbuf? x)
  1705. (let ((x (stringbuf-string x)))
  1706. (+ (* 2 word-size)
  1707. (case (string-bytes-per-char x)
  1708. ((1) (1+ (string-length x)))
  1709. ((4) (* (1+ (string-length x)) 4))
  1710. (else (error "bad string bytes per char" x))))))
  1711. ((static-procedure? x)
  1712. (* 2 word-size))
  1713. ((string? x)
  1714. (* 4 word-size))
  1715. ((pair? x)
  1716. (* 2 word-size))
  1717. ((simple-vector? x)
  1718. (* (1+ (vector-length x)) word-size))
  1719. ((syntax? x)
  1720. (* 5 word-size))
  1721. ((jit-data? x)
  1722. (case word-size
  1723. ((4) (+ word-size (* 4 3)))
  1724. ((8) (+ word-size (* 4 4))) ;; One additional uint32_t for padding.
  1725. (else (error word-size))))
  1726. ((simple-uniform-vector? x)
  1727. (* 4 word-size))
  1728. ((uniform-vector-backing-store? x)
  1729. (bytevector-length (uniform-vector-backing-store-bytes x)))
  1730. ((array? x)
  1731. (* word-size (+ 3 (* 3 (array-rank x)))))
  1732. (else
  1733. word-size)))
  1734. (define (write-constant-reference buf pos x)
  1735. (let ((bits (immediate-bits asm x)))
  1736. (if bits
  1737. (write-immediate asm buf pos bits)
  1738. ;; The asm-inits will fix up any reference to a
  1739. ;; non-immediate.
  1740. (write-placeholder asm buf pos))))
  1741. (define (write buf pos obj)
  1742. (cond
  1743. ((stringbuf? obj)
  1744. (let* ((x (stringbuf-string obj))
  1745. (len (string-length x))
  1746. (tag (logior tc7-stringbuf
  1747. (if (= (string-bytes-per-char x) 1)
  1748. 0
  1749. stringbuf-wide-flag))))
  1750. (case word-size
  1751. ((4)
  1752. (bytevector-u32-set! buf pos tag endianness)
  1753. (bytevector-u32-set! buf (+ pos 4) len endianness))
  1754. ((8)
  1755. (bytevector-u64-set! buf pos tag endianness)
  1756. (bytevector-u64-set! buf (+ pos 8) len endianness))
  1757. (else
  1758. (error "bad word size" asm)))
  1759. (let ((pos (+ pos (* word-size 2))))
  1760. (case (string-bytes-per-char x)
  1761. ((1)
  1762. (let lp ((i 0))
  1763. (if (< i len)
  1764. (let ((u8 (char->integer (string-ref x i))))
  1765. (bytevector-u8-set! buf (+ pos i) u8)
  1766. (lp (1+ i)))
  1767. (bytevector-u8-set! buf (+ pos i) 0))))
  1768. ((4)
  1769. (let lp ((i 0))
  1770. (if (< i len)
  1771. (let ((u32 (char->integer (string-ref x i))))
  1772. (bytevector-u32-set! buf (+ pos (* i 4)) u32 endianness)
  1773. (lp (1+ i)))
  1774. (bytevector-u32-set! buf (+ pos (* i 4)) 0 endianness))))
  1775. (else (error "bad string bytes per char" x))))))
  1776. ((static-procedure? obj)
  1777. (case word-size
  1778. ((4)
  1779. (bytevector-u32-set! buf pos tc7-program endianness)
  1780. (bytevector-u32-set! buf (+ pos 4) 0 endianness))
  1781. ((8)
  1782. (bytevector-u64-set! buf pos tc7-program endianness)
  1783. (bytevector-u64-set! buf (+ pos 8) 0 endianness))
  1784. (else (error "bad word size"))))
  1785. ((cache-cell? obj)
  1786. (write-placeholder asm buf pos))
  1787. ((jit-data? obj)
  1788. ;; Default initialization of 0.
  1789. (values))
  1790. ((string? obj)
  1791. (let ((tag (logior tc7-string string-read-only-flag)))
  1792. (case word-size
  1793. ((4)
  1794. (bytevector-u32-set! buf pos tag endianness)
  1795. (write-placeholder asm buf (+ pos 4)) ; stringbuf
  1796. (bytevector-u32-set! buf (+ pos 8) 0 endianness)
  1797. (bytevector-u32-set! buf (+ pos 12) (string-length obj) endianness))
  1798. ((8)
  1799. (bytevector-u64-set! buf pos tag endianness)
  1800. (write-placeholder asm buf (+ pos 8)) ; stringbuf
  1801. (bytevector-u64-set! buf (+ pos 16) 0 endianness)
  1802. (bytevector-u64-set! buf (+ pos 24) (string-length obj) endianness))
  1803. (else (error "bad word size")))))
  1804. ((pair? obj)
  1805. (write-constant-reference buf pos (car obj))
  1806. (write-constant-reference buf (+ pos word-size) (cdr obj)))
  1807. ((simple-vector? obj)
  1808. (let* ((len (vector-length obj))
  1809. (tag (logior tc7-vector vector-immutable-flag (ash len 8))))
  1810. (case word-size
  1811. ((4) (bytevector-u32-set! buf pos tag endianness))
  1812. ((8) (bytevector-u64-set! buf pos tag endianness))
  1813. (else (error "bad word size")))
  1814. (let lp ((i 0))
  1815. (when (< i (vector-length obj))
  1816. (let ((pos (+ pos word-size (* i word-size)))
  1817. (elt (vector-ref obj i)))
  1818. (write-constant-reference buf pos elt)
  1819. (lp (1+ i)))))))
  1820. ((symbol? obj)
  1821. (write-placeholder asm buf pos))
  1822. ((keyword? obj)
  1823. (write-placeholder asm buf pos))
  1824. ((syntax? obj)
  1825. (let ((tag (logior tc7-syntax syntax-has-source-flag)))
  1826. (case word-size
  1827. ((4) (bytevector-u32-set! buf pos tag endianness))
  1828. ((8) (bytevector-u64-set! buf pos tag endianness))
  1829. (else (error "bad word size"))))
  1830. (write-constant-reference buf (+ pos (* 1 word-size))
  1831. (syntax-expression obj))
  1832. (write-constant-reference buf (+ pos (* 2 word-size))
  1833. (syntax-wrap obj))
  1834. (write-constant-reference buf (+ pos (* 3 word-size))
  1835. (syntax-module obj))
  1836. (write-constant-reference buf (+ pos (* 4 word-size))
  1837. (syntax-sourcev obj)))
  1838. ((number? obj)
  1839. (write-placeholder asm buf pos))
  1840. ((simple-uniform-vector? obj)
  1841. (let ((tag (if (bitvector? obj)
  1842. (logior tc7-bitvector
  1843. bitvector-immutable-flag)
  1844. (logior tc7-bytevector
  1845. ;; Bytevector immutable flag also shifted
  1846. ;; left.
  1847. (ash (logior bytevector-immutable-flag
  1848. (array-type-code obj))
  1849. 7)))))
  1850. (case word-size
  1851. ((4)
  1852. (bytevector-u32-set! buf pos tag endianness)
  1853. (bytevector-u32-set! buf (+ pos 4)
  1854. (if (bitvector? obj)
  1855. (bitvector-length obj)
  1856. (bytevector-length obj))
  1857. endianness) ; length
  1858. (bytevector-u32-set! buf (+ pos 8) 0 endianness) ; pointer
  1859. (write-placeholder asm buf (+ pos 12))) ; owner
  1860. ((8)
  1861. (bytevector-u64-set! buf pos tag endianness)
  1862. (bytevector-u64-set! buf (+ pos 8)
  1863. (if (bitvector? obj)
  1864. (bitvector-length obj)
  1865. (bytevector-length obj))
  1866. endianness) ; length
  1867. (bytevector-u64-set! buf (+ pos 16) 0 endianness) ; pointer
  1868. (write-placeholder asm buf (+ pos 24))) ; owner
  1869. (else (error "bad word size")))))
  1870. ((uniform-vector-backing-store? obj)
  1871. (let ((bv (uniform-vector-backing-store-bytes obj)))
  1872. (bytevector-copy! bv 0 buf pos (bytevector-length bv))
  1873. (unless (eq? endianness (native-endianness))
  1874. (case (uniform-vector-backing-store-element-size obj)
  1875. ((1) #f) ;; Nothing to do.
  1876. ((2) (byte-swap/2! buf pos (+ pos (bytevector-length bv))))
  1877. ((4) (byte-swap/4! buf pos (+ pos (bytevector-length bv))))
  1878. ((8) (byte-swap/8! buf pos (+ pos (bytevector-length bv))))
  1879. (else (error "FIXME: Implement byte order swap"))))))
  1880. ((array? obj)
  1881. (let-values
  1882. ;; array tag + rank
  1883. ;; see libguile/arrays.h: SCM_I_ARRAY_NDIM, SCM_I_ARRAYP, scm_i_raw_array
  1884. (((tag) (logior tc7-array (ash (array-rank obj) 17)))
  1885. ((bv-set! bvs-set!)
  1886. (case word-size
  1887. ((4) (values bytevector-u32-set! bytevector-s32-set!))
  1888. ((8) (values bytevector-u64-set! bytevector-s64-set!))
  1889. (else (error "bad word size")))))
  1890. (bv-set! buf pos tag endianness)
  1891. (write-placeholder asm buf (+ pos word-size)) ; root vector (fixed later)
  1892. (bv-set! buf (+ pos (* word-size 2)) 0 endianness) ; base
  1893. (let lp ((pos (+ pos (* word-size 3)))
  1894. (bounds (array-shape obj))
  1895. (incs (shared-array-increments obj)))
  1896. (when (pair? bounds)
  1897. (bvs-set! buf pos (first (first bounds)) endianness)
  1898. (bvs-set! buf (+ pos word-size) (second (first bounds)) endianness)
  1899. (bvs-set! buf (+ pos (* word-size 2)) (first incs) endianness)
  1900. (lp (+ pos (* 3 word-size)) (cdr bounds) (cdr incs))))))
  1901. (else
  1902. (error "unrecognized object" obj))))
  1903. (define (add-relocs obj pos relocs)
  1904. (match obj
  1905. (($ <jit-data> low-pc high-pc)
  1906. ;; Patch "start" and "end" fields of "struct jit_data".
  1907. (cons* (make-linker-reloc 'rel32/1 (+ pos word-size 4)
  1908. (+ low-pc word-size 4)
  1909. '.rtl-text)
  1910. (make-linker-reloc 'rel32/1 (+ pos word-size 8)
  1911. (+ high-pc word-size 8)
  1912. '.rtl-text)
  1913. relocs))
  1914. (_ relocs)))
  1915. (cond
  1916. ((vlist-null? data) #f)
  1917. (else
  1918. (let* ((byte-len (vhash-fold (lambda (k v len)
  1919. (+ (byte-length k) (align len 8)))
  1920. 0 data)))
  1921. (let lp ((i 0) (pos 0) (relocs '()) (symbols '()))
  1922. (if (< i (vlist-length data))
  1923. (match (vlist-ref data i)
  1924. ((obj . obj-label)
  1925. (lp (1+ i)
  1926. (align (+ (byte-length obj) pos) 8)
  1927. (add-relocs obj pos relocs)
  1928. (cons (make-linker-symbol obj-label pos) symbols))))
  1929. (make-object asm name byte-len
  1930. (lambda (bv)
  1931. (let loop ((i 0) (pos 0))
  1932. (when (< i (vlist-length data))
  1933. (match (vlist-ref data i)
  1934. ((obj . obj-label)
  1935. (write bv pos obj)
  1936. (loop (1+ i)
  1937. (align
  1938. (+ (byte-length obj) pos)
  1939. 8)))))))
  1940. relocs symbols
  1941. #:flags (match name
  1942. ('.data (logior SHF_ALLOC SHF_WRITE))
  1943. ('.rodata SHF_ALLOC))))))))))
  1944. (define (link-constants asm)
  1945. "Link sections to hold constants needed by the program text emitted
  1946. using @var{asm}.
  1947. Returns three values: an object for the .rodata section, an object for
  1948. the .data section, and a label for an initialization procedure. Any of
  1949. these may be @code{#f}."
  1950. (define (shareable? x)
  1951. (cond
  1952. ((stringbuf? x) #t)
  1953. ((pair? x)
  1954. (and (immediate-bits asm (car x)) (immediate-bits asm (cdr x))))
  1955. ((simple-vector? x)
  1956. (let lp ((i 0))
  1957. (or (= i (vector-length x))
  1958. (and (immediate-bits asm (vector-ref x i))
  1959. (lp (1+ i))))))
  1960. ((uniform-vector-backing-store? x) #t)
  1961. (else #f)))
  1962. (let* ((init-constants (emit-init-constants asm))
  1963. (constants (asm-constants asm))
  1964. (len (vlist-length constants)))
  1965. (let lp ((i 0)
  1966. (ro vlist-null)
  1967. (rw vlist-null))
  1968. (if (= i len)
  1969. (values (link-data asm ro '.rodata)
  1970. (link-data asm rw '.data)
  1971. init-constants)
  1972. (match (vlist-ref constants i)
  1973. ((obj . label)
  1974. (if (shareable? obj)
  1975. (lp (1+ i) (vhash-consq obj label ro) rw)
  1976. (lp (1+ i) ro (vhash-consq obj label rw)))))))))
  1977. ;;;
  1978. ;;; Linking program text.
  1979. ;;;
  1980. (define (process-relocs relocs labels)
  1981. "Return a list of linker relocations for references to symbols defined
  1982. outside the text section."
  1983. (fold (lambda (reloc tail)
  1984. (match reloc
  1985. ((type label base offset)
  1986. (let ((abs (hashq-ref labels label))
  1987. (dst (+ base offset)))
  1988. (case type
  1989. ((s32)
  1990. (if abs
  1991. tail
  1992. (cons (make-linker-reloc 'rel32/4 dst offset label)
  1993. tail)))
  1994. ((x8-s24)
  1995. (unless abs
  1996. (error "unbound near relocation" reloc))
  1997. tail)
  1998. (else (error "bad relocation kind" reloc)))))))
  1999. '()
  2000. relocs))
  2001. (define (patch-relocs! buf relocs labels)
  2002. "Patch up internal x8-s24 relocations, and any s32 relocations that
  2003. reference symbols in the text section."
  2004. (for-each (lambda (reloc)
  2005. (match reloc
  2006. ((type label base offset)
  2007. (let ((abs (hashq-ref labels label))
  2008. (dst (+ base offset)))
  2009. (case type
  2010. ((s32)
  2011. (when abs
  2012. (let ((rel (- abs base)))
  2013. (unless (zero? (logand rel #x3))
  2014. (error "reloc not in 32-bit units!"))
  2015. (bytevector-s32-native-set! buf dst (ash rel -2)))))
  2016. ((x8-s24)
  2017. (unless abs
  2018. (error "unbound near relocation" reloc))
  2019. (let ((rel (- abs base))
  2020. (u32 (bytevector-u32-native-ref buf dst)))
  2021. (unless (zero? (logand rel #x3))
  2022. (error "reloc not in 32-bit units!"))
  2023. (bytevector-u32-native-set! buf dst
  2024. (pack-u8-s24 (logand u32 #xff)
  2025. (ash rel -2)))))
  2026. (else (error "bad relocation kind" reloc)))))))
  2027. relocs))
  2028. (define (process-labels labels)
  2029. "Define linker symbols for the label-offset map in @var{labels}.
  2030. The offsets are expected to be expressed in words."
  2031. (hash-map->list (lambda (label loc)
  2032. (make-linker-symbol label loc))
  2033. labels))
  2034. (define (link-text-object asm)
  2035. "Link the .rtl-text section, swapping the endianness of the bytes if
  2036. needed."
  2037. (let ((size (asm-pos asm)))
  2038. (make-object asm '.rtl-text size
  2039. (lambda (buf)
  2040. (bytevector-copy! (asm-buf asm) 0 buf 0 size)
  2041. (unless (eq? (asm-endianness asm) (native-endianness))
  2042. (byte-swap/4! buf))
  2043. (patch-relocs! buf (asm-relocs asm) (asm-labels asm)))
  2044. (process-relocs (asm-relocs asm)
  2045. (asm-labels asm))
  2046. (process-labels (asm-labels asm)))))
  2047. ;;;
  2048. ;;; Create the frame maps. These maps are used by GC to identify dead
  2049. ;;; slots in pending call frames, to avoid marking them. We only do
  2050. ;;; this when frame makes a non-tail call, as that is the common case.
  2051. ;;; Only the topmost frame will see a GC at any other point, but we mark
  2052. ;;; top frames conservatively as serializing live slot maps at every
  2053. ;;; instruction would take up too much space in the object file.
  2054. ;;;
  2055. ;; The .guile.frame-maps section starts with two packed u32 values: one
  2056. ;; indicating the offset of the first byte of the .rtl-text section, and
  2057. ;; another indicating the relative offset in bytes of the slots data.
  2058. (define frame-maps-prefix-len 8)
  2059. ;; Each header is 8 bytes: 4 for the offset from .rtl_text, and 4 for
  2060. ;; the offset of the slot map from the beginning of the
  2061. ;; .guile.frame-maps section. The length of a frame map depends on the
  2062. ;; frame size at the call site, and is not encoded into this section as
  2063. ;; it is available at run-time.
  2064. (define frame-map-header-len 8)
  2065. (define (link-frame-maps asm)
  2066. (define (map-byte-length proc-slot)
  2067. (ceiling-quotient (* 2 (- proc-slot 2)) 8))
  2068. (define (make-frame-maps maps count map-len)
  2069. (let* ((endianness (asm-endianness asm))
  2070. (header-pos frame-maps-prefix-len)
  2071. (map-pos (+ header-pos (* count frame-map-header-len)))
  2072. (size (+ map-pos map-len)))
  2073. (define (write! bv)
  2074. (bytevector-u32-set! bv 4 map-pos endianness)
  2075. (let lp ((maps maps) (header-pos header-pos) (map-pos map-pos))
  2076. (match maps
  2077. (()
  2078. #t)
  2079. (((pos proc-slot . map) . maps)
  2080. (bytevector-u32-set! bv header-pos pos endianness)
  2081. (bytevector-u32-set! bv (+ header-pos 4) map-pos endianness)
  2082. (let write-bytes ((map-pos map-pos)
  2083. (map map)
  2084. (byte-length (map-byte-length proc-slot)))
  2085. (if (zero? byte-length)
  2086. (lp maps (+ header-pos frame-map-header-len) map-pos)
  2087. (begin
  2088. (bytevector-u8-set! bv map-pos (logand map #xff))
  2089. (write-bytes (1+ map-pos) (ash map -8)
  2090. (1- byte-length)))))))))
  2091. (make-object asm '.guile.frame-maps size write!
  2092. (list (make-linker-reloc 'abs32/1 0 0 '.rtl-text))
  2093. '() #:type SHT_PROGBITS #:flags SHF_ALLOC)))
  2094. (match (asm-slot-maps asm)
  2095. (() #f)
  2096. (in
  2097. (let lp ((in in) (out '()) (count 0) (map-len 0))
  2098. (match in
  2099. (() (make-frame-maps out count map-len))
  2100. (((and head (pos proc-slot . map)) . in)
  2101. (lp in (cons head out)
  2102. (1+ count)
  2103. (+ (map-byte-length proc-slot) map-len))))))))
  2104. ;;;
  2105. ;;; Linking other sections of the ELF file, like the dynamic segment,
  2106. ;;; the symbol table, etc.
  2107. ;;;
  2108. ;; FIXME: Define these somewhere central, shared with C.
  2109. (define *bytecode-major-version* #x0300)
  2110. (define *bytecode-minor-version* 6)
  2111. (define (link-dynamic-section asm text rw rw-init frame-maps)
  2112. "Link the dynamic section for an ELF image with bytecode @var{text},
  2113. given the writable data section @var{rw} needing fixup from the
  2114. procedure with label @var{rw-init}. @var{rw-init} may be false. If
  2115. @var{rw} is true, it will be added to the GC roots at runtime."
  2116. (define-syntax-rule (emit-dynamic-section word-size %set-uword! reloc-type)
  2117. (let* ((endianness (asm-endianness asm))
  2118. (words 6)
  2119. (words (if rw (+ words 4) words))
  2120. (words (if rw-init (+ words 2) words))
  2121. (words (if frame-maps (+ words 2) words))
  2122. (size (* word-size words)))
  2123. (define relocs
  2124. ;; This must match the 'set-label!' calls below.
  2125. (let ((reloc (lambda (i label)
  2126. (make-linker-reloc 'reloc-type
  2127. (* i word-size) 0 label))))
  2128. `(,(reloc 3 '.rtl-text)
  2129. ,@(if rw
  2130. (list (reloc 5 '.data))
  2131. '())
  2132. ,@(if (and rw rw-init)
  2133. (list (reloc 9 rw-init))
  2134. '())
  2135. ,@(if frame-maps
  2136. (list (reloc (- words 3) '.guile.frame-maps))
  2137. '()))))
  2138. (define (write! bv)
  2139. (define (set-uword! i uword)
  2140. (%set-uword! bv (* i word-size) uword endianness))
  2141. (define (set-label! i label)
  2142. (%set-uword! bv (* i word-size) 0 endianness))
  2143. (set-uword! 0 DT_GUILE_VM_VERSION)
  2144. (set-uword! 1 (logior (ash *bytecode-major-version* 16)
  2145. *bytecode-minor-version*))
  2146. (set-uword! 2 DT_GUILE_ENTRY)
  2147. (set-label! 3 '.rtl-text)
  2148. (when rw
  2149. ;; Add roots to GC.
  2150. (set-uword! 4 DT_GUILE_GC_ROOT)
  2151. (set-label! 5 '.data)
  2152. (set-uword! 6 DT_GUILE_GC_ROOT_SZ)
  2153. (set-uword! 7 (linker-object-size rw))
  2154. (when rw-init
  2155. (set-uword! 8 DT_INIT) ; constants
  2156. (set-label! 9 rw-init)))
  2157. (when frame-maps
  2158. (set-uword! (- words 4) DT_GUILE_FRAME_MAPS)
  2159. (set-label! (- words 3) '.guile.frame-maps))
  2160. (set-uword! (- words 2) DT_NULL)
  2161. (set-uword! (- words 1) 0))
  2162. (make-object asm '.dynamic size write!
  2163. relocs '()
  2164. #:type SHT_DYNAMIC #:flags SHF_ALLOC)))
  2165. (case (asm-word-size asm)
  2166. ((4) (emit-dynamic-section 4 bytevector-u32-set! abs32/1))
  2167. ((8) (emit-dynamic-section 8 bytevector-u64-set! abs64/1))
  2168. (else (error "bad word size" asm))))
  2169. (define (link-shstrtab asm)
  2170. "Link the string table for the section headers."
  2171. (intern-section-name! asm ".shstrtab")
  2172. (make-object asm '.shstrtab
  2173. (string-table-size (asm-shstrtab asm))
  2174. (string-table-writer (asm-shstrtab asm))
  2175. '() '()
  2176. #:type SHT_STRTAB #:flags 0))
  2177. (define (link-symtab text-section asm)
  2178. (let* ((endianness (asm-endianness asm))
  2179. (word-size (asm-word-size asm))
  2180. (size (elf-symbol-len word-size))
  2181. (meta (reverse (asm-meta asm)))
  2182. (n (length meta))
  2183. (strtab (make-string-table)))
  2184. (define (intern-string! name)
  2185. (string-table-intern! strtab (if name (symbol->string name) "")))
  2186. (define names
  2187. (map (lambda (meta n)
  2188. (intern-string! (meta-name meta)))
  2189. meta (iota n)))
  2190. (define (write-symbols! bv)
  2191. (for-each (lambda (name meta n)
  2192. (write-elf-symbol bv (* n size)
  2193. endianness word-size
  2194. (make-elf-symbol
  2195. #:name name
  2196. ;; Symbol value and size are measured in
  2197. ;; bytes, not u32s.
  2198. #:value (meta-low-pc meta)
  2199. #:size (- (meta-high-pc meta)
  2200. (meta-low-pc meta))
  2201. #:type STT_FUNC
  2202. #:visibility STV_HIDDEN
  2203. #:shndx (elf-section-index
  2204. text-section))))
  2205. names meta (iota n)))
  2206. (let ((strtab (make-object asm '.strtab
  2207. (string-table-size strtab)
  2208. (string-table-writer strtab)
  2209. '() '()
  2210. #:type SHT_STRTAB #:flags 0)))
  2211. (values (make-object asm '.symtab
  2212. (* n size) write-symbols!
  2213. '() '()
  2214. #:type SHT_SYMTAB #:flags 0 #:entsize size
  2215. #:link (elf-section-index
  2216. (linker-object-section strtab)))
  2217. strtab))))
  2218. ;;; The .guile.arities section describes the arities that a function can
  2219. ;;; have. It is in two parts: a sorted array of headers describing
  2220. ;;; basic arities, and an array of links out to a string table (and in
  2221. ;;; the case of keyword arguments, to the data section) for argument
  2222. ;;; names. The whole thing is prefixed by a uint32 indicating the
  2223. ;;; offset of the end of the headers array.
  2224. ;;;
  2225. ;;; The arity headers array is a packed array of structures of the form:
  2226. ;;;
  2227. ;;; struct arity_header {
  2228. ;;; uint32_t low_pc;
  2229. ;;; uint32_t high_pc;
  2230. ;;; uint32_t offset;
  2231. ;;; uint32_t flags;
  2232. ;;; uint32_t nreq;
  2233. ;;; uint32_t nopt;
  2234. ;;; uint32_t nlocals;
  2235. ;;; }
  2236. ;;;
  2237. ;;; All of the offsets and addresses are 32 bits. We can expand in the
  2238. ;;; future to use 64-bit offsets if appropriate, but there are other
  2239. ;;; aspects of bytecode that constrain us to a total image that fits in
  2240. ;;; 32 bits, so for the moment we'll simplify the problem space.
  2241. ;;;
  2242. ;;; The following flags values are defined:
  2243. ;;;
  2244. ;;; #x1: has-rest?
  2245. ;;; #x2: allow-other-keys?
  2246. ;;; #x4: has-keyword-args?
  2247. ;;; #x8: is-case-lambda?
  2248. ;;; #x10: is-in-case-lambda?
  2249. ;;; #x20: elided-closure?
  2250. ;;;
  2251. ;;; Functions with a single arity specify their number of required and
  2252. ;;; optional arguments in nreq and nopt, and do not have the
  2253. ;;; is-case-lambda? flag set. Their "offset" member links to an array
  2254. ;;; of pointers into the associated .guile.arities.strtab string table,
  2255. ;;; identifying the argument names. This offset is relative to the
  2256. ;;; start of the .guile.arities section.
  2257. ;;;
  2258. ;;; If the arity has keyword arguments -- if has-keyword-args? is set in
  2259. ;;; the flags -- the first uint32 pointed to by offset encodes a link to
  2260. ;;; the "keyword indices" literal, in the data section. Then follow the
  2261. ;;; names for all locals, in order, as uleb128 values. The required
  2262. ;;; arguments will be the first locals, followed by the optionals,
  2263. ;;; followed by the rest argument if if has-rest? is set. The names
  2264. ;;; point into the associated string table section.
  2265. ;;;
  2266. ;;; Functions with no arities have no arities information present in the
  2267. ;;; .guile.arities section.
  2268. ;;;
  2269. ;;; Functions with multiple arities are preceded by a header with
  2270. ;;; is-case-lambda? set. All other fields are 0, except low-pc and
  2271. ;;; high-pc which should be the bounds of the whole function. Headers
  2272. ;;; for the individual arities follow, with the is-in-case-lambda? flag
  2273. ;;; set. In this way the whole headers array is sorted in increasing
  2274. ;;; low-pc order, and case-lambda clauses are contained within the
  2275. ;;; [low-pc, high-pc] of the case-lambda header.
  2276. ;;;
  2277. ;;; Normally the 0th argument is the closure for the function being
  2278. ;;; called. However if the function is "well-known" -- all of its call
  2279. ;;; sites are visible -- then the compiler may elide the closure, and
  2280. ;;; the 0th argument is the first user-visible argument.
  2281. ;; Length of the prefix to the arities section, in bytes.
  2282. (define arities-prefix-len 4)
  2283. ;; Length of an arity header, in bytes.
  2284. (define arity-header-len (* 7 4))
  2285. ;; Some helpers.
  2286. (define (put-uleb128 port val)
  2287. (let lp ((val val))
  2288. (let ((next (ash val -7)))
  2289. (if (zero? next)
  2290. (put-u8 port val)
  2291. (begin
  2292. (put-u8 port (logior #x80 (logand val #x7f)))
  2293. (lp next))))))
  2294. (define (put-sleb128 port val)
  2295. (let lp ((val val))
  2296. (if (<= 0 (+ val 64) 127)
  2297. (put-u8 port (logand val #x7f))
  2298. (begin
  2299. (put-u8 port (logior #x80 (logand val #x7f)))
  2300. (lp (ash val -7))))))
  2301. (define (port-position port)
  2302. (seek port 0 SEEK_CUR))
  2303. (define-inline (pack-arity-flags has-rest? allow-other-keys?
  2304. has-keyword-args? is-case-lambda?
  2305. is-in-case-lambda? elided-closure?)
  2306. (logior (if has-rest? (ash 1 0) 0)
  2307. (if allow-other-keys? (ash 1 1) 0)
  2308. (if has-keyword-args? (ash 1 2) 0)
  2309. (if is-case-lambda? (ash 1 3) 0)
  2310. (if is-in-case-lambda? (ash 1 4) 0)
  2311. (if elided-closure? (ash 1 5) 0)))
  2312. (define (write-arities asm metas headers names-port strtab)
  2313. (define (write-header pos low-pc high-pc offset flags nreq nopt nlocals)
  2314. (unless (<= (+ nreq nopt) nlocals)
  2315. (error "forgot to emit definition instructions?"))
  2316. (bytevector-u32-set! headers pos low-pc (asm-endianness asm))
  2317. (bytevector-u32-set! headers (+ pos 4) high-pc (asm-endianness asm))
  2318. (bytevector-u32-set! headers (+ pos 8) offset (asm-endianness asm))
  2319. (bytevector-u32-set! headers (+ pos 12) flags (asm-endianness asm))
  2320. (bytevector-u32-set! headers (+ pos 16) nreq (asm-endianness asm))
  2321. (bytevector-u32-set! headers (+ pos 20) nopt (asm-endianness asm))
  2322. (bytevector-u32-set! headers (+ pos 24) nlocals (asm-endianness asm)))
  2323. (define (write-kw-indices kw-indices relocs)
  2324. ;; FIXME: Assert that kw-indices is already interned.
  2325. (if (pair? kw-indices)
  2326. (let ((pos (+ (bytevector-length headers)
  2327. (port-position names-port)))
  2328. (label (intern-constant asm kw-indices)))
  2329. (put-bytevector names-port #vu8(0 0 0 0))
  2330. (cons (make-linker-reloc 'abs32/1 pos 0 label) relocs))
  2331. relocs))
  2332. (define (write-arity pos arity in-case-lambda? relocs)
  2333. (write-header pos (arity-low-pc arity)
  2334. (arity-high-pc arity)
  2335. ;; FIXME: Seems silly to add on bytevector-length of
  2336. ;; headers, given the arities-prefix.
  2337. (+ (bytevector-length headers) (port-position names-port))
  2338. (pack-arity-flags (arity-rest arity)
  2339. (arity-allow-other-keys? arity)
  2340. (pair? (arity-kw-indices arity))
  2341. #f
  2342. in-case-lambda?
  2343. (not (arity-has-closure? arity)))
  2344. (length (arity-req arity))
  2345. (length (arity-opt arity))
  2346. (length (arity-definitions arity)))
  2347. (let ((relocs (write-kw-indices (arity-kw-indices arity) relocs)))
  2348. ;; Write local names.
  2349. (let lp ((definitions (arity-definitions arity)))
  2350. (match definitions
  2351. (() relocs)
  2352. ((#(name slot representation def) . definitions)
  2353. (let ((sym (if (symbol? name)
  2354. (string-table-intern! strtab (symbol->string name))
  2355. 0)))
  2356. (put-uleb128 names-port sym)
  2357. (lp definitions)))))
  2358. ;; Now write their definitions.
  2359. (let lp ((definitions (arity-definitions arity)))
  2360. (match definitions
  2361. (() relocs)
  2362. ((#(name slot representation def) . definitions)
  2363. (put-uleb128 names-port def)
  2364. (let ((tag (case representation
  2365. ((scm) 0)
  2366. ((f64) 1)
  2367. ((u64) 2)
  2368. ((s64) 3)
  2369. ((ptr code) 4)
  2370. (else (error "what!" representation)))))
  2371. (put-uleb128 names-port (logior (ash slot 3) tag)))
  2372. (lp definitions))))))
  2373. (let lp ((metas metas) (pos arities-prefix-len) (relocs '()))
  2374. (match metas
  2375. (()
  2376. (unless (= pos (bytevector-length headers))
  2377. (error "expected to fully fill the bytevector"
  2378. pos (bytevector-length headers)))
  2379. relocs)
  2380. ((meta . metas)
  2381. (match (meta-arities meta)
  2382. (() (lp metas pos relocs))
  2383. ((arity)
  2384. (lp metas
  2385. (+ pos arity-header-len)
  2386. (write-arity pos arity #f relocs)))
  2387. (arities
  2388. ;; Write a case-lambda header, then individual arities.
  2389. ;; The case-lambda header's offset link is 0.
  2390. (write-header pos (meta-low-pc meta) (meta-high-pc meta) 0
  2391. (pack-arity-flags #f #f #f #t #f #f) 0 0 0)
  2392. (let lp* ((arities arities) (pos (+ pos arity-header-len))
  2393. (relocs relocs))
  2394. (match arities
  2395. (() (lp metas pos relocs))
  2396. ((arity . arities)
  2397. (lp* arities
  2398. (+ pos arity-header-len)
  2399. (write-arity pos arity #t relocs)))))))))))
  2400. (define (link-arities asm)
  2401. (define (meta-arities-header-size meta)
  2402. (define (lambda-size arity)
  2403. arity-header-len)
  2404. (define (case-lambda-size arities)
  2405. (fold +
  2406. arity-header-len ;; case-lambda header
  2407. (map lambda-size arities))) ;; the cases
  2408. (match (meta-arities meta)
  2409. (() 0)
  2410. ((arity) (lambda-size arity))
  2411. (arities (case-lambda-size arities))))
  2412. (let* ((endianness (asm-endianness asm))
  2413. (metas (reverse (asm-meta asm)))
  2414. (header-size (fold (lambda (meta size)
  2415. (+ size (meta-arities-header-size meta)))
  2416. arities-prefix-len
  2417. metas))
  2418. (strtab (make-string-table))
  2419. (headers (make-bytevector header-size 0)))
  2420. (bytevector-u32-set! headers 0 (bytevector-length headers) endianness)
  2421. (let-values (((names-port get-name-bv) (open-bytevector-output-port)))
  2422. (let* ((relocs (write-arities asm metas headers names-port strtab))
  2423. (name-bv (get-name-bv))
  2424. (strtab (make-object asm '.guile.arities.strtab
  2425. (string-table-size strtab)
  2426. (string-table-writer strtab)
  2427. '() '()
  2428. #:type SHT_STRTAB #:flags 0)))
  2429. (values (make-object asm '.guile.arities
  2430. (+ header-size (bytevector-length name-bv))
  2431. (lambda (bv)
  2432. ;; FIXME: Avoid extra allocation + copy.
  2433. (bytevector-copy! headers 0 bv 0
  2434. header-size)
  2435. (bytevector-copy! name-bv 0 bv header-size
  2436. (bytevector-length name-bv)))
  2437. relocs '()
  2438. #:type SHT_PROGBITS #:flags 0
  2439. #:link (elf-section-index
  2440. (linker-object-section strtab)))
  2441. strtab)))))
  2442. ;;;
  2443. ;;; The .guile.docstrs section is a packed, sorted array of (pc, str)
  2444. ;;; values. Pc and str are both 32 bits wide. (Either could change to
  2445. ;;; 64 bits if appropriate in the future.) Pc is the address of the
  2446. ;;; entry to a program, relative to the start of the text section, in
  2447. ;;; bytes, and str is an index into the associated .guile.docstrs.strtab
  2448. ;;; string table section.
  2449. ;;;
  2450. ;; The size of a docstrs entry, in bytes.
  2451. (define docstr-size 8)
  2452. (define (link-docstrs asm)
  2453. (define (find-docstrings)
  2454. (filter-map (lambda (meta)
  2455. (define (is-documentation? pair)
  2456. (eq? (car pair) 'documentation))
  2457. (let* ((props (meta-properties meta))
  2458. (tail (find-tail is-documentation? props)))
  2459. (and tail
  2460. (not (find-tail is-documentation? (cdr tail)))
  2461. (string? (cdar tail))
  2462. (cons (meta-low-pc meta) (cdar tail)))))
  2463. (reverse (asm-meta asm))))
  2464. (let* ((endianness (asm-endianness asm))
  2465. (strtab (make-string-table))
  2466. (docstrings (map (match-lambda
  2467. ((pc . str)
  2468. (cons pc (string-table-intern! strtab str))))
  2469. (find-docstrings))))
  2470. (define (write-docstrings! bv)
  2471. (fold (lambda (pair pos)
  2472. (match pair
  2473. ((pc . string-pos)
  2474. (bytevector-u32-set! bv pos pc endianness)
  2475. (bytevector-u32-set! bv (+ pos 4)
  2476. string-pos
  2477. endianness)
  2478. (+ pos docstr-size))))
  2479. 0
  2480. docstrings))
  2481. (let ((strtab (make-object asm '.guile.docstrs.strtab
  2482. (string-table-size strtab)
  2483. (string-table-writer strtab)
  2484. '() '()
  2485. #:type SHT_STRTAB #:flags 0)))
  2486. (values (make-object asm '.guile.docstrs
  2487. (* (length docstrings) docstr-size)
  2488. write-docstrings!
  2489. '() '()
  2490. #:type SHT_PROGBITS #:flags 0
  2491. #:link (elf-section-index
  2492. (linker-object-section strtab)))
  2493. strtab))))
  2494. ;;;
  2495. ;;; The .guile.procprops section is a packed, sorted array of (pc, addr)
  2496. ;;; values. Pc and addr are both 32 bits wide. (Either could change to
  2497. ;;; 64 bits if appropriate in the future.) Pc is the address of the
  2498. ;;; entry to a program, relative to the start of the text section, and
  2499. ;;; addr is the address of the associated properties alist, relative to
  2500. ;;; the start of the ELF image.
  2501. ;;;
  2502. ;;; Since procedure properties are stored in the data sections, we need
  2503. ;;; to link the procedures property section first. (Note that this
  2504. ;;; constraint does not apply to the arities section, which may
  2505. ;;; reference the data sections via the kw-indices literal, because
  2506. ;;; assembling the text section already makes sure that the kw-indices
  2507. ;;; are interned.)
  2508. ;;;
  2509. ;; The size of a procprops entry, in bytes.
  2510. (define procprops-size 8)
  2511. (define (link-procprops asm)
  2512. (define (assoc-remove-one alist key value-pred)
  2513. (match alist
  2514. (() '())
  2515. ((((? (lambda (x) (eq? x key))) . value) . alist)
  2516. (if (value-pred value)
  2517. alist
  2518. (acons key value alist)))
  2519. (((k . v) . alist)
  2520. (acons k v (assoc-remove-one alist key value-pred)))))
  2521. (define (props-without-name-or-docstring meta)
  2522. (assoc-remove-one
  2523. (assoc-remove-one (meta-properties meta) 'name (lambda (x) #t))
  2524. 'documentation
  2525. string?))
  2526. (define (find-procprops)
  2527. (filter-map (lambda (meta)
  2528. (let ((props (props-without-name-or-docstring meta)))
  2529. (and (pair? props)
  2530. (cons (meta-low-pc meta) props))))
  2531. (reverse (asm-meta asm))))
  2532. (let* ((endianness (asm-endianness asm))
  2533. (procprops (find-procprops))
  2534. (size (* (length procprops) procprops-size)))
  2535. (define (write-procprops! bv)
  2536. (let lp ((procprops procprops) (pos 0))
  2537. (match procprops
  2538. (()
  2539. #t)
  2540. (((pc . props) . procprops)
  2541. (bytevector-u32-set! bv pos pc endianness)
  2542. (lp procprops (+ pos procprops-size))))))
  2543. (define relocs
  2544. (let lp ((procprops procprops) (pos 0) (relocs '()))
  2545. (match procprops
  2546. (()
  2547. relocs)
  2548. (((pc . props) . procprops)
  2549. (lp procprops
  2550. (+ pos procprops-size)
  2551. (cons (make-linker-reloc 'abs32/1 (+ pos 4) 0
  2552. (intern-constant asm props))
  2553. relocs))))))
  2554. (make-object asm '.guile.procprops
  2555. size write-procprops!
  2556. relocs '()
  2557. #:type SHT_PROGBITS #:flags 0)))
  2558. ;;;
  2559. ;;; The DWARF .debug_info, .debug_abbrev, .debug_str, and .debug_loc
  2560. ;;; sections provide line number and local variable liveness
  2561. ;;; information. Their format is defined by the DWARF
  2562. ;;; specifications.
  2563. ;;;
  2564. (define (asm-language asm)
  2565. ;; FIXME: Plumb language through to the assembler.
  2566. 'scheme)
  2567. ;; -> 5 values: .debug_info, .debug_abbrev, .debug_str, .debug_loc, .debug_lines
  2568. (define (link-debug asm)
  2569. (define (put-s8 port val)
  2570. (let ((bv (make-bytevector 1)))
  2571. (bytevector-s8-set! bv 0 val)
  2572. (put-bytevector port bv)))
  2573. (define (put-u16 port val)
  2574. (let ((bv (make-bytevector 2)))
  2575. (bytevector-u16-set! bv 0 val (asm-endianness asm))
  2576. (put-bytevector port bv)))
  2577. (define (put-u32 port val)
  2578. (let ((bv (make-bytevector 4)))
  2579. (bytevector-u32-set! bv 0 val (asm-endianness asm))
  2580. (put-bytevector port bv)))
  2581. (define (put-u64 port val)
  2582. (let ((bv (make-bytevector 8)))
  2583. (bytevector-u64-set! bv 0 val (asm-endianness asm))
  2584. (put-bytevector port bv)))
  2585. (define (meta->subprogram-die meta)
  2586. `(subprogram
  2587. (@ ,@(cond
  2588. ((meta-name meta)
  2589. => (lambda (name) `((name ,(symbol->string name)))))
  2590. (else
  2591. '()))
  2592. (low-pc ,(meta-label meta))
  2593. (high-pc ,(- (meta-high-pc meta) (meta-low-pc meta))))))
  2594. (define (make-compile-unit-die asm)
  2595. `(compile-unit
  2596. (@ (producer ,(string-append "Guile " (version)))
  2597. (language ,(asm-language asm))
  2598. (low-pc .rtl-text)
  2599. (high-pc ,(asm-pos asm))
  2600. (stmt-list 0))
  2601. ,@(map meta->subprogram-die (reverse (asm-meta asm)))))
  2602. (let-values (((die-port get-die-bv) (open-bytevector-output-port))
  2603. ((die-relocs) '())
  2604. ((abbrev-port get-abbrev-bv) (open-bytevector-output-port))
  2605. ;; (tag has-kids? attrs forms) -> code
  2606. ((abbrevs) vlist-null)
  2607. ((strtab) (make-string-table))
  2608. ((line-port get-line-bv) (open-bytevector-output-port))
  2609. ((line-relocs) '())
  2610. ;; file -> code
  2611. ((files) vlist-null))
  2612. (define (write-abbrev code tag has-children? attrs forms)
  2613. (put-uleb128 abbrev-port code)
  2614. (put-uleb128 abbrev-port (tag-name->code tag))
  2615. (put-u8 abbrev-port (children-name->code (if has-children? 'yes 'no)))
  2616. (for-each (lambda (attr form)
  2617. (put-uleb128 abbrev-port (attribute-name->code attr))
  2618. (put-uleb128 abbrev-port (form-name->code form)))
  2619. attrs forms)
  2620. (put-uleb128 abbrev-port 0)
  2621. (put-uleb128 abbrev-port 0))
  2622. (define (intern-abbrev tag has-children? attrs forms)
  2623. (let ((key (list tag has-children? attrs forms)))
  2624. (match (vhash-assoc key abbrevs)
  2625. ((_ . code) code)
  2626. (#f (let ((code (1+ (vlist-length abbrevs))))
  2627. (set! abbrevs (vhash-cons key code abbrevs))
  2628. (write-abbrev code tag has-children? attrs forms)
  2629. code)))))
  2630. (define (intern-file file)
  2631. (match (vhash-assoc file files)
  2632. ((_ . code) code)
  2633. (#f (let ((code (1+ (vlist-length files))))
  2634. (set! files (vhash-cons file code files))
  2635. code))))
  2636. (define (write-sources)
  2637. ;; Choose line base and line range values that will allow for an
  2638. ;; address advance range of 16 words. The special opcode range is
  2639. ;; from 10 to 255, so 246 values.
  2640. (define base -4)
  2641. (define range 15)
  2642. (define min-inc 4) ; Minimum PC increment.
  2643. (let lp ((sources (asm-sources asm)) (out '()))
  2644. (match sources
  2645. (((pc . location) . sources)
  2646. (let-values (((file line col)
  2647. ;; Usually CPS records contain a "source
  2648. ;; vector" coming from tree-il, but some might
  2649. ;; contain a source property alist.
  2650. (match location
  2651. (#(file line col) (values file line col))
  2652. (lst (values (assq-ref lst 'filename)
  2653. (assq-ref lst 'line)
  2654. (assq-ref lst 'column))))))
  2655. (lp sources
  2656. ;; Guile line and column numbers are 0-indexed, but
  2657. ;; they are 1-indexed for DWARF.
  2658. (if (and line col)
  2659. (cons (list pc
  2660. (if (string? file) (intern-file file) 0)
  2661. (1+ line)
  2662. (1+ col))
  2663. out)
  2664. out))))
  2665. (()
  2666. ;; Compilation unit header for .debug_line. We write in
  2667. ;; DWARF 2 format because more tools understand it than DWARF
  2668. ;; 4, which incompatibly adds another field to this header.
  2669. (put-u32 line-port 0) ; Length; will patch later.
  2670. (put-u16 line-port 2) ; DWARF 2 format.
  2671. (put-u32 line-port 0) ; Prologue length; will patch later.
  2672. (put-u8 line-port min-inc) ; Minimum instruction length: 4 bytes.
  2673. (put-u8 line-port 1) ; Default is-stmt: true.
  2674. (put-s8 line-port base) ; Line base. See the DWARF standard.
  2675. (put-u8 line-port range) ; Line range. See the DWARF standard.
  2676. (put-u8 line-port 10) ; Opcode base: the first "special" opcode.
  2677. ;; A table of the number of uleb128 arguments taken by each
  2678. ;; of the standard opcodes.
  2679. (put-u8 line-port 0) ; 1: copy
  2680. (put-u8 line-port 1) ; 2: advance-pc
  2681. (put-u8 line-port 1) ; 3: advance-line
  2682. (put-u8 line-port 1) ; 4: set-file
  2683. (put-u8 line-port 1) ; 5: set-column
  2684. (put-u8 line-port 0) ; 6: negate-stmt
  2685. (put-u8 line-port 0) ; 7: set-basic-block
  2686. (put-u8 line-port 0) ; 8: const-add-pc
  2687. (put-u8 line-port 1) ; 9: fixed-advance-pc
  2688. ;; Include directories, as a zero-terminated sequence of
  2689. ;; nul-terminated strings. Nothing, for the moment.
  2690. (put-u8 line-port 0)
  2691. ;; File table. For each file that contributes to this
  2692. ;; compilation unit, a nul-terminated file name string, and a
  2693. ;; uleb128 for each of directory the file was found in, the
  2694. ;; modification time, and the file's size in bytes. We pass
  2695. ;; zero for the latter three fields.
  2696. (vlist-fold-right
  2697. (lambda (pair seed)
  2698. (match pair
  2699. ((file . code)
  2700. (put-bytevector line-port (string->utf8 file))
  2701. (put-u8 line-port 0)
  2702. (put-uleb128 line-port 0) ; directory
  2703. (put-uleb128 line-port 0) ; mtime
  2704. (put-uleb128 line-port 0))) ; size
  2705. seed)
  2706. #f
  2707. files)
  2708. (put-u8 line-port 0) ; 0 byte terminating file list.
  2709. ;; Patch prologue length.
  2710. (let ((offset (port-position line-port)))
  2711. (seek line-port 6 SEEK_SET)
  2712. (put-u32 line-port (- offset 10))
  2713. (seek line-port offset SEEK_SET))
  2714. ;; Now write the statement program.
  2715. (let ()
  2716. (define (extended-op opcode payload-len)
  2717. (put-u8 line-port 0) ; extended op
  2718. (put-uleb128 line-port (1+ payload-len)) ; payload-len + opcode
  2719. (put-uleb128 line-port opcode))
  2720. (define (set-address sym)
  2721. (define (add-reloc! kind)
  2722. (set! line-relocs
  2723. (cons (make-linker-reloc kind
  2724. (port-position line-port)
  2725. 0
  2726. sym)
  2727. line-relocs)))
  2728. (match (asm-word-size asm)
  2729. (4
  2730. (extended-op 2 4)
  2731. (add-reloc! 'abs32/1)
  2732. (put-u32 line-port 0))
  2733. (8
  2734. (extended-op 2 8)
  2735. (add-reloc! 'abs64/1)
  2736. (put-u64 line-port 0))))
  2737. (define (end-sequence pc)
  2738. (let ((pc-inc (/ (- (asm-pos asm) pc) min-inc)))
  2739. (put-u8 line-port 2) ; advance-pc
  2740. (put-uleb128 line-port pc-inc))
  2741. (extended-op 1 0))
  2742. (define (advance-pc pc-inc line-inc)
  2743. (let ((spec (+ (- line-inc base)
  2744. (* (/ pc-inc min-inc) range)
  2745. 10)))
  2746. (cond
  2747. ((or (< line-inc base) (>= line-inc (+ base range)))
  2748. (advance-line line-inc)
  2749. (advance-pc pc-inc 0))
  2750. ((<= spec 255)
  2751. (put-u8 line-port spec))
  2752. ((< spec 500)
  2753. (put-u8 line-port 8) ; const-advance-pc
  2754. (advance-pc (- pc-inc (* (floor/ (- 255 10) range) min-inc))
  2755. line-inc))
  2756. (else
  2757. (put-u8 line-port 2) ; advance-pc
  2758. (put-uleb128 line-port (/ pc-inc min-inc))
  2759. (advance-pc 0 line-inc)))))
  2760. (define (advance-line inc)
  2761. (put-u8 line-port 3)
  2762. (put-sleb128 line-port inc))
  2763. (define (set-file file)
  2764. (put-u8 line-port 4)
  2765. (put-uleb128 line-port file))
  2766. (define (set-column col)
  2767. (put-u8 line-port 5)
  2768. (put-uleb128 line-port col))
  2769. (set-address '.rtl-text)
  2770. (let lp ((in out) (pc 0) (file 1) (line 1) (col 0))
  2771. (match in
  2772. (()
  2773. (when (null? out)
  2774. ;; There was no source info in the first place. Set
  2775. ;; file register to 0 before adding final row.
  2776. (set-file 0))
  2777. (end-sequence pc))
  2778. (((pc* file* line* col*) . in*)
  2779. (cond
  2780. ((and (eqv? file file*) (eqv? line line*) (eqv? col col*))
  2781. (lp in* pc file line col))
  2782. (else
  2783. (unless (eqv? col col*)
  2784. (set-column col*))
  2785. (unless (eqv? file file*)
  2786. (set-file file*))
  2787. (advance-pc (- pc* pc) (- line* line))
  2788. (lp in* pc* file* line* col*)))))))))))
  2789. (define (compute-code attr val)
  2790. (match attr
  2791. ('name (string-table-intern! strtab val))
  2792. ('low-pc val)
  2793. ('high-pc val)
  2794. ('producer (string-table-intern! strtab val))
  2795. ('language (language-name->code val))
  2796. ('stmt-list val)))
  2797. (define (choose-form attr val code)
  2798. (cond
  2799. ((string? val) 'strp)
  2800. ((eq? attr 'stmt-list) 'sec-offset)
  2801. ((eq? attr 'low-pc) 'addr)
  2802. ((exact-integer? code)
  2803. (cond
  2804. ((< code 0) 'sleb128)
  2805. ((<= code #xff) 'data1)
  2806. ((<= code #xffff) 'data2)
  2807. ((<= code #xffffffff) 'data4)
  2808. ((<= code #xffffffffffffffff) 'data8)
  2809. (else 'uleb128)))
  2810. (else (error "unhandled case" attr val code))))
  2811. (define (add-die-relocation! kind sym)
  2812. (set! die-relocs
  2813. (cons (make-linker-reloc kind (port-position die-port) 0 sym)
  2814. die-relocs)))
  2815. (define (write-value code form)
  2816. (match form
  2817. ('data1 (put-u8 die-port code))
  2818. ('data2 (put-u16 die-port code))
  2819. ('data4 (put-u32 die-port code))
  2820. ('data8 (put-u64 die-port code))
  2821. ('uleb128 (put-uleb128 die-port code))
  2822. ('sleb128 (put-sleb128 die-port code))
  2823. ('addr
  2824. (match (asm-word-size asm)
  2825. (4
  2826. (add-die-relocation! 'abs32/1 code)
  2827. (put-u32 die-port 0))
  2828. (8
  2829. (add-die-relocation! 'abs64/1 code)
  2830. (put-u64 die-port 0))))
  2831. ('sec-offset (put-u32 die-port code))
  2832. ('strp (put-u32 die-port code))))
  2833. (define (write-die die)
  2834. (match die
  2835. ((tag ('@ (attrs vals) ...) children ...)
  2836. (let* ((codes (map compute-code attrs vals))
  2837. (forms (map choose-form attrs vals codes))
  2838. (has-children? (not (null? children)))
  2839. (abbrev-code (intern-abbrev tag has-children? attrs forms)))
  2840. (put-uleb128 die-port abbrev-code)
  2841. (for-each write-value codes forms)
  2842. (when has-children?
  2843. (for-each write-die children)
  2844. (put-uleb128 die-port 0))))))
  2845. (define (copy-writer source)
  2846. (lambda (bv)
  2847. (bytevector-copy! source 0 bv 0
  2848. (bytevector-length source))))
  2849. ;; Compilation unit header.
  2850. (put-u32 die-port 0) ; Length; will patch later.
  2851. (put-u16 die-port 4) ; DWARF 4.
  2852. (put-u32 die-port 0) ; Abbrevs offset.
  2853. (put-u8 die-port (asm-word-size asm)) ; Address size.
  2854. (write-die (make-compile-unit-die asm))
  2855. ;; Terminate the abbrevs list.
  2856. (put-uleb128 abbrev-port 0)
  2857. (write-sources)
  2858. (values (let ((bv (get-die-bv)))
  2859. ;; Patch DWARF32 length.
  2860. (bytevector-u32-set! bv 0 (- (bytevector-length bv) 4)
  2861. (asm-endianness asm))
  2862. (make-object asm '.debug_info
  2863. (bytevector-length bv)
  2864. (copy-writer bv)
  2865. die-relocs '()
  2866. #:type SHT_PROGBITS #:flags 0))
  2867. (let ((bv (get-abbrev-bv)))
  2868. (make-object asm '.debug_abbrev
  2869. (bytevector-length bv) (copy-writer bv)
  2870. '() '()
  2871. #:type SHT_PROGBITS #:flags 0))
  2872. (make-object asm '.debug_str
  2873. (string-table-size strtab)
  2874. (string-table-writer strtab)
  2875. '() '()
  2876. #:type SHT_PROGBITS #:flags 0)
  2877. (make-object asm '.debug_loc
  2878. 0 (lambda (bv) #t)
  2879. '() '()
  2880. #:type SHT_PROGBITS #:flags 0)
  2881. (let ((bv (get-line-bv)))
  2882. ;; Patch DWARF32 length.
  2883. (bytevector-u32-set! bv 0 (- (bytevector-length bv) 4)
  2884. (asm-endianness asm))
  2885. (make-object asm '.debug_line
  2886. (bytevector-length bv) (copy-writer bv)
  2887. line-relocs '()
  2888. #:type SHT_PROGBITS #:flags 0)))))
  2889. (define (link-objects asm)
  2890. (let*-values (;; Link procprops before constants, because it probably
  2891. ;; interns more constants.
  2892. ((procprops) (link-procprops asm))
  2893. ((ro rw rw-init) (link-constants asm))
  2894. ;; Link text object after constants, so that the
  2895. ;; constants initializer gets included.
  2896. ((text) (link-text-object asm))
  2897. ((frame-maps) (link-frame-maps asm))
  2898. ((dt) (link-dynamic-section asm text rw rw-init frame-maps))
  2899. ((symtab strtab) (link-symtab (linker-object-section text) asm))
  2900. ((arities arities-strtab) (link-arities asm))
  2901. ((docstrs docstrs-strtab) (link-docstrs asm))
  2902. ((dinfo dabbrev dstrtab dloc dline) (link-debug asm))
  2903. ;; This needs to be linked last, because linking other
  2904. ;; sections adds entries to the string table.
  2905. ((shstrtab) (link-shstrtab asm)))
  2906. (filter identity
  2907. (list text ro frame-maps rw dt symtab strtab
  2908. arities arities-strtab
  2909. docstrs docstrs-strtab procprops
  2910. dinfo dabbrev dstrtab dloc dline
  2911. shstrtab))))
  2912. ;;;
  2913. ;;; High-level public interfaces.
  2914. ;;;
  2915. (define* (link-assembly asm #:key (page-aligned? #t))
  2916. "Produce an ELF image from the code and data emitted into @var{asm}.
  2917. The result is a bytevector, by default linked so that read-only and
  2918. writable data are on separate pages. Pass @code{#:page-aligned? #f} to
  2919. disable this behavior."
  2920. (link-elf (link-objects asm) #:page-aligned? page-aligned?))