assembler.scm 120 KB

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