assembler.scm 109 KB

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