aasmtai.pas 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640
  1. {
  2. Copyright (c) 1998-2006 by Florian Klaempfl
  3. This unit implements an abstract asmoutput class for all processor types
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. { @abstract(This unit implements an abstract asm output class for all processor types)
  18. This unit implements an abstract assembler output class for all processors, these
  19. are then overridden for each assembler writer to actually write the data in these
  20. classes to an assembler file.
  21. }
  22. unit aasmtai;
  23. {$i fpcdefs.inc}
  24. interface
  25. uses
  26. cutils,cclasses,
  27. globtype,systems,
  28. cpuinfo,cpubase,
  29. {$ifdef llvm}
  30. { overrides max_operands }
  31. llvmbase,
  32. {$endif llvm}
  33. cgbase,cgutils,
  34. symtype,
  35. aasmbase,aasmdata,ogbase
  36. {$ifdef jvm}
  37. ,widestr
  38. {$endif jvm}
  39. ;
  40. type
  41. { keep the number of elements in this enumeration less or equal than 32 as long
  42. as FPC knows only 4 byte and 32 byte sets (FK) }
  43. taitype = (
  44. ait_none,
  45. ait_align,
  46. ait_section,
  47. ait_comment,
  48. ait_string,
  49. ait_instruction,
  50. ait_datablock,
  51. ait_symbol,
  52. { needed to calc the size of a symbol }
  53. ait_symbol_end,
  54. ait_directive,
  55. ait_label,
  56. ait_const,
  57. ait_realconst,
  58. ait_typedconst,
  59. ait_stab,
  60. ait_force_line,
  61. ait_function_name,
  62. ait_symbolpair,
  63. { used to split into tiny assembler files }
  64. ait_cutobject,
  65. ait_regalloc,
  66. ait_tempalloc,
  67. { used to mark assembler blocks and inlined functions }
  68. ait_marker,
  69. { used to describe a new location of a variable }
  70. ait_varloc,
  71. {$ifdef JVM}
  72. { JVM only }
  73. ait_jvar, { debug information for a local variable }
  74. ait_jcatch, { exception catch clause }
  75. {$endif JVM}
  76. {$ifdef llvm}
  77. ait_llvmins, { llvm instruction }
  78. ait_llvmalias, { alias for a symbol }
  79. ait_llvmdecl, { llvm symbol declaration (global/external variable, external procdef) }
  80. ait_llvmmetadatanode, (* llvm metadata node: !id = !{type value, ...} *)
  81. ait_llvmmetadatareftypedconst, { reference to metadata inside a metadata constant }
  82. ait_llvmmetadatarefoperand, { llvm metadata referece: !metadataname !id }
  83. {$endif}
  84. {$ifdef wasm}
  85. ait_importexport,
  86. ait_local,
  87. ait_functype,
  88. ait_tagtype,
  89. ait_import_module,
  90. ait_import_name,
  91. {$endif}
  92. { SEH directives used in ARM,MIPS and x86_64 COFF targets }
  93. ait_seh_directive,
  94. { Dwarf CFI directive }
  95. ait_cfi,
  96. ait_eabi_attribute
  97. );
  98. taiconst_type = (
  99. aitconst_128bit,
  100. aitconst_64bit,
  101. aitconst_32bit,
  102. aitconst_16bit,
  103. aitconst_8bit,
  104. aitconst_sleb128bit,
  105. aitconst_uleb128bit,
  106. { win32 only }
  107. aitconst_rva_symbol,
  108. aitconst_secrel32_symbol,
  109. { darwin only }
  110. { From gcc/config/darwin.c (darwin_asm_output_dwarf_delta):
  111. ***
  112. Output a difference of two labels that will be an assembly time
  113. constant if the two labels are local. (.long lab1-lab2 will be
  114. very different if lab1 is at the boundary between two sections; it
  115. will be relocated according to the second section, not the first,
  116. so one ends up with a difference between labels in different
  117. sections, which is bad in the dwarf2 eh context for instance.)
  118. ***
  119. We cannot use this everywhere, because older versions of the
  120. darwin assembler don't support the construct used for these
  121. relsyms (nor do they support dwarf, for that matter)
  122. }
  123. aitconst_darwin_dwarf_delta64,
  124. aitconst_darwin_dwarf_delta32,
  125. { ARM Thumb-2 only }
  126. aitconst_half16bit, { used for table jumps. The actual value is the 16bit value shifted left once }
  127. { AVR }
  128. aitconst_gs, { Upper 16-bit of 17-bit constant }
  129. { for use by dwarf debugger information }
  130. aitconst_16bit_unaligned,
  131. aitconst_32bit_unaligned,
  132. aitconst_64bit_unaligned,
  133. { i8086 far pointer; emits: 'DW symbol, SEG symbol' }
  134. aitconst_farptr,
  135. { i8086 segment of symbol; emits: 'DW SEG symbol' }
  136. aitconst_seg,
  137. { i8086 data segment group; emits: 'DW dgroup'
  138. generated by the this inline asm:
  139. DW SEG @DATA
  140. in all memory models, except huge }
  141. aitconst_dgroup,
  142. { i8086 far data segment of the current pascal module (unit or program);
  143. emits: 'DW CURRENTMODULENAME_DATA'
  144. generated by the this inline asm:
  145. DW SEG @DATA
  146. in the huge memory model }
  147. aitconst_fardataseg,
  148. { offset of symbol's GOT slot in GOT }
  149. aitconst_got,
  150. { offset of symbol itself from GOT }
  151. aitconst_gotoff_symbol,
  152. { offset in TLS block }
  153. aitconst_dtpoff,
  154. { ARM TLS code }
  155. aitconst_gottpoff,
  156. aitconst_tpoff,
  157. aitconst_tlsgd,
  158. aitconst_tlsdesc
  159. );
  160. tairealconsttype = (
  161. aitrealconst_s32bit,
  162. aitrealconst_s64bit,
  163. aitrealconst_s80bit,
  164. aitrealconst_s128bit,
  165. aitrealconst_s64comp
  166. );
  167. const
  168. {$if defined(cpu64bitaddr)}
  169. aitconst_ptr = aitconst_64bit;
  170. aitconst_ptr_unaligned = aitconst_64bit_unaligned;
  171. aitconst_sizeint = aitconst_64bit;
  172. aitconst_sizeint_unaligned = aitconst_64bit_unaligned;
  173. {$elseif defined(cpu32bitaddr)}
  174. aitconst_ptr = aitconst_32bit;
  175. aitconst_ptr_unaligned = aitconst_32bit_unaligned;
  176. aitconst_sizeint = aitconst_32bit;
  177. aitconst_sizeint_unaligned = aitconst_32bit_unaligned;
  178. {$elseif defined(cpu16bitaddr)}
  179. aitconst_ptr = aitconst_16bit;
  180. aitconst_ptr_unaligned = aitconst_16bit_unaligned;
  181. aitconst_sizeint = aitconst_16bit;
  182. aitconst_sizeint_unaligned = aitconst_16bit_unaligned;
  183. {$endif}
  184. {$if defined(cpu64bitalu)}
  185. aitconst_aint = aitconst_64bit;
  186. {$elseif defined(cpu32bitalu)}
  187. aitconst_aint = aitconst_32bit;
  188. {$elseif defined(cpu16bitalu)}
  189. aitconst_aint = aitconst_16bit;
  190. {$elseif defined(cpu8bitalu)}
  191. aitconst_aint = aitconst_8bit;
  192. {$endif}
  193. taitypestr : array[taitype] of string[24] = (
  194. '<none>',
  195. 'align',
  196. 'section',
  197. 'comment',
  198. 'string',
  199. 'instruction',
  200. 'datablock',
  201. 'symbol',
  202. 'symbol_end',
  203. 'symbol_directive',
  204. 'label',
  205. 'const',
  206. 'realconst',
  207. 'typedconst',
  208. 'stab',
  209. 'force_line',
  210. 'function_name',
  211. 'symbolpair',
  212. 'cut',
  213. 'regalloc',
  214. 'tempalloc',
  215. 'marker',
  216. 'varloc',
  217. {$ifdef JVM}
  218. 'jvar',
  219. 'jcatch',
  220. {$endif JVM}
  221. {$ifdef llvm}
  222. 'llvmins',
  223. 'llvmalias',
  224. 'llvmdecl',
  225. 'llvmmetadata',
  226. 'llvmmetadatareftc',
  227. 'llvmmetadatarefop',
  228. {$endif}
  229. {$ifdef wasm}
  230. 'importexport',
  231. 'local',
  232. 'functype',
  233. 'tagtype',
  234. 'import_module',
  235. 'import_name',
  236. {$endif}
  237. 'cfi',
  238. 'seh_directive',
  239. 'eabi_attribute'
  240. );
  241. type
  242. { Types of operand }
  243. toptype=(top_none,top_reg,top_ref,top_const,top_bool,top_local
  244. {$ifdef arm}
  245. { ARM only }
  246. ,top_modeflags
  247. ,top_specialreg
  248. {$endif arm}
  249. {$if defined(arm) or defined(aarch64)}
  250. ,top_regset
  251. ,top_conditioncode
  252. ,top_shifterop
  253. ,top_realconst
  254. {$endif defined(arm) or defined(aarch64)}
  255. {$ifdef aarch64}
  256. ,top_indexedreg
  257. {$endif}
  258. {$ifdef m68k}
  259. { m68k only }
  260. ,top_regset
  261. ,top_regpair
  262. ,top_realconst
  263. {$endif m68k}
  264. {$ifdef jvm}
  265. { jvm only}
  266. ,top_single
  267. ,top_double
  268. ,top_string
  269. ,top_wstring
  270. {$endif jvm}
  271. {$ifdef llvm}
  272. { llvm only }
  273. ,top_single
  274. ,top_double
  275. ,top_undef
  276. {$ifdef cpuextended}
  277. ,top_extended80
  278. {$endif cpuextended}
  279. ,top_tai
  280. ,top_def
  281. ,top_fpcond
  282. ,top_cond
  283. ,top_para
  284. ,top_asmlist
  285. ,top_callingconvention
  286. {$endif llvm}
  287. {$if defined(riscv32) or defined(riscv64)}
  288. ,top_fenceflags
  289. ,top_roundingmode
  290. {$endif defined(riscv32) or defined(riscv64)}
  291. {$ifdef wasm}
  292. ,top_functype
  293. ,top_single
  294. ,top_double
  295. {$endif wasm}
  296. );
  297. { kinds of operations that an instruction can perform on an operand }
  298. topertype = (operand_read,operand_write,operand_readwrite);
  299. tlocaloper = record
  300. localsym : pointer;
  301. localsymderef : tderef;
  302. localsymofs : longint;
  303. {$ifdef x86}
  304. localsegment,
  305. {$endif x86}
  306. localindexreg : tregister;
  307. localscale : byte;
  308. localgetoffset,
  309. localforceref : boolean
  310. end;
  311. plocaloper = ^tlocaloper;
  312. const
  313. { ait_* types which don't result in executable code or which don't influence
  314. the way the program runs/behaves, but which may be encountered by the
  315. optimizer (= if it's sometimes added to the exprasm list). Update if you add
  316. a new ait type! }
  317. SkipInstr = [ait_comment, ait_symbol,ait_section
  318. ,ait_stab, ait_function_name, ait_force_line
  319. ,ait_regalloc, ait_tempalloc, ait_symbol_end
  320. ,ait_directive
  321. ,ait_varloc,
  322. {$ifdef JVM}
  323. ait_jvar,
  324. {$endif JVM}
  325. ait_seh_directive];
  326. { ait_* types which do not have line information (and hence which are of type
  327. tai, otherwise, they are of type tailineinfo }
  328. SkipLineInfo =[ait_label,
  329. ait_regalloc,ait_tempalloc,
  330. ait_stab,ait_function_name,
  331. ait_cutobject,ait_marker,ait_varloc,ait_align,ait_section,ait_comment,
  332. ait_const,ait_directive,
  333. ait_symbolpair,
  334. ait_realconst,
  335. ait_symbol,
  336. {$ifdef JVM}
  337. ait_jvar, ait_jcatch,
  338. {$endif JVM}
  339. {$ifdef llvm}
  340. ait_llvmdecl,
  341. ait_llvmmetadatanode,
  342. ait_llvmmetadatareftypedconst,
  343. ait_llvmmetadatarefoperand,
  344. {$endif llvm}
  345. {$ifdef wasm}
  346. ait_importexport,ait_local,ait_functype,ait_tagtype,ait_import_module,ait_import_name,
  347. {$endif wasm}
  348. ait_seh_directive,
  349. ait_cfi,
  350. ait_eabi_attribute
  351. ];
  352. type
  353. { cut type, required for alphanumeric ordering of the assembler filenames }
  354. TCutPlace=(cut_normal,cut_begin,cut_end);
  355. TAsmMarker = (
  356. mark_NoPropInfoStart,mark_NoPropInfoEnd,
  357. mark_AsmBlockStart,mark_AsmBlockEnd,
  358. mark_NoLineInfoStart,mark_NoLineInfoEnd,mark_BlockStart,
  359. mark_Position
  360. );
  361. TRegAllocType = (ra_alloc,ra_dealloc,ra_sync,ra_resize,ra_markused);
  362. TStabType = (stab_stabs,stab_stabn,stab_stabd,
  363. { AIX/XCOFF stab types }
  364. stab_stabx,
  365. { begin/end include file }
  366. stabx_bi,stabx_ei,
  367. { begin/end function }
  368. stabx_bf, stabx_ef,
  369. { begin/end static data block }
  370. stabx_bs, stabx_es,
  371. { line spec, function start/end label }
  372. stabx_line, stabx_function);
  373. TAsmDirective=(
  374. asd_indirect_symbol,
  375. asd_extern,asd_nasm_import, asd_toc_entry,
  376. asd_reference,asd_no_dead_strip,asd_weak_reference,asd_lazy_reference,
  377. asd_weak_definition,
  378. { for Jasmin }
  379. asd_jclass,asd_jinterface,asd_jsuper,asd_jfield,asd_jlimit,asd_jline,
  380. { .ent/.end for MIPS }
  381. asd_ent,asd_ent_end,
  382. { supported by recent clang-based assemblers for data-in-code }
  383. asd_data_region, asd_end_data_region,
  384. { ARM }
  385. asd_thumb_func,asd_code,asd_force_thumb,
  386. { restricts the assembler only to those instructions, which are
  387. available on the specified CPU; this represents directives such as
  388. NASM's 'CPU 686' or MASM/TASM's '.686p'. Might not be supported by
  389. all assemblers. }
  390. asd_cpu,
  391. { for the OMF object format }
  392. asd_omf_linnum_line,
  393. { RISC-V }
  394. asd_option
  395. );
  396. TAsmSehDirective=(
  397. ash_proc,ash_endproc,
  398. ash_endprologue,ash_handler,ash_handlerdata,
  399. ash_eh,ash_32,ash_no32,
  400. ash_setframe,ash_stackalloc,ash_pushreg,
  401. ash_savereg,ash_savereg_x,ash_saveregp,ash_saveregp_x,
  402. ash_savexmm,ash_savefreg,ash_savefreg_x,ash_savefregp,ash_savefregp_x,ash_pushframe,
  403. ash_setfp,ash_addfp,ash_savefplr,ash_savefplr_x,
  404. ash_nop,
  405. ash_pushnv,ash_savenv
  406. );
  407. TSymbolPairKind = (spk_set, spk_set_global, spk_thumb_set, spk_localentry);
  408. const
  409. regallocstr : array[tregalloctype] of string[10]=('allocated','released','sync','resized','used');
  410. tempallocstr : array[boolean] of string[10]=('released','allocated');
  411. stabtypestr : array[TStabType] of string[8]=(
  412. 'stabs','stabn','stabd',
  413. 'stabx',
  414. 'bi','ei',
  415. 'bf','ef',
  416. 'bs','es',
  417. 'line','function');
  418. directivestr : array[TAsmDirective] of string[23]=(
  419. 'indirect_symbol',
  420. 'extern','nasm_import', 'tc', 'reference',
  421. 'no_dead_strip','weak','lazy_reference','weak',
  422. { for Jasmin }
  423. 'class','interface','super','field','limit','line',
  424. { .ent/.end for MIPS }
  425. 'ent','end',
  426. { supported by recent clang-based assemblers for data-in-code }
  427. 'data_region','end_data_region',
  428. { ARM }
  429. 'thumb_func',
  430. 'code',
  431. 'force_thumb',
  432. 'cpu',
  433. { for the OMF object format }
  434. 'omf_line',
  435. { RISC-V }
  436. 'option'
  437. );
  438. sehdirectivestr : array[TAsmSehDirective] of string[16]=(
  439. '.seh_proc','.seh_endproc',
  440. '.seh_endprologue','.seh_handler','.seh_handlerdata',
  441. '.seh_eh','.seh_32','seh_no32',
  442. '.seh_setframe','.seh_stackalloc','.seh_pushreg',
  443. '.seh_savereg','.seh_savereg_x','.seh_saveregp','.seh_saveregp_x',
  444. '.seh_savexmm','.seh_savefreg','.seh_savefreg_x','.seh_savefregp','.seh_savefregp_x','.seh_pushframe',
  445. '.seh_setfp','.seh_addfp','.seh_savefplr','.seh_savefplr_x',
  446. '.seh_nop',
  447. '.pushnv','.savenv'
  448. );
  449. symbolpairkindstr: array[TSymbolPairKind] of string[11]=(
  450. '.set', '.set', '.thumb_set', '.localentry'
  451. );
  452. type
  453. tai = class;
  454. { please keep the size of this record <=12 bytes and keep it properly aligned }
  455. toper = record
  456. ot : longint;
  457. {$ifdef x86}
  458. vopext: smallint;
  459. {$ENDIF}
  460. case typ : toptype of
  461. top_none : ();
  462. top_reg : (reg:tregister);
  463. top_ref : (ref:preference);
  464. top_const : (val:tcgint);
  465. top_bool : (b:boolean);
  466. { local varsym that will be inserted in pass_generate_code }
  467. top_local : (localoper:plocaloper);
  468. {$ifdef arm}
  469. top_regset : (regset:^tcpuregisterset; regtyp: tregistertype; subreg: tsubregister; usermode: boolean);
  470. top_modeflags : (modeflags : tcpumodeflags);
  471. top_specialreg : (specialreg:tregister; specialflags:tspecialregflags);
  472. {$endif arm}
  473. {$if defined(arm) or defined(aarch64)}
  474. top_shifterop : (shifterop : pshifterop);
  475. top_conditioncode : (cc : TAsmCond);
  476. top_realconst : (val_real:bestreal);
  477. {$endif defined(arm) or defined(aarch64)}
  478. {$ifdef aarch64}
  479. top_regset : (basereg: tregister; nregs, regsetindex: byte);
  480. top_indexedreg : (indexedreg: tregister; regindex: byte);
  481. {$endif}
  482. {$ifdef m68k}
  483. top_regset : (dataregset,addrregset,fpuregset: tcpuregisterset);
  484. top_regpair : (reghi,reglo: tregister);
  485. top_realconst : (val_real:bestreal);
  486. {$endif m68k}
  487. {$ifdef jvm}
  488. top_single : (sval:single);
  489. top_double : (dval:double);
  490. top_string : (pcvallen: aint; pcval: pchar);
  491. top_wstring : (pwstrval: pcompilerwidestring);
  492. {$endif jvm}
  493. {$ifdef llvm}
  494. top_single : (sval:single);
  495. top_double : (dval:double);
  496. top_undef : ();
  497. {$ifdef cpuextended}
  498. top_extended80 : (eval:extended);
  499. {$endif cpuextended}
  500. top_tai : (ai: tai);
  501. top_def : (def: tdef);
  502. top_cond : (cond: topcmp);
  503. top_fpcond : (fpcond: tllvmfpcmp);
  504. top_para : (paras: tfplist);
  505. top_asmlist : (asmlist: tasmlist);
  506. top_callingconvention: (callingconvention: tproccalloption);
  507. {$endif llvm}
  508. {$if defined(riscv32) or defined(riscv64)}
  509. top_fenceflags : (fenceflags : TFenceFlags);
  510. top_roundingmode : (roundingmode : TRoundingMode);
  511. {$endif defined(riscv32) or defined(riscv64)}
  512. {$ifdef wasm}
  513. top_functype : (functype: TWasmFuncType);
  514. top_single : (sval:single);
  515. top_double : (dval:double);
  516. {$endif wasm}
  517. end;
  518. poper=^toper;
  519. { abstract assembler item }
  520. tai = class(TLinkedListItem)
  521. {$ifndef NOOPT}
  522. { pointer to record with optimizer info about this tai object }
  523. optinfo : pointer;
  524. {$endif NOOPT}
  525. typ : taitype;
  526. constructor Create;
  527. constructor ppuload(t:taitype;ppufile:tcompilerppufile);virtual;
  528. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  529. procedure buildderefimpl;virtual;
  530. procedure derefimpl;virtual;
  531. end;
  532. { abstract assembler item with line information }
  533. tailineinfo = class(tai)
  534. fileinfo : tfileposinfo;
  535. constructor Create;
  536. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  537. procedure ppuwrite(ppufile:tcompilerppufile);override;
  538. end;
  539. tai_simple = class(tai)
  540. constructor create(_typ : taitype);
  541. end;
  542. taiclass = class of tai;
  543. taiclassarray = array[taitype] of taiclass;
  544. { Generates an assembler string }
  545. tai_string = class(tailineinfo)
  546. str : pchar;
  547. { extra len so the string can contain an \0 }
  548. len : longint;
  549. constructor Create(const _str : string);
  550. constructor Create_pchar(_str : pchar;length : longint);
  551. destructor Destroy;override;
  552. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  553. procedure ppuwrite(ppufile:tcompilerppufile);override;
  554. function getcopy:tlinkedlistitem;override;
  555. end;
  556. { Generates a common label }
  557. tai_symbol = class(tai)
  558. sym : tasmsymbol;
  559. value : puint;
  560. size : longint;
  561. is_global,
  562. has_value : boolean;
  563. constructor Create(_sym:tasmsymbol;siz:longint);
  564. constructor Create_Global(_sym:tasmsymbol;siz:longint);
  565. constructor Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint;def:tdef);
  566. constructor Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint;def:tdef);
  567. constructor Createname_hidden(const _name : string;_symtyp:Tasmsymtype;siz:longint;def:tdef);
  568. constructor Createname_global_value(const _name : string;_symtyp:Tasmsymtype;siz:longint;val:ptruint;def:tdef);
  569. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  570. procedure ppuwrite(ppufile:tcompilerppufile);override;
  571. procedure derefimpl;override;
  572. end;
  573. tai_symbol_end = class(tailineinfo)
  574. sym : tasmsymbol;
  575. constructor Create(_sym:tasmsymbol);
  576. constructor Createname(const _name : string);
  577. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  578. procedure ppuwrite(ppufile:tcompilerppufile);override;
  579. procedure derefimpl;override;
  580. end;
  581. tai_directive = class(tailineinfo)
  582. name : ansistring;
  583. directive : TAsmDirective;
  584. constructor Create(_directive:TAsmDirective;const _name:ansistring);
  585. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  586. procedure ppuwrite(ppufile:tcompilerppufile);override;
  587. end;
  588. { Generates an assembler label }
  589. tai_label = class(tai)
  590. labsym : tasmlabel;
  591. {$ifdef arm}
  592. { set to true when the label has been moved by insertpcrelativedata to the correct location
  593. so one label can be used multiple times }
  594. moved : boolean;
  595. { true, if a label has been already inserted, this is important for arm thumb where no negative
  596. pc relative offsets are allowed }
  597. inserted : boolean;
  598. {$endif arm}
  599. constructor Create(_labsym : tasmlabel);
  600. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  601. procedure ppuwrite(ppufile:tcompilerppufile);override;
  602. procedure derefimpl;override;
  603. end;
  604. { Generates an assembler comment }
  605. tai_comment = class(tai)
  606. str : pchar;
  607. constructor Create(_str : pchar);
  608. destructor Destroy; override;
  609. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  610. procedure ppuwrite(ppufile:tcompilerppufile);override;
  611. function getcopy:tlinkedlistitem;override;
  612. end;
  613. { Generates a section / segment directive }
  614. tai_section = class(tai)
  615. sectype : TAsmSectiontype;
  616. secorder : TasmSectionorder;
  617. secalign : longint;
  618. name : pshortstring;
  619. { used in binary writer }
  620. sec : TObjSection;
  621. { used only by ELF so far }
  622. secflags : TSectionFlags;
  623. secprogbits : TSectionProgbits;
  624. destructor Destroy;override;
  625. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  626. procedure ppuwrite(ppufile:tcompilerppufile);override;
  627. {$push}{$warnings off}
  628. private
  629. { this constructor is made private on purpose }
  630. { because sections should be created via new_section() }
  631. constructor Create(Asectype:TAsmSectiontype;const Aname:string;Aalign:longint;Asecorder:TasmSectionorder=secorder_default);
  632. {$pop}
  633. end;
  634. { Generates an uninitializised data block }
  635. tai_datablock = class(tailineinfo)
  636. is_global : boolean;
  637. sym : tasmsymbol;
  638. size : asizeint;
  639. constructor Create(const _name: string; _size: asizeint; def: tdef; _typ: Tasmsymtype);
  640. constructor Create_hidden(const _name: string; _size: asizeint; def: tdef; _typ: Tasmsymtype);
  641. constructor Create_global(const _name: string; _size: asizeint; def: tdef; _typ: Tasmsymtype);
  642. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  643. procedure ppuwrite(ppufile:tcompilerppufile);override;
  644. procedure derefimpl;override;
  645. end;
  646. { Generates an integer const }
  647. { tai_const }
  648. tai_const = class(tai)
  649. sym,
  650. endsym : tasmsymbol;
  651. { if symbols and offset are provided the symofs is used,
  652. the value is calculated during assembling }
  653. symofs,
  654. value : int64;
  655. consttype : taiconst_type;
  656. { sleb128 and uleb128 values have a varying length, by calling FixSize their size can be fixed
  657. to avoid that other offsets need to be changed. The value to write is stored in fixed_size }
  658. fixed_size : byte;
  659. { we use for the 128bit int64/qword for now because I can't imagine a
  660. case where we need 128 bit now (FK) }
  661. constructor Create(_typ:taiconst_type;_value : int64);
  662. constructor Create_128bit(_value : int64);
  663. constructor Create_64bit(_value : int64);
  664. constructor Create_32bit(_value : longint);
  665. constructor Create_16bit(_value : word);
  666. constructor Create_64bit_unaligned(_value : int64);
  667. constructor Create_32bit_unaligned(_value : longint);
  668. constructor Create_16bit_unaligned(_value : word);
  669. constructor Create_8bit(_value : byte);
  670. constructor Create_char(size: integer; _value: dword);
  671. constructor Create_sleb128bit(_value : int64);
  672. constructor Create_uleb128bit(_value : qword);
  673. constructor Create_aint(_value : aint);
  674. constructor Create_sizeint(_value : asizeint);
  675. constructor Create_sizeint_unaligned(_value : asizeint);
  676. constructor Create_sym(_sym:tasmsymbol);
  677. {$ifdef i8086}
  678. constructor Create_sym_near(_sym:tasmsymbol);
  679. constructor Create_sym_far(_sym:tasmsymbol);
  680. constructor Createname_near(const name:string;ofs:asizeint);
  681. constructor Createname_far(const name:string;ofs:asizeint);
  682. constructor Createname_near(const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  683. constructor Createname_far(const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  684. {$endif i8086}
  685. constructor Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  686. constructor Create_sym_offset(_sym:tasmsymbol;ofs:asizeint);
  687. constructor Create_type_sym_offset(_typ:taiconst_type;_sym:tasmsymbol;ofs:asizeint);
  688. constructor Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  689. constructor Create_rel_sym_offset(_typ : taiconst_type; _sym,_endsym : tasmsymbol; _ofs : int64);
  690. constructor Create_rva_sym(_sym:tasmsymbol);
  691. constructor Createname(const name:string;ofs:asizeint);
  692. constructor Createname_rel(const name, endname: string);
  693. constructor Createname(const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  694. constructor Create_type_name(_typ:taiconst_type;const name:string;ofs:asizeint);
  695. constructor Create_type_name(_typ:taiconst_type;const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  696. constructor Create_nil_codeptr;
  697. constructor Create_nil_codeptr_unaligned;
  698. constructor Create_nil_dataptr;
  699. constructor Create_nil_dataptr_unaligned;
  700. constructor Create_int_codeptr(_value: int64);
  701. constructor Create_int_codeptr_unaligned(_value: int64);
  702. constructor Create_int_dataptr(_value: int64);
  703. constructor Create_int_dataptr_unaligned(_value: int64);
  704. {$ifdef avr}
  705. constructor Create_int_dataptr_unaligned(_value: int64; size: taiconst_type);
  706. {$endif}
  707. {$ifdef i8086}
  708. constructor Create_seg_name(const name:string);
  709. constructor Create_dgroup;
  710. constructor Create_fardataseg;
  711. {$endif i8086}
  712. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  713. procedure ppuwrite(ppufile:tcompilerppufile);override;
  714. procedure derefimpl;override;
  715. function getcopy:tlinkedlistitem;override;
  716. function size:longint;
  717. { sleb128 and uleb128 values have a varying length, by calling FixSize their size can be fixed
  718. to avoid that other offsets need to be changed. The value to write is stored in fixed_size }
  719. Procedure FixSize;
  720. end;
  721. { floating point const }
  722. tformatoptions = (fo_none,fo_hiloswapped);
  723. tai_realconst = class(tai)
  724. realtyp: tairealconsttype;
  725. savesize: byte;
  726. value: record
  727. case tairealconsttype of
  728. aitrealconst_s32bit: (s32val: ts32real);
  729. aitrealconst_s64bit: (s64val: ts64real);
  730. aitrealconst_s80bit: (s80val: ts80real);
  731. aitrealconst_s128bit: (s128val: ts128real);
  732. aitrealconst_s64comp: (s64compval: ts64comp);
  733. end;
  734. {$ifdef ARM}
  735. formatoptions : tformatoptions;
  736. {$endif ARM}
  737. constructor create_s32real(val: ts32real);
  738. constructor create_s64real(val: ts64real);
  739. {$ifdef ARM}
  740. constructor create_s64real_hiloswapped(val : ts64real);
  741. {$endif ARM}
  742. constructor create_s80real(val: ts80real; _savesize: byte);
  743. constructor create_s128real(val: ts128real);
  744. constructor create_s64compreal(val: ts64comp);
  745. constructor ppuload(t: taitype;ppufile: tcompilerppufile); override;
  746. procedure ppuwrite(ppufile: tcompilerppufile); override;
  747. function getcopy:tlinkedlistitem;override;
  748. function datasize: word;
  749. end;
  750. { tai_stab }
  751. tai_stab = class(tai)
  752. str : pchar;
  753. stabtype : TStabType;
  754. constructor Create(_stabtype:TStabType;_str : pchar);
  755. constructor Create_str(_stabtype:TStabType;const s:string);
  756. constructor create_ansistr(_stabtype: TStabType; const s: ansistring);
  757. destructor Destroy;override;
  758. end;
  759. tai_force_line = class(tailineinfo)
  760. constructor Create;
  761. end;
  762. tai_function_name = class(tai)
  763. funcname : pshortstring;
  764. constructor create(const s:string);
  765. destructor destroy;override;
  766. end;
  767. { Insert a cut to split assembler into several smaller files }
  768. tai_cutobject = class(tai)
  769. place : tcutplace;
  770. constructor Create;
  771. constructor Create_begin;
  772. constructor Create_end;
  773. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  774. procedure ppuwrite(ppufile:tcompilerppufile);override;
  775. end;
  776. { Insert a marker for assembler and inline blocks }
  777. tai_marker = class(tai)
  778. Kind: TAsmMarker;
  779. Constructor Create(_Kind: TAsmMarker);
  780. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  781. procedure ppuwrite(ppufile:tcompilerppufile);override;
  782. end;
  783. tai_tempalloc = class(tai)
  784. allocation : boolean;
  785. {$ifdef EXTDEBUG}
  786. problem : pshortstring;
  787. {$endif EXTDEBUG}
  788. temppos,
  789. tempsize : longint;
  790. constructor alloc(pos,size:longint);
  791. constructor dealloc(pos,size:longint);
  792. {$ifdef EXTDEBUG}
  793. constructor allocinfo(pos,size:longint;const st:string);
  794. {$endif EXTDEBUG}
  795. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  796. destructor destroy;override;
  797. procedure ppuwrite(ppufile:tcompilerppufile);override;
  798. end;
  799. tai_regalloc = class(tai)
  800. reg : tregister;
  801. ratype : TRegAllocType;
  802. { tells BuildLabelTableAndFixRegAlloc that the deallocation should be kept }
  803. keep : boolean;
  804. { reg(de)alloc belongs to this instruction, this
  805. is only used for automatic inserted (de)alloc for
  806. imaginary register and required for spilling code }
  807. instr : tai;
  808. constructor alloc(r : tregister;ainstr:tai);
  809. constructor dealloc(r : tregister;ainstr:tai);
  810. constructor sync(r : tregister);
  811. constructor resize(r : tregister);
  812. constructor markused(r : tregister);
  813. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  814. procedure ppuwrite(ppufile:tcompilerppufile);override;
  815. end;
  816. tadd_reg_instruction_proc=procedure(instr:Tai;r:tregister) of object;
  817. { Class template for assembler instructions
  818. }
  819. tai_cpu_abstract = class(tailineinfo)
  820. protected
  821. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);virtual;
  822. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);virtual;
  823. procedure ppubuildderefimploper(var o:toper);virtual;abstract;
  824. procedure ppuderefoper(var o:toper);virtual;abstract;
  825. public
  826. { Condition flags for instruction }
  827. condition : TAsmCond;
  828. { Number of operands to instruction }
  829. ops : byte;
  830. { Number of allocate oper structures }
  831. opercnt : byte;
  832. { Operands of instruction }
  833. oper : array[0..max_operands-1] of poper;
  834. { Actual opcode of instruction }
  835. opcode : tasmop;
  836. {$ifdef x86}
  837. segprefix : tregister;
  838. {$endif x86}
  839. { true if instruction is a jmp }
  840. is_jmp : boolean; { is this instruction a jump? (needed for optimizer) }
  841. Constructor Create(op : tasmop);virtual;
  842. Destructor Destroy;override;
  843. function getcopy:TLinkedListItem;override;
  844. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  845. procedure ppuwrite(ppufile:tcompilerppufile);override;
  846. procedure buildderefimpl;override;
  847. procedure derefimpl;override;
  848. procedure SetCondition(const c:TAsmCond);
  849. procedure allocate_oper(opers:longint);
  850. procedure loadconst(opidx:longint;l:tcgint);
  851. procedure loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  852. procedure loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  853. procedure loadref(opidx:longint;const r:treference);
  854. procedure loadreg(opidx:longint;r:tregister);
  855. procedure loadoper(opidx:longint;o:toper); virtual;
  856. procedure clearop(opidx:longint); virtual;
  857. procedure freeop(opidx:longint);
  858. { register allocator }
  859. function is_same_reg_move(regtype: Tregistertype):boolean;virtual;
  860. function spilling_get_operation_type(opnr: longint): topertype;virtual;
  861. function spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;virtual;
  862. function Pass1(objdata:TObjData):longint;virtual;
  863. procedure Pass2(objdata:TObjData);virtual;
  864. procedure resetpass1; virtual;
  865. procedure resetpass2; virtual;
  866. end;
  867. tai_cpu_class = class of tai_cpu_abstract;
  868. { Buffer type used for alignment }
  869. tfillbuffer = array[0..63] of char;
  870. { alignment for operator }
  871. tai_align_abstract = class(tai)
  872. aligntype : byte; { 1 = no align, 2 = word align, 4 = dword align }
  873. maxbytes : byte; { if needed bytes would be larger than maxbyes, alignment is ignored }
  874. fillsize : byte; { real size to fill }
  875. fillop : byte; { value to fill with - optional }
  876. use_op : boolean;
  877. constructor Create(b:byte);virtual;
  878. constructor Create_op(b: byte; _op: byte);virtual;
  879. constructor create_max(b: byte; max: byte);virtual;
  880. constructor Create_zeros(b:byte);
  881. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  882. procedure ppuwrite(ppufile:tcompilerppufile);override;
  883. function calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;virtual;
  884. end;
  885. tai_align_class = class of tai_align_abstract;
  886. tai_varloc = class(tai)
  887. newlocation,
  888. newlocationhi : tregister;
  889. varsym : tsym;
  890. constructor create(sym : tsym;loc : tregister);
  891. constructor create64(sym : tsym;loc,lochi : tregister);
  892. {$ifdef cpu64bitalu}
  893. constructor create128(sym : tsym;loc,lochi : tregister);
  894. {$endif cpu64bitalu}
  895. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  896. procedure ppuwrite(ppufile:tcompilerppufile);override;
  897. procedure buildderefimpl;override;
  898. procedure derefimpl;override;
  899. end;
  900. TSehDirectiveDatatype=(sd_none,sd_string,sd_reg,sd_offset,sd_regoffset);
  901. TSehDirectiveData=record
  902. case typ: TSehDirectiveDatatype of
  903. sd_none: ();
  904. sd_string: (name:pshortstring;flags:byte);
  905. sd_reg,sd_offset,sd_regoffset: (reg:TRegister;offset:dword);
  906. end;
  907. tai_seh_directive = class(tai)
  908. kind: TAsmSehDirective;
  909. data: TSehDirectiveData;
  910. constructor create(_kind:TAsmSehDirective);
  911. constructor create_name(_kind:TAsmSehDirective;const _name: string);
  912. constructor create_reg(_kind:TAsmSehDirective;r:TRegister);
  913. constructor create_offset(_kind:TAsmSehDirective;ofs:dword);
  914. constructor create_reg_offset(_kind:TAsmSehDirective;r:TRegister;ofs:dword);
  915. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  916. destructor destroy;override;
  917. procedure ppuwrite(ppufile:tcompilerppufile);override;
  918. procedure generate_code(objdata:TObjData);virtual;
  919. property datatype: TSehDirectiveDatatype read data.typ;
  920. end;
  921. tai_seh_directive_class=class of tai_seh_directive;
  922. {$ifdef JVM}
  923. { JVM variable live range description }
  924. tai_jvar = class(tai)
  925. stackslot: longint;
  926. desc: pshortstring;
  927. startlab,stoplab: tasmsymbol;
  928. constructor Create(_stackslot: longint; const _desc: shortstring; _startlab, _stoplab: TAsmSymbol);
  929. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  930. procedure ppuwrite(ppufile:tcompilerppufile);override;
  931. destructor destroy;override;
  932. end;
  933. tai_jvar_class = class of tai_jvar;
  934. { JVM exception catch description }
  935. tai_jcatch = class(tai)
  936. name: pshortstring;
  937. startlab,stoplab,handlerlab: tasmsymbol;
  938. constructor Create(const _name: shortstring; _startlab, _stoplab, _handlerlab: TAsmSymbol);
  939. destructor destroy;override;
  940. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  941. procedure ppuwrite(ppufile:tcompilerppufile);override;
  942. end;
  943. tai_jcatch_class = class of tai_jcatch;
  944. {$endif JVM}
  945. tai_symbolpair = class(tai)
  946. kind: TSymbolPairKind;
  947. sym,
  948. value: pshortstring;
  949. constructor create(akind: TSymbolPairKind; const asym, avalue: string);
  950. destructor destroy;override;
  951. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  952. procedure ppuwrite(ppufile:tcompilerppufile);override;
  953. end;
  954. teattrtyp = (eattrtype_none,eattrtype_dword,eattrtype_ntbs);
  955. tai_eabi_attribute = class(tai)
  956. eattr_typ : teattrtyp;
  957. tag,value : dword;
  958. valuestr : pstring;
  959. constructor create(atag,avalue : dword);
  960. constructor create(atag : dword;const avalue : string);
  961. destructor destroy;override;
  962. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  963. procedure ppuwrite(ppufile:tcompilerppufile);override;
  964. end;
  965. var
  966. { array with all class types for tais }
  967. aiclass : taiclassarray;
  968. { target specific tais, possibly overwritten in target specific aasmcpu }
  969. cai_align : tai_align_class = tai_align_abstract;
  970. cai_cpu : tai_cpu_class = tai_cpu_abstract;
  971. cai_seh_directive: tai_seh_directive_class = tai_seh_directive;
  972. { hook to notify uses of registers }
  973. add_reg_instruction_hook : tadd_reg_instruction_proc;
  974. procedure maybe_new_object_file(list:TAsmList);
  975. function new_section(list:TAsmList;Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default) : tai_section;
  976. function ppuloadai(ppufile:tcompilerppufile):tai;
  977. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  978. implementation
  979. uses
  980. {$ifdef x86}
  981. aasmcpu,
  982. {$endif x86}
  983. SysUtils,
  984. verbose,
  985. globals,
  986. ppu;
  987. const
  988. pputaimarker = 254;
  989. {****************************************************************************
  990. Helpers
  991. ****************************************************************************}
  992. procedure maybe_new_object_file(list:TAsmList);
  993. begin
  994. if create_smartlink_library then
  995. list.concat(tai_cutobject.create);
  996. end;
  997. function new_section(list:TAsmList;Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default) : tai_section;
  998. begin
  999. Result:=tai_section.create(Asectype,Aname,Aalign,Asecorder);
  1000. list.concat(Result);
  1001. inc(list.section_count);
  1002. list.concat(cai_align.create(Aalign));
  1003. end;
  1004. function ppuloadai(ppufile:tcompilerppufile):tai;
  1005. var
  1006. b : byte;
  1007. t : taitype;
  1008. begin
  1009. { marker }
  1010. b:=ppufile.getbyte;
  1011. if b<>pputaimarker then
  1012. internalerror(200208181);
  1013. { load nodetype }
  1014. t:=taitype(ppufile.getbyte);
  1015. if t<>ait_none then
  1016. begin
  1017. if t>high(taitype) then
  1018. internalerror(200208182);
  1019. if not assigned(aiclass[t]) then
  1020. internalerror(200208183);
  1021. {writeln('taiload: ',taitypestr[t]);}
  1022. { generate tai of the correct class }
  1023. ppuloadai:=aiclass[t].ppuload(t,ppufile);
  1024. end
  1025. else
  1026. ppuloadai:=nil;
  1027. end;
  1028. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  1029. begin
  1030. { marker, read by ppuloadnode }
  1031. ppufile.putbyte(pputaimarker);
  1032. if assigned(n) then
  1033. begin
  1034. { type, read by ppuloadnode }
  1035. ppufile.putbyte(byte(n.typ));
  1036. {writeln('taiwrite: ',taitypestr[n.typ]);}
  1037. n.ppuwrite(ppufile);
  1038. end
  1039. else
  1040. ppufile.putbyte(byte(ait_none));
  1041. end;
  1042. constructor tai_symbolpair.create(akind: TSymbolPairKind; const asym, avalue: string);
  1043. begin
  1044. inherited create;
  1045. kind:=akind;
  1046. typ:=ait_symbolpair;
  1047. sym:=stringdup(asym);
  1048. value:=stringdup(avalue);
  1049. end;
  1050. destructor tai_symbolpair.destroy;
  1051. begin
  1052. stringdispose(sym);
  1053. stringdispose(value);
  1054. inherited destroy;
  1055. end;
  1056. constructor tai_symbolpair.ppuload(t: taitype; ppufile: tcompilerppufile);
  1057. begin
  1058. inherited ppuload(t,ppufile);
  1059. kind:=TSymbolPairKind(ppufile.getbyte);
  1060. sym:=ppufile.getpshortstring;
  1061. value:=ppufile.getpshortstring;
  1062. end;
  1063. procedure tai_symbolpair.ppuwrite(ppufile: tcompilerppufile);
  1064. begin
  1065. inherited ppuwrite(ppufile);
  1066. ppufile.putbyte(byte(kind));
  1067. ppufile.putstring(sym^);
  1068. ppufile.putstring(value^);
  1069. end;
  1070. constructor tai_varloc.create(sym: tsym; loc: tregister);
  1071. begin
  1072. inherited Create;
  1073. typ:=ait_varloc;
  1074. newlocation:=loc;
  1075. newlocationhi:=NR_NO;
  1076. varsym:=sym;
  1077. end;
  1078. constructor tai_varloc.create64(sym: tsym; loc, lochi: tregister);
  1079. begin
  1080. inherited Create;
  1081. typ:=ait_varloc;
  1082. newlocation:=loc;
  1083. newlocationhi:=lochi;
  1084. varsym:=sym;
  1085. end;
  1086. {$ifdef cpu64bitalu}
  1087. constructor tai_varloc.create128(sym: tsym; loc, lochi: tregister);
  1088. begin
  1089. inherited Create;
  1090. typ:=ait_varloc;
  1091. newlocation:=loc;
  1092. newlocationhi:=lochi;
  1093. varsym:=sym;
  1094. end;
  1095. {$endif cpu64bitalu}
  1096. constructor tai_varloc.ppuload(t: taitype; ppufile: tcompilerppufile);
  1097. begin
  1098. inherited ppuload(t, ppufile);
  1099. end;
  1100. procedure tai_varloc.ppuwrite(ppufile: tcompilerppufile);
  1101. begin
  1102. inherited ppuwrite(ppufile);
  1103. end;
  1104. procedure tai_varloc.buildderefimpl;
  1105. begin
  1106. inherited buildderefimpl;
  1107. end;
  1108. procedure tai_varloc.derefimpl;
  1109. begin
  1110. inherited derefimpl;
  1111. end;
  1112. {****************************************************************************
  1113. TAI
  1114. ****************************************************************************}
  1115. constructor tai.Create;
  1116. begin
  1117. {$ifndef NOOPT}
  1118. optinfo:=nil;
  1119. {$endif NOOPT}
  1120. end;
  1121. constructor tai.ppuload(t:taitype;ppufile:tcompilerppufile);
  1122. begin
  1123. typ:=t;
  1124. {$ifndef NOOPT}
  1125. optinfo:=nil;
  1126. {$endif}
  1127. end;
  1128. procedure tai.ppuwrite(ppufile:tcompilerppufile);
  1129. begin
  1130. end;
  1131. procedure tai.buildderefimpl;
  1132. begin
  1133. end;
  1134. procedure tai.derefimpl;
  1135. begin
  1136. end;
  1137. {****************************************************************************
  1138. TAILINEINFO
  1139. ****************************************************************************}
  1140. constructor tailineinfo.create;
  1141. begin
  1142. inherited create;
  1143. fileinfo:=current_filepos;
  1144. end;
  1145. constructor tailineinfo.ppuload(t:taitype;ppufile:tcompilerppufile);
  1146. begin
  1147. inherited ppuload(t,ppufile);
  1148. ppufile.getposinfo(fileinfo);
  1149. end;
  1150. procedure tailineinfo.ppuwrite(ppufile:tcompilerppufile);
  1151. begin
  1152. inherited ppuwrite(ppufile);
  1153. ppufile.putposinfo(fileinfo);
  1154. end;
  1155. {****************************************************************************
  1156. TAI_SIMPLE
  1157. ****************************************************************************}
  1158. constructor tai_simple.create(_typ : taitype);
  1159. begin
  1160. inherited create;
  1161. typ:=_typ;
  1162. end;
  1163. {****************************************************************************
  1164. TAI_SECTION
  1165. ****************************************************************************}
  1166. constructor tai_section.Create(Asectype:TAsmSectiontype;const Aname:string;Aalign:longint;Asecorder:TasmSectionorder=secorder_default);
  1167. begin
  1168. inherited Create;
  1169. typ:=ait_section;
  1170. sectype:=asectype;
  1171. secalign:=Aalign;
  1172. secorder:=Asecorder;
  1173. TObjData.sectiontype2progbitsandflags(sectype,secprogbits,secflags);
  1174. name:=stringdup(Aname);
  1175. sec:=nil;
  1176. end;
  1177. constructor tai_section.ppuload(t:taitype;ppufile:tcompilerppufile);
  1178. begin
  1179. inherited ppuload(t,ppufile);
  1180. sectype:=TAsmSectiontype(ppufile.getbyte);
  1181. secalign:=ppufile.getlongint;
  1182. name:=ppufile.getpshortstring;
  1183. ppufile.getset(tppuset1(secflags));
  1184. secprogbits:=TSectionProgbits(ppufile.getbyte);
  1185. sec:=nil;
  1186. end;
  1187. destructor tai_section.Destroy;
  1188. begin
  1189. stringdispose(name);
  1190. end;
  1191. procedure tai_section.ppuwrite(ppufile:tcompilerppufile);
  1192. begin
  1193. inherited ppuwrite(ppufile);
  1194. ppufile.putbyte(byte(sectype));
  1195. ppufile.putlongint(secalign);
  1196. ppufile.putstring(name^);
  1197. ppufile.putset(tppuset1(secflags));
  1198. ppufile.putbyte(byte(secprogbits));
  1199. end;
  1200. {****************************************************************************
  1201. TAI_DATABLOCK
  1202. ****************************************************************************}
  1203. constructor tai_datablock.Create(const _name : string;_size : asizeint; def: tdef; _typ:Tasmsymtype);
  1204. begin
  1205. inherited Create;
  1206. typ:=ait_datablock;
  1207. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,_typ,def);
  1208. { keep things aligned }
  1209. if _size<=0 then
  1210. _size:=sizeof(aint);
  1211. size:=_size;
  1212. is_global:=false;
  1213. end;
  1214. constructor tai_datablock.Create_hidden(const _name: string; _size: asizeint; def: tdef; _typ:Tasmsymtype);
  1215. begin
  1216. if tf_supports_hidden_symbols in target_info.flags then
  1217. begin
  1218. inherited Create;
  1219. typ:=ait_datablock;
  1220. sym:=current_asmdata.DefineAsmSymbol(_name,AB_PRIVATE_EXTERN,_typ,def);
  1221. { keep things aligned }
  1222. if _size<=0 then
  1223. _size:=sizeof(aint);
  1224. size:=_size;
  1225. is_global:=true;
  1226. end
  1227. else
  1228. Create(_name,_size,def,_typ);
  1229. end;
  1230. constructor tai_datablock.Create_global(const _name : string;_size : asizeint; def: tdef; _typ:Tasmsymtype);
  1231. begin
  1232. inherited Create;
  1233. typ:=ait_datablock;
  1234. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,_typ,def);
  1235. { keep things aligned }
  1236. if _size<=0 then
  1237. _size:=sizeof(aint);
  1238. size:=_size;
  1239. is_global:=true;
  1240. end;
  1241. constructor tai_datablock.ppuload(t:taitype;ppufile:tcompilerppufile);
  1242. begin
  1243. inherited Create;
  1244. sym:=ppufile.getasmsymbol;
  1245. size:=ppufile.getaint;
  1246. is_global:=ppufile.getboolean;
  1247. end;
  1248. procedure tai_datablock.ppuwrite(ppufile:tcompilerppufile);
  1249. begin
  1250. inherited ppuwrite(ppufile);
  1251. ppufile.putasmsymbol(sym);
  1252. ppufile.putaint(size);
  1253. ppufile.putboolean(is_global);
  1254. end;
  1255. procedure tai_datablock.derefimpl;
  1256. begin
  1257. end;
  1258. {****************************************************************************
  1259. TAI_SYMBOL
  1260. ****************************************************************************}
  1261. constructor tai_symbol.Create(_sym:tasmsymbol;siz:longint);
  1262. begin
  1263. inherited Create;
  1264. typ:=ait_symbol;
  1265. sym:=_sym;
  1266. size:=siz;
  1267. { don't redefine global/external symbols as local, as code to access
  1268. such symbols is different on some platforms }
  1269. if not(sym.bind in [AB_NONE,AB_LOCAL]) then
  1270. internalerror(2013081601);
  1271. sym.bind:=AB_LOCAL;
  1272. is_global:=false;
  1273. end;
  1274. constructor tai_symbol.Create_global(_sym:tasmsymbol;siz:longint);
  1275. begin
  1276. inherited Create;
  1277. typ:=ait_symbol;
  1278. sym:=_sym;
  1279. size:=siz;
  1280. { don't override PRIVATE_EXTERN with GLOBAL }
  1281. if not(sym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN]) then
  1282. sym.bind:=AB_GLOBAL;
  1283. is_global:=true;
  1284. end;
  1285. constructor tai_symbol.Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint;def:tdef);
  1286. begin
  1287. inherited Create;
  1288. typ:=ait_symbol;
  1289. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,_symtyp,def);
  1290. size:=siz;
  1291. is_global:=false;
  1292. end;
  1293. constructor tai_symbol.Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint;def:tdef);
  1294. begin
  1295. inherited Create;
  1296. typ:=ait_symbol;
  1297. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,_symtyp,def);
  1298. size:=siz;
  1299. is_global:=true;
  1300. end;
  1301. constructor tai_symbol.Createname_hidden(const _name: string; _symtyp: Tasmsymtype; siz: longint; def: tdef);
  1302. begin
  1303. if tf_supports_hidden_symbols in target_info.flags then
  1304. begin
  1305. inherited Create;
  1306. typ:=ait_symbol;
  1307. sym:=current_asmdata.DefineAsmSymbol(_name,AB_PRIVATE_EXTERN,_symtyp,def);
  1308. size:=siz;
  1309. is_global:=true;
  1310. end
  1311. else
  1312. Createname(_name, _symtyp, siz, def);
  1313. end;
  1314. constructor tai_symbol.createname_global_value(const _name: string;_symtyp: tasmsymtype; siz: longint; val: ptruint;def:tdef);
  1315. begin
  1316. Createname_global(_name,_symtyp,siz,def);
  1317. value:=val;
  1318. has_value:=true;
  1319. end;
  1320. constructor tai_symbol.ppuload(t:taitype;ppufile:tcompilerppufile);
  1321. begin
  1322. inherited ppuload(t,ppufile);
  1323. sym:=ppufile.getasmsymbol;
  1324. size:=ppufile.getlongint;
  1325. is_global:=ppufile.getboolean;
  1326. end;
  1327. procedure tai_symbol.ppuwrite(ppufile:tcompilerppufile);
  1328. begin
  1329. inherited ppuwrite(ppufile);
  1330. ppufile.putasmsymbol(sym);
  1331. ppufile.putlongint(size);
  1332. ppufile.putboolean(is_global);
  1333. end;
  1334. procedure tai_symbol.derefimpl;
  1335. begin
  1336. end;
  1337. {****************************************************************************
  1338. TAI_SYMBOL_END
  1339. ****************************************************************************}
  1340. constructor tai_symbol_end.Create(_sym:tasmsymbol);
  1341. begin
  1342. inherited Create;
  1343. typ:=ait_symbol_end;
  1344. sym:=_sym;
  1345. end;
  1346. constructor tai_symbol_end.Createname(const _name : string);
  1347. begin
  1348. inherited Create;
  1349. typ:=ait_symbol_end;
  1350. sym:=current_asmdata.GetAsmSymbol(_name);
  1351. if not assigned(sym) then
  1352. internalerror(2013080301);
  1353. end;
  1354. constructor tai_symbol_end.ppuload(t:taitype;ppufile:tcompilerppufile);
  1355. begin
  1356. inherited ppuload(t,ppufile);
  1357. sym:=ppufile.getasmsymbol;
  1358. end;
  1359. procedure tai_symbol_end.ppuwrite(ppufile:tcompilerppufile);
  1360. begin
  1361. inherited ppuwrite(ppufile);
  1362. ppufile.putasmsymbol(sym);
  1363. end;
  1364. procedure tai_symbol_end.derefimpl;
  1365. begin
  1366. end;
  1367. {****************************************************************************
  1368. TAI_SYMBOL_END
  1369. ****************************************************************************}
  1370. constructor tai_directive.Create(_directive:TAsmDirective;const _name:ansistring);
  1371. begin
  1372. inherited Create;
  1373. typ:=ait_directive;
  1374. name:=_name;
  1375. directive:=_directive;
  1376. end;
  1377. constructor tai_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  1378. begin
  1379. inherited ppuload(t,ppufile);
  1380. name:=ppufile.getansistring;
  1381. directive:=TAsmDirective(ppufile.getbyte);
  1382. end;
  1383. procedure tai_directive.ppuwrite(ppufile:tcompilerppufile);
  1384. begin
  1385. inherited ppuwrite(ppufile);
  1386. ppufile.putansistring(name);
  1387. ppufile.putbyte(byte(directive));
  1388. end;
  1389. {****************************************************************************
  1390. TAI_CONST
  1391. ****************************************************************************}
  1392. constructor tai_const.Create(_typ:taiconst_type;_value : int64);
  1393. begin
  1394. inherited Create;
  1395. typ:=ait_const;
  1396. consttype:=_typ;
  1397. value:=_value;
  1398. sym:=nil;
  1399. endsym:=nil;
  1400. end;
  1401. constructor tai_const.Create_128bit(_value : int64);
  1402. begin
  1403. inherited Create;
  1404. typ:=ait_const;
  1405. consttype:=aitconst_128bit;
  1406. value:=_value;
  1407. sym:=nil;
  1408. endsym:=nil;
  1409. end;
  1410. constructor tai_const.Create_64bit(_value : int64);
  1411. begin
  1412. inherited Create;
  1413. typ:=ait_const;
  1414. consttype:=aitconst_64bit;
  1415. value:=_value;
  1416. sym:=nil;
  1417. endsym:=nil;
  1418. end;
  1419. constructor tai_const.Create_32bit(_value : longint);
  1420. begin
  1421. inherited Create;
  1422. typ:=ait_const;
  1423. consttype:=aitconst_32bit;
  1424. value:=_value;
  1425. sym:=nil;
  1426. endsym:=nil;
  1427. end;
  1428. constructor tai_const.Create_16bit(_value : word);
  1429. begin
  1430. inherited Create;
  1431. typ:=ait_const;
  1432. consttype:=aitconst_16bit;
  1433. value:=_value;
  1434. sym:=nil;
  1435. endsym:=nil;
  1436. end;
  1437. constructor tai_const.Create_64bit_unaligned(_value : int64);
  1438. begin
  1439. inherited Create;
  1440. typ:=ait_const;
  1441. consttype:=aitconst_64bit_unaligned;
  1442. value:=_value;
  1443. sym:=nil;
  1444. endsym:=nil;
  1445. end;
  1446. constructor tai_const.Create_32bit_unaligned(_value : longint);
  1447. begin
  1448. inherited Create;
  1449. typ:=ait_const;
  1450. consttype:=aitconst_32bit_unaligned;
  1451. value:=_value;
  1452. sym:=nil;
  1453. endsym:=nil;
  1454. end;
  1455. constructor tai_const.Create_16bit_unaligned(_value : word);
  1456. begin
  1457. inherited Create;
  1458. typ:=ait_const;
  1459. consttype:=aitconst_16bit_unaligned;
  1460. value:=_value;
  1461. sym:=nil;
  1462. endsym:=nil;
  1463. end;
  1464. constructor tai_const.Create_8bit(_value : byte);
  1465. begin
  1466. inherited Create;
  1467. typ:=ait_const;
  1468. consttype:=aitconst_8bit;
  1469. value:=_value;
  1470. sym:=nil;
  1471. endsym:=nil;
  1472. end;
  1473. constructor tai_const.Create_char(size: integer; _value: dword);
  1474. begin
  1475. inherited Create;
  1476. typ:=ait_const;
  1477. case size of
  1478. 1:
  1479. begin
  1480. consttype:=aitconst_8bit;
  1481. value:=byte(_value)
  1482. end;
  1483. 2:
  1484. begin
  1485. consttype:=aitconst_16bit;
  1486. value:=word(_value)
  1487. end
  1488. else
  1489. InternalError(2010030701)
  1490. end
  1491. end;
  1492. constructor tai_const.Create_sleb128bit(_value : int64);
  1493. begin
  1494. inherited Create;
  1495. typ:=ait_const;
  1496. consttype:=aitconst_sleb128bit;
  1497. value:=_value;
  1498. sym:=nil;
  1499. endsym:=nil;
  1500. end;
  1501. constructor tai_const.Create_uleb128bit(_value : qword);
  1502. begin
  1503. inherited Create;
  1504. typ:=ait_const;
  1505. consttype:=aitconst_uleb128bit;
  1506. value:=int64(_value);
  1507. sym:=nil;
  1508. endsym:=nil;
  1509. end;
  1510. constructor tai_const.Create_aint(_value : aint);
  1511. begin
  1512. inherited Create;
  1513. typ:=ait_const;
  1514. consttype:=aitconst_aint;
  1515. value:=_value;
  1516. sym:=nil;
  1517. endsym:=nil;
  1518. end;
  1519. constructor tai_const.Create_sizeint(_value : asizeint);
  1520. begin
  1521. inherited Create;
  1522. typ:=ait_const;
  1523. consttype:=aitconst_sizeint;
  1524. value:=_value;
  1525. sym:=nil;
  1526. endsym:=nil;
  1527. end;
  1528. constructor tai_const.Create_sizeint_unaligned(_value : asizeint);
  1529. begin
  1530. inherited Create;
  1531. typ:=ait_const;
  1532. consttype:=aitconst_sizeint_unaligned;
  1533. value:=_value;
  1534. sym:=nil;
  1535. endsym:=nil;
  1536. end;
  1537. constructor tai_const.Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  1538. begin
  1539. inherited Create;
  1540. typ:=ait_const;
  1541. consttype:=_typ;
  1542. sym:=_sym;
  1543. endsym:=nil;
  1544. value:=0;
  1545. { update sym info }
  1546. if assigned(sym) then
  1547. sym.increfs;
  1548. end;
  1549. constructor tai_const.Create_sym(_sym:tasmsymbol);
  1550. begin
  1551. self.create_sym_offset(_sym,0);
  1552. end;
  1553. {$ifdef i8086}
  1554. constructor tai_const.Create_sym_near(_sym: tasmsymbol);
  1555. begin
  1556. self.create_sym(_sym);
  1557. consttype:=aitconst_ptr;
  1558. end;
  1559. constructor tai_const.Create_sym_far(_sym: tasmsymbol);
  1560. begin
  1561. self.create_sym(_sym);
  1562. consttype:=aitconst_farptr;
  1563. end;
  1564. constructor tai_const.Createname_near(const name:string;ofs:asizeint);
  1565. begin
  1566. self.Createname(name,ofs);
  1567. consttype:=aitconst_ptr;
  1568. end;
  1569. constructor tai_const.Createname_far(const name:string;ofs:asizeint);
  1570. begin
  1571. self.Createname(name,ofs);
  1572. consttype:=aitconst_farptr;
  1573. end;
  1574. constructor tai_const.Createname_near(const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  1575. begin
  1576. self.Createname(name,_symtyp,ofs);
  1577. consttype:=aitconst_ptr;
  1578. end;
  1579. constructor tai_const.Createname_far(const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  1580. begin
  1581. self.Createname(name,_symtyp,ofs);
  1582. consttype:=aitconst_farptr;
  1583. end;
  1584. {$endif i8086}
  1585. constructor tai_const.Create_sym_offset(_sym:tasmsymbol;ofs:asizeint);
  1586. begin
  1587. inherited Create;
  1588. typ:=ait_const;
  1589. {$ifdef i8086}
  1590. if assigned(_sym) and (_sym.typ=AT_DATA) then
  1591. begin
  1592. if current_settings.x86memorymodel in x86_far_data_models then
  1593. consttype:=aitconst_farptr
  1594. else
  1595. consttype:=aitconst_ptr;
  1596. end
  1597. else
  1598. begin
  1599. if current_settings.x86memorymodel in x86_far_code_models then
  1600. consttype:=aitconst_farptr
  1601. else
  1602. consttype:=aitconst_ptr;
  1603. end;
  1604. {$else i8086}
  1605. {$ifdef avr}
  1606. if assigned(_sym) and (_sym.typ=AT_FUNCTION) then
  1607. consttype:=aitconst_gs
  1608. else
  1609. {$endif avr}
  1610. consttype:=aitconst_ptr;
  1611. {$endif i8086}
  1612. { sym is allowed to be nil, this is used to write nil pointers }
  1613. sym:=_sym;
  1614. endsym:=nil;
  1615. { store the original offset in symofs so that we can recalculate the
  1616. value field in the assembler }
  1617. symofs:=ofs;
  1618. value:=ofs;
  1619. { update sym info }
  1620. if assigned(sym) then
  1621. sym.increfs;
  1622. end;
  1623. constructor tai_const.Create_type_sym_offset(_typ : taiconst_type;_sym : tasmsymbol; ofs : asizeint);
  1624. begin
  1625. inherited Create;
  1626. typ:=ait_const;
  1627. consttype:=_typ;
  1628. { sym is allowed to be nil, this is used to write nil pointers }
  1629. sym:=_sym;
  1630. endsym:=nil;
  1631. { store the original offset in symofs so that we can recalculate the
  1632. value field in the assembler }
  1633. symofs:=ofs;
  1634. value:=ofs;
  1635. { update sym info }
  1636. if assigned(sym) then
  1637. sym.increfs;
  1638. end;
  1639. constructor tai_const.Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  1640. begin
  1641. self.create_sym_offset(_sym,0);
  1642. consttype:=_typ;
  1643. endsym:=_endsym;
  1644. endsym.increfs;
  1645. end;
  1646. constructor tai_const.Create_rel_sym_offset(_typ: taiconst_type; _sym, _endsym: tasmsymbol; _ofs: int64);
  1647. begin
  1648. self.create_sym_offset(_sym,_ofs);
  1649. consttype:=_typ;
  1650. endsym:=_endsym;
  1651. endsym.increfs;
  1652. end;
  1653. constructor tai_const.Create_rva_sym(_sym:tasmsymbol);
  1654. begin
  1655. self.create_sym_offset(_sym,0);
  1656. consttype:=aitconst_rva_symbol;
  1657. end;
  1658. constructor tai_const.Createname(const name:string;ofs:asizeint);
  1659. begin
  1660. self.Createname(name,AT_NONE,ofs);
  1661. end;
  1662. constructor tai_const.Createname(const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  1663. begin
  1664. self.create_sym_offset(current_asmdata.RefAsmSymbol(name,_symtyp),ofs);
  1665. end;
  1666. constructor tai_const.Createname_rel(const name,endname:string);
  1667. begin
  1668. self.create_sym_offset(current_asmdata.RefAsmSymbol(name,AT_NONE),0);
  1669. endsym:=current_asmdata.RefAsmSymbol(endname,AT_NONE)
  1670. end;
  1671. constructor tai_const.Create_type_name(_typ:taiconst_type;const name:string;ofs:asizeint);
  1672. begin
  1673. self.Create_type_name(_typ,name,AT_NONE,ofs);
  1674. end;
  1675. constructor tai_const.Create_type_name(_typ:taiconst_type;const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  1676. begin
  1677. self.create_sym_offset(current_asmdata.RefAsmSymbol(name,_symtyp),ofs);
  1678. consttype:=_typ;
  1679. end;
  1680. constructor tai_const.Create_nil_codeptr;
  1681. begin
  1682. self.Create_int_codeptr(0);
  1683. end;
  1684. constructor tai_const.Create_nil_codeptr_unaligned;
  1685. begin
  1686. self.Create_int_codeptr_unaligned(0);
  1687. end;
  1688. constructor tai_const.Create_nil_dataptr;
  1689. begin
  1690. self.Create_int_dataptr(0);
  1691. end;
  1692. constructor tai_const.Create_nil_dataptr_unaligned;
  1693. begin
  1694. self.Create_int_dataptr_unaligned(0);
  1695. end;
  1696. constructor tai_const.Create_int_codeptr(_value: int64);
  1697. begin
  1698. inherited Create;
  1699. typ:=ait_const;
  1700. {$ifdef i8086}
  1701. if current_settings.x86memorymodel in x86_far_code_models then
  1702. consttype:=aitconst_farptr
  1703. else
  1704. {$endif i8086}
  1705. {$ifdef avr}
  1706. consttype:=aitconst_gs;
  1707. {$else avr}
  1708. consttype:=aitconst_ptr;
  1709. {$endif avr}
  1710. sym:=nil;
  1711. endsym:=nil;
  1712. symofs:=0;
  1713. value:=_value;
  1714. end;
  1715. constructor tai_const.Create_int_codeptr_unaligned(_value: int64);
  1716. begin
  1717. inherited Create;
  1718. typ:=ait_const;
  1719. {$ifdef i8086}
  1720. if current_settings.x86memorymodel in x86_far_code_models then
  1721. consttype:=aitconst_farptr
  1722. else
  1723. {$endif i8086}
  1724. {$ifdef avr}
  1725. consttype:=aitconst_gs;
  1726. {$else avr}
  1727. consttype:=aitconst_ptr_unaligned;
  1728. {$endif avr}
  1729. sym:=nil;
  1730. endsym:=nil;
  1731. symofs:=0;
  1732. value:=_value;
  1733. end;
  1734. constructor tai_const.Create_int_dataptr(_value: int64);
  1735. begin
  1736. inherited Create;
  1737. typ:=ait_const;
  1738. {$ifdef i8086}
  1739. if current_settings.x86memorymodel in x86_far_data_models then
  1740. consttype:=aitconst_farptr
  1741. else
  1742. {$endif i8086}
  1743. consttype:=aitconst_ptr;
  1744. sym:=nil;
  1745. endsym:=nil;
  1746. symofs:=0;
  1747. value:=_value;
  1748. end;
  1749. constructor tai_const.Create_int_dataptr_unaligned(_value: int64);
  1750. begin
  1751. inherited Create;
  1752. typ:=ait_const;
  1753. {$ifdef i8086}
  1754. if current_settings.x86memorymodel in x86_far_data_models then
  1755. consttype:=aitconst_farptr
  1756. else
  1757. {$endif i8086}
  1758. consttype:=aitconst_ptr_unaligned;
  1759. sym:=nil;
  1760. endsym:=nil;
  1761. symofs:=0;
  1762. value:=_value;
  1763. end;
  1764. {$ifdef avr}
  1765. constructor tai_const.Create_int_dataptr_unaligned(_value: int64;
  1766. size: taiconst_type);
  1767. begin
  1768. inherited Create;
  1769. typ:=ait_const;
  1770. consttype:=size;
  1771. sym:=nil;
  1772. endsym:=nil;
  1773. symofs:=0;
  1774. value:=_value;
  1775. end;
  1776. {$endif avr}
  1777. {$ifdef i8086}
  1778. constructor tai_const.Create_seg_name(const name:string);
  1779. begin
  1780. self.Createname(name,0);
  1781. self.consttype:=aitconst_seg;
  1782. end;
  1783. constructor tai_const.Create_dgroup;
  1784. begin
  1785. self.Create_16bit(0);
  1786. self.consttype:=aitconst_dgroup;
  1787. end;
  1788. constructor tai_const.Create_fardataseg;
  1789. begin
  1790. self.Create_16bit(0);
  1791. self.consttype:=aitconst_fardataseg;
  1792. end;
  1793. {$endif i8086}
  1794. constructor tai_const.ppuload(t:taitype;ppufile:tcompilerppufile);
  1795. begin
  1796. inherited ppuload(t,ppufile);
  1797. consttype:=taiconst_type(ppufile.getbyte);
  1798. sym:=ppufile.getasmsymbol;
  1799. endsym:=ppufile.getasmsymbol;
  1800. value:=ppufile.getint64;
  1801. end;
  1802. procedure tai_const.ppuwrite(ppufile:tcompilerppufile);
  1803. begin
  1804. inherited ppuwrite(ppufile);
  1805. ppufile.putbyte(byte(consttype));
  1806. ppufile.putasmsymbol(sym);
  1807. ppufile.putasmsymbol(endsym);
  1808. ppufile.putint64(value);
  1809. end;
  1810. procedure tai_const.derefimpl;
  1811. begin
  1812. end;
  1813. function tai_const.getcopy:tlinkedlistitem;
  1814. begin
  1815. getcopy:=inherited getcopy;
  1816. { we need to increase the reference number }
  1817. if assigned(sym) then
  1818. sym.increfs;
  1819. if assigned(endsym) then
  1820. endsym.increfs;
  1821. end;
  1822. function tai_const.size:longint;
  1823. begin
  1824. case consttype of
  1825. aitconst_8bit :
  1826. result:=1;
  1827. aitconst_16bit,aitconst_16bit_unaligned :
  1828. result:=2;
  1829. aitconst_32bit,aitconst_darwin_dwarf_delta32,
  1830. aitconst_32bit_unaligned:
  1831. result:=4;
  1832. aitconst_64bit,aitconst_darwin_dwarf_delta64,
  1833. aitconst_64bit_unaligned:
  1834. result:=8;
  1835. aitconst_secrel32_symbol,
  1836. aitconst_rva_symbol :
  1837. if target_info.system in systems_peoptplus then
  1838. result:=sizeof(longint)
  1839. else
  1840. result:=sizeof(pint);
  1841. aitconst_uleb128bit :
  1842. begin
  1843. if fixed_size>0 then
  1844. result:=fixed_size
  1845. else if sym=nil then
  1846. begin
  1847. FixSize;
  1848. result:=fixed_size;
  1849. end
  1850. else
  1851. { worst case }
  1852. result:=sizeof(pint)+2;
  1853. end;
  1854. aitconst_sleb128bit :
  1855. begin
  1856. if fixed_size>0 then
  1857. result:=fixed_size
  1858. else if sym=nil then
  1859. begin
  1860. FixSize;
  1861. result:=fixed_size;
  1862. end
  1863. else
  1864. { worst case }
  1865. result:=sizeof(pint)+2;
  1866. end;
  1867. aitconst_half16bit,
  1868. aitconst_gs:
  1869. result:=2;
  1870. aitconst_farptr:
  1871. result:=4;
  1872. aitconst_dgroup,
  1873. aitconst_fardataseg,
  1874. aitconst_seg:
  1875. result:=2;
  1876. aitconst_got:
  1877. result:=sizeof(pint);
  1878. aitconst_gotoff_symbol:
  1879. result:=4;
  1880. aitconst_gottpoff:
  1881. result:=4;
  1882. aitconst_tlsgd:
  1883. result:=4;
  1884. aitconst_tpoff:
  1885. result:=4;
  1886. aitconst_tlsdesc:
  1887. result:=4;
  1888. aitconst_dtpoff:
  1889. result:=4;
  1890. else
  1891. internalerror(200603253);
  1892. end;
  1893. end;
  1894. procedure tai_const.FixSize;
  1895. begin
  1896. case consttype of
  1897. aitconst_uleb128bit:
  1898. fixed_size:=LengthUleb128(qword(value));
  1899. aitconst_sleb128bit:
  1900. fixed_size:=LengthSleb128(value);
  1901. else
  1902. Internalerror(2019030301);
  1903. end;
  1904. end;
  1905. {****************************************************************************
  1906. TAI_realconst
  1907. ****************************************************************************}
  1908. constructor tai_realconst.create_s32real(val: ts32real);
  1909. begin
  1910. inherited create;
  1911. typ:=ait_realconst;
  1912. realtyp:=aitrealconst_s32bit;
  1913. savesize:=4;
  1914. value.s32val:=val;
  1915. end;
  1916. constructor tai_realconst.create_s64real(val: ts64real);
  1917. begin
  1918. inherited create;
  1919. typ:=ait_realconst;
  1920. realtyp:=aitrealconst_s64bit;
  1921. savesize:=8;
  1922. value.s64val:=val;
  1923. end;
  1924. {$ifdef ARM}
  1925. constructor tai_realconst.create_s64real_hiloswapped(val : ts64real);
  1926. begin
  1927. inherited create;
  1928. typ:=ait_realconst;
  1929. realtyp:=aitrealconst_s64bit;
  1930. value.s64val:=val;
  1931. savesize:=8;
  1932. formatoptions:=fo_hiloswapped;
  1933. end;
  1934. {$endif ARM}
  1935. constructor tai_realconst.create_s80real(val: ts80real; _savesize: byte);
  1936. begin
  1937. inherited create;
  1938. typ:=ait_realconst;
  1939. realtyp:=aitrealconst_s80bit;
  1940. savesize:=_savesize;
  1941. value.s80val:=val;
  1942. end;
  1943. constructor tai_realconst.create_s128real(val: ts128real);
  1944. begin
  1945. inherited create;
  1946. typ:=ait_realconst;
  1947. realtyp:=aitrealconst_s128bit;
  1948. savesize:=16;
  1949. value.s128val:=val;
  1950. end;
  1951. constructor tai_realconst.create_s64compreal(val: ts64comp);
  1952. begin
  1953. inherited create;
  1954. typ:=ait_realconst;
  1955. realtyp:=aitrealconst_s64comp;
  1956. savesize:=8;
  1957. value.s64compval:=val;
  1958. end;
  1959. constructor tai_realconst.ppuload(t: taitype; ppufile: tcompilerppufile);
  1960. begin
  1961. inherited;
  1962. realtyp:=tairealconsttype(ppufile.getbyte);
  1963. {$ifdef ARM}
  1964. formatoptions:=tformatoptions(ppufile.getbyte);
  1965. {$endif ARM}
  1966. case realtyp of
  1967. aitrealconst_s32bit:
  1968. value.s32val:=ppufile.getreal;
  1969. aitrealconst_s64bit:
  1970. value.s64val:=ppufile.getreal;
  1971. aitrealconst_s80bit:
  1972. value.s80val:=ppufile.getreal;
  1973. aitrealconst_s128bit:
  1974. value.s128val:=ppufile.getreal;
  1975. aitrealconst_s64comp:
  1976. value.s64compval:=comp(ppufile.getint64);
  1977. end;
  1978. end;
  1979. procedure tai_realconst.ppuwrite(ppufile: tcompilerppufile);
  1980. var
  1981. c: comp;
  1982. begin
  1983. inherited ppuwrite(ppufile);
  1984. ppufile.putbyte(byte(realtyp));
  1985. {$ifdef ARM}
  1986. ppufile.putbyte(byte(formatoptions));
  1987. {$endif ARM}
  1988. case realtyp of
  1989. aitrealconst_s32bit:
  1990. ppufile.putreal(value.s32val);
  1991. aitrealconst_s64bit:
  1992. ppufile.putreal(value.s64val);
  1993. aitrealconst_s80bit:
  1994. ppufile.putreal(value.s80val);
  1995. aitrealconst_s128bit:
  1996. ppufile.putreal(value.s128val);
  1997. aitrealconst_s64comp:
  1998. begin
  1999. c:=comp(value.s64compval);
  2000. ppufile.putint64(int64(c));
  2001. end
  2002. end;
  2003. end;
  2004. function tai_realconst.getcopy: tlinkedlistitem;
  2005. begin
  2006. result:=inherited getcopy;
  2007. tai_realconst(result).value:=value;
  2008. tai_realconst(result).realtyp:=realtyp;
  2009. tai_realconst(result).savesize:=savesize;
  2010. {$ifdef ARM}
  2011. tai_realconst(result).formatoptions:=formatoptions;
  2012. {$endif ARM}
  2013. end;
  2014. function tai_realconst.datasize: word;
  2015. begin
  2016. result:=0;
  2017. case realtyp of
  2018. aitrealconst_s32bit:
  2019. result:=4;
  2020. aitrealconst_s64bit,
  2021. aitrealconst_s64comp:
  2022. result:=8;
  2023. aitrealconst_s80bit:
  2024. result:=10;
  2025. aitrealconst_s128bit:
  2026. result:=16;
  2027. end;
  2028. end;
  2029. {****************************************************************************
  2030. TAI_STRING
  2031. ****************************************************************************}
  2032. constructor tai_string.Create(const _str : string);
  2033. begin
  2034. inherited Create;
  2035. typ:=ait_string;
  2036. len:=length(_str);
  2037. getmem(str,len+1);
  2038. move(_str[1],str^,len);
  2039. str[len]:=#0;
  2040. end;
  2041. constructor tai_string.Create_pchar(_str : pchar;length : longint);
  2042. begin
  2043. inherited Create;
  2044. typ:=ait_string;
  2045. str:=_str;
  2046. len:=length;
  2047. end;
  2048. destructor tai_string.destroy;
  2049. begin
  2050. if str<>nil then
  2051. freemem(str);
  2052. inherited Destroy;
  2053. end;
  2054. constructor tai_string.ppuload(t:taitype;ppufile:tcompilerppufile);
  2055. begin
  2056. inherited ppuload(t,ppufile);
  2057. len:=ppufile.getlongint;
  2058. getmem(str,len+1);
  2059. ppufile.getdata(str^,len);
  2060. str[len]:=#0
  2061. end;
  2062. procedure tai_string.ppuwrite(ppufile:tcompilerppufile);
  2063. begin
  2064. inherited ppuwrite(ppufile);
  2065. ppufile.putlongint(len);
  2066. ppufile.putdata(str^,len);
  2067. end;
  2068. function tai_string.getcopy : tlinkedlistitem;
  2069. var
  2070. p : tlinkedlistitem;
  2071. begin
  2072. p:=inherited getcopy;
  2073. getmem(tai_string(p).str,len);
  2074. move(str^,tai_string(p).str^,len);
  2075. getcopy:=p;
  2076. end;
  2077. {****************************************************************************
  2078. TAI_LABEL
  2079. ****************************************************************************}
  2080. constructor tai_label.Create(_labsym : tasmlabel);
  2081. begin
  2082. inherited Create;
  2083. typ:=ait_label;
  2084. labsym:=_labsym;
  2085. labsym.is_set:=true;
  2086. end;
  2087. constructor tai_label.ppuload(t:taitype;ppufile:tcompilerppufile);
  2088. begin
  2089. inherited ppuload(t,ppufile);
  2090. labsym:=tasmlabel(ppufile.getasmsymbol);
  2091. ppufile.getbyte; { was is_global flag, now unused }
  2092. end;
  2093. procedure tai_label.ppuwrite(ppufile:tcompilerppufile);
  2094. begin
  2095. inherited ppuwrite(ppufile);
  2096. ppufile.putasmsymbol(labsym);
  2097. ppufile.putbyte(0); { was is_global flag, now unused }
  2098. end;
  2099. procedure tai_label.derefimpl;
  2100. begin
  2101. labsym.is_set:=true;
  2102. end;
  2103. {****************************************************************************
  2104. tai_comment comment to be inserted in the assembler file
  2105. ****************************************************************************}
  2106. constructor tai_comment.Create(_str : pchar);
  2107. begin
  2108. inherited Create;
  2109. typ:=ait_comment;
  2110. str:=_str;
  2111. end;
  2112. destructor tai_comment.destroy;
  2113. begin
  2114. freemem(str);
  2115. inherited Destroy;
  2116. end;
  2117. constructor tai_comment.ppuload(t:taitype;ppufile:tcompilerppufile);
  2118. var
  2119. len : longint;
  2120. begin
  2121. inherited ppuload(t,ppufile);
  2122. len:=ppufile.getlongint;
  2123. getmem(str,len+1);
  2124. ppufile.getdata(str^,len);
  2125. str[len]:=#0;
  2126. end;
  2127. procedure tai_comment.ppuwrite(ppufile:tcompilerppufile);
  2128. var
  2129. len : longint;
  2130. begin
  2131. inherited ppuwrite(ppufile);
  2132. len:=strlen(str);
  2133. ppufile.putlongint(len);
  2134. ppufile.putdata(str^,len);
  2135. end;
  2136. function tai_comment.getcopy : tlinkedlistitem;
  2137. var
  2138. p : tlinkedlistitem;
  2139. begin
  2140. p:=inherited getcopy;
  2141. getmem(tai_comment(p).str,strlen(str)+1);
  2142. move(str^,tai_comment(p).str^,strlen(str)+1);
  2143. getcopy:=p;
  2144. end;
  2145. {****************************************************************************
  2146. TAI_STABS
  2147. ****************************************************************************}
  2148. constructor tai_stab.create(_stabtype:TStabType;_str : pchar);
  2149. begin
  2150. inherited create;
  2151. typ:=ait_stab;
  2152. str:=_str;
  2153. stabtype:=_stabtype;
  2154. end;
  2155. constructor tai_stab.create_str(_stabtype:TStabType;const s:string);
  2156. begin
  2157. self.create(_stabtype,strpnew(s));
  2158. end;
  2159. constructor tai_stab.create_ansistr(_stabtype:TStabType;const s:ansistring);
  2160. begin
  2161. inherited create;
  2162. typ:=ait_stab;
  2163. stabtype:=_stabtype;
  2164. getmem(str,length(s)+1);
  2165. if length(s)>0 then
  2166. move(s[1],str^,length(s)+1)
  2167. else
  2168. str^:=#0;
  2169. end;
  2170. destructor tai_stab.destroy;
  2171. begin
  2172. freemem(str);
  2173. inherited destroy;
  2174. end;
  2175. {****************************************************************************
  2176. TAI_FORCE_LINE
  2177. ****************************************************************************}
  2178. constructor tai_force_line.create;
  2179. begin
  2180. inherited create;
  2181. typ:=ait_force_line;
  2182. end;
  2183. {****************************************************************************
  2184. TAI_FUNCTION_NAME
  2185. ****************************************************************************}
  2186. constructor tai_function_name.create(const s:string);
  2187. begin
  2188. inherited create;
  2189. typ:=ait_function_name;
  2190. funcname:=stringdup(s);
  2191. end;
  2192. destructor tai_function_name.destroy;
  2193. begin
  2194. stringdispose(funcname);
  2195. inherited destroy;
  2196. end;
  2197. {****************************************************************************
  2198. TAI_CUTOBJECT
  2199. ****************************************************************************}
  2200. constructor tai_cutobject.Create;
  2201. begin
  2202. inherited Create;
  2203. typ:=ait_cutobject;
  2204. place:=cut_normal;
  2205. end;
  2206. constructor tai_cutobject.Create_begin;
  2207. begin
  2208. inherited Create;
  2209. typ:=ait_cutobject;
  2210. place:=cut_begin;
  2211. end;
  2212. constructor tai_cutobject.Create_end;
  2213. begin
  2214. inherited Create;
  2215. typ:=ait_cutobject;
  2216. place:=cut_end;
  2217. end;
  2218. constructor tai_cutobject.ppuload(t:taitype;ppufile:tcompilerppufile);
  2219. begin
  2220. inherited ppuload(t,ppufile);
  2221. place:=TCutPlace(ppufile.getbyte);
  2222. end;
  2223. procedure tai_cutobject.ppuwrite(ppufile:tcompilerppufile);
  2224. begin
  2225. inherited ppuwrite(ppufile);
  2226. ppufile.putbyte(byte(place));
  2227. end;
  2228. {****************************************************************************
  2229. Tai_Marker
  2230. ****************************************************************************}
  2231. constructor Tai_Marker.Create(_Kind: TAsmMarker);
  2232. begin
  2233. Inherited Create;
  2234. typ := ait_marker;
  2235. Kind := _Kind;
  2236. end;
  2237. constructor Tai_Marker.ppuload(t:taitype;ppufile:tcompilerppufile);
  2238. begin
  2239. inherited ppuload(t,ppufile);
  2240. kind:=TAsmMarker(ppufile.getbyte);
  2241. end;
  2242. procedure Tai_Marker.ppuwrite(ppufile:tcompilerppufile);
  2243. begin
  2244. inherited ppuwrite(ppufile);
  2245. ppufile.putbyte(byte(kind));
  2246. end;
  2247. {*****************************************************************************
  2248. tai_tempalloc
  2249. *****************************************************************************}
  2250. constructor tai_tempalloc.alloc(pos,size:longint);
  2251. begin
  2252. inherited Create;
  2253. typ:=ait_tempalloc;
  2254. allocation:=true;
  2255. temppos:=pos;
  2256. tempsize:=size;
  2257. {$ifdef EXTDEBUG}
  2258. problem:=nil;
  2259. {$endif EXTDEBUG}
  2260. end;
  2261. destructor tai_tempalloc.destroy;
  2262. begin
  2263. {$ifdef EXTDEBUG}
  2264. stringdispose(problem);
  2265. {$endif EXTDEBUG}
  2266. inherited destroy;
  2267. end;
  2268. constructor tai_tempalloc.dealloc(pos,size:longint);
  2269. begin
  2270. inherited Create;
  2271. typ:=ait_tempalloc;
  2272. allocation:=false;
  2273. temppos:=pos;
  2274. tempsize:=size;
  2275. {$ifdef EXTDEBUG}
  2276. problem:=nil;
  2277. {$endif EXTDEBUG}
  2278. end;
  2279. {$ifdef EXTDEBUG}
  2280. constructor tai_tempalloc.allocinfo(pos,size:longint;const st:string);
  2281. begin
  2282. inherited Create;
  2283. typ:=ait_tempalloc;
  2284. allocation:=false;
  2285. temppos:=pos;
  2286. tempsize:=size;
  2287. problem:=stringdup(st);
  2288. end;
  2289. {$endif EXTDEBUG}
  2290. constructor tai_tempalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  2291. begin
  2292. inherited ppuload(t,ppufile);
  2293. temppos:=ppufile.getlongint;
  2294. tempsize:=ppufile.getlongint;
  2295. allocation:=ppufile.getboolean;
  2296. {$ifdef EXTDEBUG}
  2297. problem:=nil;
  2298. {$endif EXTDEBUG}
  2299. end;
  2300. procedure tai_tempalloc.ppuwrite(ppufile:tcompilerppufile);
  2301. begin
  2302. inherited ppuwrite(ppufile);
  2303. ppufile.putlongint(temppos);
  2304. ppufile.putlongint(tempsize);
  2305. ppufile.putboolean(allocation);
  2306. end;
  2307. {*****************************************************************************
  2308. tai_regalloc
  2309. *****************************************************************************}
  2310. constructor tai_regalloc.alloc(r : tregister;ainstr:tai);
  2311. begin
  2312. inherited create;
  2313. typ:=ait_regalloc;
  2314. ratype:=ra_alloc;
  2315. reg:=r;
  2316. { ainstr must be an instruction }
  2317. if assigned(ainstr) and
  2318. (ainstr.typ<>ait_instruction) then
  2319. internalerror(200411011);
  2320. instr:=ainstr;
  2321. end;
  2322. constructor tai_regalloc.dealloc(r : tregister;ainstr:tai);
  2323. begin
  2324. inherited create;
  2325. typ:=ait_regalloc;
  2326. ratype:=ra_dealloc;
  2327. reg:=r;
  2328. { ainstr must be an instruction }
  2329. if assigned(ainstr) and
  2330. (ainstr.typ<>ait_instruction) then
  2331. internalerror(200411012);
  2332. instr:=ainstr;
  2333. end;
  2334. constructor tai_regalloc.sync(r : tregister);
  2335. begin
  2336. inherited create;
  2337. typ:=ait_regalloc;
  2338. ratype:=ra_sync;
  2339. reg:=r;
  2340. end;
  2341. constructor tai_regalloc.resize(r : tregister);
  2342. begin
  2343. inherited create;
  2344. typ:=ait_regalloc;
  2345. ratype:=ra_resize;
  2346. reg:=r;
  2347. end;
  2348. constructor tai_regalloc.markused(r : tregister);
  2349. begin
  2350. inherited create;
  2351. typ:=ait_regalloc;
  2352. ratype:=ra_markused;
  2353. reg:=r;
  2354. end;
  2355. constructor tai_regalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  2356. begin
  2357. inherited ppuload(t,ppufile);
  2358. ppufile.getdata(reg,sizeof(Tregister));
  2359. ratype:=tregalloctype(ppufile.getbyte);
  2360. keep:=ppufile.getboolean;
  2361. end;
  2362. procedure tai_regalloc.ppuwrite(ppufile:tcompilerppufile);
  2363. begin
  2364. inherited ppuwrite(ppufile);
  2365. ppufile.putdata(reg,sizeof(Tregister));
  2366. ppufile.putbyte(byte(ratype));
  2367. ppufile.putboolean(keep);
  2368. end;
  2369. {*****************************************************************************
  2370. TaiInstruction
  2371. *****************************************************************************}
  2372. constructor tai_cpu_abstract.Create(op : tasmop);
  2373. begin
  2374. inherited create;
  2375. typ:=ait_instruction;
  2376. is_jmp:=false;
  2377. opcode:=op;
  2378. ops:=0;
  2379. end;
  2380. destructor tai_cpu_abstract.Destroy;
  2381. var
  2382. i : integer;
  2383. begin
  2384. for i:=0 to opercnt-1 do
  2385. freeop(i);
  2386. inherited destroy;
  2387. end;
  2388. { ---------------------------------------------------------------------
  2389. Loading of operands.
  2390. ---------------------------------------------------------------------}
  2391. procedure tai_cpu_abstract.allocate_oper(opers:longint);
  2392. begin
  2393. while (opers>opercnt) do
  2394. begin
  2395. new(oper[opercnt]);
  2396. fillchar(oper[opercnt]^,sizeof(toper),0);
  2397. inc(opercnt);
  2398. end;
  2399. end;
  2400. procedure tai_cpu_abstract.loadconst(opidx:longint;l:tcgint);
  2401. begin
  2402. allocate_oper(opidx+1);
  2403. with oper[opidx]^ do
  2404. begin
  2405. if typ<>top_const then
  2406. clearop(opidx);
  2407. val:=l;
  2408. typ:=top_const;
  2409. end;
  2410. end;
  2411. procedure tai_cpu_abstract.loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  2412. var
  2413. r : treference;
  2414. begin
  2415. reference_reset_symbol(r,s,sofs,1,[]);
  2416. r.refaddr:=addr_full;
  2417. loadref(opidx,r);
  2418. end;
  2419. procedure tai_cpu_abstract.loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  2420. begin
  2421. if not assigned(s) then
  2422. internalerror(200204251);
  2423. allocate_oper(opidx+1);
  2424. with oper[opidx]^ do
  2425. begin
  2426. if typ<>top_local then
  2427. begin
  2428. clearop(opidx);
  2429. new(localoper);
  2430. end;
  2431. with oper[opidx]^.localoper^ do
  2432. begin
  2433. localsym:=s;
  2434. localsymofs:=sofs;
  2435. localindexreg:=indexreg;
  2436. localscale:=scale;
  2437. localgetoffset:=getoffset;
  2438. localforceref:=forceref;
  2439. {$ifdef x86}
  2440. localsegment:=NR_NO;
  2441. {$endif x86}
  2442. end;
  2443. typ:=top_local;
  2444. end;
  2445. end;
  2446. procedure tai_cpu_abstract.loadref(opidx:longint;const r:treference);
  2447. {$ifdef x86}
  2448. var
  2449. si_param: ShortInt;
  2450. {$endif}
  2451. begin
  2452. allocate_oper(opidx+1);
  2453. with oper[opidx]^ do
  2454. begin
  2455. if typ<>top_ref then
  2456. begin
  2457. clearop(opidx);
  2458. new(ref);
  2459. end;
  2460. ref^:=r;
  2461. {$ifdef x86}
  2462. { We allow this exception for x86, since overloading this would be
  2463. too much of a a speed penalty}
  2464. if is_x86_parameterized_string_op(opcode) then
  2465. begin
  2466. si_param:=get_x86_string_op_si_param(opcode);
  2467. if (si_param<>-1) and (taicpu(self).OperandOrder=op_att) then
  2468. si_param:=x86_parameterized_string_op_param_count(opcode)-si_param-1;
  2469. if (si_param=opidx) and (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  2470. segprefix:=ref^.segment;
  2471. end
  2472. else if (opcode=A_XLAT) and (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  2473. segprefix:=ref^.segment
  2474. else if (ref^.segment<>NR_NO) and (ref^.segment<>get_default_segment_of_ref(ref^)) then
  2475. segprefix:=ref^.segment;
  2476. {$endif}
  2477. {$ifndef llvm}
  2478. if (cs_create_pic in current_settings.moduleswitches) and
  2479. assigned(r.symbol) and
  2480. not assigned(r.relsymbol) and
  2481. (r.refaddr=addr_no)
  2482. {$ifdef ARM}
  2483. and not(r.base=NR_R15)
  2484. {$endif ARM}
  2485. {$ifdef aarch64}
  2486. and not(r.refaddr in [addr_full,addr_gotpageoffset,addr_gotpage])
  2487. {$endif aarch64}
  2488. then
  2489. internalerror(200502052);
  2490. {$endif not llvm}
  2491. typ:=top_ref;
  2492. if assigned(add_reg_instruction_hook) then
  2493. begin
  2494. add_reg_instruction_hook(self,ref^.base);
  2495. add_reg_instruction_hook(self,ref^.index);
  2496. end;
  2497. { mark symbol as used }
  2498. if assigned(ref^.symbol) then
  2499. ref^.symbol.increfs;
  2500. if assigned(ref^.relsymbol) then
  2501. ref^.relsymbol.increfs;
  2502. end;
  2503. end;
  2504. procedure tai_cpu_abstract.loadreg(opidx:longint;r:tregister);
  2505. begin
  2506. allocate_oper(opidx+1);
  2507. with oper[opidx]^ do
  2508. begin
  2509. if typ<>top_reg then
  2510. clearop(opidx);
  2511. reg:=r;
  2512. typ:=top_reg;
  2513. end;
  2514. if assigned(add_reg_instruction_hook) then
  2515. add_reg_instruction_hook(self,r);
  2516. {$ifdef ARM}
  2517. { R15 is the PC on the ARM thus moves to R15 are jumps.
  2518. Due to speed considerations we don't use a virtual overridden method here.
  2519. Because the pc/r15 isn't handled by the reg. allocator this should never cause
  2520. problems with iregs getting r15.
  2521. }
  2522. is_jmp:=(opcode=A_MOV) and (opidx=0) and (r=NR_R15);
  2523. {$endif ARM}
  2524. end;
  2525. procedure tai_cpu_abstract.loadoper(opidx:longint;o:toper);
  2526. {$ifdef x86}
  2527. var
  2528. si_param: ShortInt;
  2529. {$endif x86}
  2530. begin
  2531. allocate_oper(opidx+1);
  2532. clearop(opidx);
  2533. oper[opidx]^:=o;
  2534. { copy also the reference }
  2535. with oper[opidx]^ do
  2536. begin
  2537. case typ of
  2538. top_reg:
  2539. begin
  2540. if assigned(add_reg_instruction_hook) then
  2541. add_reg_instruction_hook(self,reg);
  2542. end;
  2543. top_ref:
  2544. begin
  2545. new(ref);
  2546. ref^:=o.ref^;
  2547. {$ifdef x86}
  2548. { We allow this exception for x86, since overloading this would be
  2549. too much of a a speed penalty}
  2550. if is_x86_parameterized_string_op(opcode) then
  2551. begin
  2552. si_param:=get_x86_string_op_si_param(opcode);
  2553. if (si_param<>-1) and (taicpu(self).OperandOrder=op_att) then
  2554. si_param:=x86_parameterized_string_op_param_count(opcode)-si_param-1;
  2555. if (si_param=opidx) and (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  2556. segprefix:=ref^.segment;
  2557. end
  2558. else if (opcode=A_XLAT) and (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  2559. segprefix:=ref^.segment
  2560. else if (ref^.segment<>NR_NO) and (ref^.segment<>get_default_segment_of_ref(ref^)) then
  2561. segprefix:=ref^.segment;
  2562. {$endif x86}
  2563. if assigned(add_reg_instruction_hook) then
  2564. begin
  2565. add_reg_instruction_hook(self,ref^.base);
  2566. add_reg_instruction_hook(self,ref^.index);
  2567. end;
  2568. end;
  2569. {$ifdef ARM}
  2570. top_shifterop:
  2571. begin
  2572. new(shifterop);
  2573. shifterop^:=o.shifterop^;
  2574. if assigned(add_reg_instruction_hook) then
  2575. add_reg_instruction_hook(self,shifterop^.rs);
  2576. end;
  2577. {$endif ARM}
  2578. else
  2579. ;
  2580. end;
  2581. end;
  2582. end;
  2583. procedure tai_cpu_abstract.clearop(opidx:longint);
  2584. begin
  2585. with oper[opidx]^ do
  2586. begin
  2587. case typ of
  2588. top_ref:
  2589. dispose(ref);
  2590. top_local:
  2591. dispose(localoper);
  2592. {$ifdef ARM}
  2593. top_shifterop:
  2594. dispose(shifterop);
  2595. top_regset:
  2596. dispose(regset);
  2597. {$endif ARM}
  2598. {$ifdef jvm}
  2599. top_string:
  2600. freemem(pcval);
  2601. top_wstring:
  2602. donewidestring(pwstrval);
  2603. {$endif jvm}
  2604. {$ifdef wasm}
  2605. top_functype:
  2606. FreeAndNil(functype);
  2607. {$endif wasm}
  2608. else
  2609. ;
  2610. end;
  2611. typ:=top_none;
  2612. end;
  2613. end;
  2614. procedure tai_cpu_abstract.freeop(opidx:longint);
  2615. begin
  2616. clearop(opidx);
  2617. dispose(oper[opidx]);
  2618. end;
  2619. { ---------------------------------------------------------------------
  2620. Miscellaneous methods.
  2621. ---------------------------------------------------------------------}
  2622. procedure tai_cpu_abstract.SetCondition(const c:TAsmCond);
  2623. begin
  2624. condition:=c;
  2625. end;
  2626. Function tai_cpu_abstract.getcopy:TLinkedListItem;
  2627. var
  2628. i : longint;
  2629. p : tai_cpu_abstract;
  2630. begin
  2631. p:=tai_cpu_abstract(inherited getcopy);
  2632. { make a copy of the references }
  2633. p.opercnt:=0;
  2634. p.allocate_oper(ops);
  2635. for i:=0 to ops-1 do
  2636. begin
  2637. p.oper[i]^:=oper[i]^;
  2638. case oper[i]^.typ of
  2639. top_local :
  2640. begin
  2641. new(p.oper[i]^.localoper);
  2642. p.oper[i]^.localoper^:=oper[i]^.localoper^;
  2643. end;
  2644. top_ref :
  2645. begin
  2646. new(p.oper[i]^.ref);
  2647. p.oper[i]^.ref^:=oper[i]^.ref^;
  2648. end;
  2649. {$ifdef ARM}
  2650. top_shifterop:
  2651. begin
  2652. new(p.oper[i]^.shifterop);
  2653. p.oper[i]^.shifterop^:=oper[i]^.shifterop^;
  2654. end;
  2655. {$endif ARM}
  2656. else
  2657. ;
  2658. end;
  2659. end;
  2660. getcopy:=p;
  2661. end;
  2662. function tai_cpu_abstract.is_same_reg_move(regtype: Tregistertype):boolean;
  2663. begin
  2664. { When the generic RA is used this needs to be overridden, we don't use
  2665. virtual;abstract; to prevent a lot of warnings of unimplemented abstract methods
  2666. when tai_cpu is created (PFV) }
  2667. internalerror(2004040901);
  2668. result:=false;
  2669. end;
  2670. function tai_cpu_abstract.spilling_get_operation_type(opnr: longint): topertype;
  2671. begin
  2672. internalerror(2004040902);
  2673. result:=operand_readwrite;
  2674. end;
  2675. function tai_cpu_abstract.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  2676. begin
  2677. result := operand_read;
  2678. end;
  2679. constructor tai_cpu_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  2680. var
  2681. i : integer;
  2682. begin
  2683. inherited ppuload(t,ppufile);
  2684. { hopefully, we don't get problems with big/little endian here when cross compiling :/ }
  2685. ppufile.getdata(condition,sizeof(tasmcond));
  2686. ops := ppufile.getbyte;
  2687. allocate_oper(ops);
  2688. for i:=0 to ops-1 do
  2689. ppuloadoper(ppufile,oper[i]^);
  2690. opcode:=tasmop(ppufile.getword);
  2691. {$ifdef x86}
  2692. ppufile.getdata(segprefix,sizeof(Tregister));
  2693. {$endif x86}
  2694. is_jmp:=ppufile.getboolean;
  2695. end;
  2696. procedure tai_cpu_abstract.ppuwrite(ppufile:tcompilerppufile);
  2697. var
  2698. i : integer;
  2699. begin
  2700. inherited ppuwrite(ppufile);
  2701. ppufile.putdata(condition,sizeof(tasmcond));
  2702. ppufile.putbyte(ops);
  2703. for i:=0 to ops-1 do
  2704. ppuwriteoper(ppufile,oper[i]^);
  2705. ppufile.putword(word(opcode));
  2706. {$ifdef x86}
  2707. ppufile.putdata(segprefix,sizeof(Tregister));
  2708. {$endif x86}
  2709. ppufile.putboolean(is_jmp);
  2710. end;
  2711. procedure tai_cpu_abstract.buildderefimpl;
  2712. var
  2713. i : integer;
  2714. begin
  2715. for i:=0 to ops-1 do
  2716. ppubuildderefimploper(oper[i]^);
  2717. end;
  2718. procedure tai_cpu_abstract.derefimpl;
  2719. var
  2720. i : integer;
  2721. begin
  2722. for i:=0 to ops-1 do
  2723. ppuderefoper(oper[i]^);
  2724. end;
  2725. procedure tai_cpu_abstract.resetpass1;
  2726. begin
  2727. end;
  2728. procedure tai_cpu_abstract.resetpass2;
  2729. begin
  2730. end;
  2731. function tai_cpu_abstract.Pass1(objdata:TObjData):longint;
  2732. begin
  2733. result:=0;
  2734. end;
  2735. procedure tai_cpu_abstract.Pass2(objdata:TObjData);
  2736. begin
  2737. end;
  2738. procedure tai_cpu_abstract.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  2739. begin
  2740. o.typ:=toptype(ppufile.getbyte);
  2741. o.ot:=ppufile.getlongint;
  2742. case o.typ of
  2743. top_reg :
  2744. ppufile.getdata(o.reg,sizeof(Tregister));
  2745. top_ref :
  2746. begin
  2747. new(o.ref);
  2748. {$ifdef x86}
  2749. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  2750. {$endif x86}
  2751. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  2752. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  2753. ppufile.getdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  2754. o.ref^.scalefactor:=ppufile.getbyte;
  2755. o.ref^.offset:=ppufile.getaint;
  2756. o.ref^.symbol:=ppufile.getasmsymbol;
  2757. o.ref^.relsymbol:=ppufile.getasmsymbol;
  2758. end;
  2759. top_const :
  2760. o.val:=ppufile.getaint;
  2761. top_local :
  2762. begin
  2763. new(o.localoper);
  2764. with o.localoper^ do
  2765. begin
  2766. ppufile.getderef(localsymderef);
  2767. localsymofs:=ppufile.getaint;
  2768. {$ifdef x86}
  2769. localsegment:=tregister(ppufile.getlongint);
  2770. {$endif x86}
  2771. localindexreg:=tregister(ppufile.getlongint);
  2772. localscale:=ppufile.getbyte;
  2773. localgetoffset:=(ppufile.getbyte<>0);
  2774. end;
  2775. end;
  2776. else
  2777. internalerror(2007010210);
  2778. end;
  2779. end;
  2780. procedure tai_cpu_abstract.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  2781. begin
  2782. ppufile.putbyte(byte(o.typ));
  2783. ppufile.putlongint(o.ot);
  2784. case o.typ of
  2785. top_reg :
  2786. ppufile.putdata(o.reg,sizeof(Tregister));
  2787. top_ref :
  2788. begin
  2789. {$ifdef x86}
  2790. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  2791. {$endif x86}
  2792. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  2793. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  2794. ppufile.putdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  2795. ppufile.putbyte(o.ref^.scalefactor);
  2796. ppufile.putaint(o.ref^.offset);
  2797. ppufile.putasmsymbol(o.ref^.symbol);
  2798. ppufile.putasmsymbol(o.ref^.relsymbol);
  2799. end;
  2800. top_const :
  2801. ppufile.putaint(o.val);
  2802. top_local :
  2803. begin
  2804. with o.localoper^ do
  2805. begin
  2806. ppufile.putderef(localsymderef);
  2807. ppufile.putaint(localsymofs);
  2808. {$ifdef x86}
  2809. ppufile.putlongint(longint(localsegment));
  2810. {$endif x86}
  2811. ppufile.putlongint(longint(localindexreg));
  2812. ppufile.putbyte(localscale);
  2813. ppufile.putbyte(byte(localgetoffset));
  2814. end;
  2815. end;
  2816. else
  2817. internalerror(2007010211);
  2818. end;
  2819. end;
  2820. {****************************************************************************
  2821. tai_align_abstract
  2822. ****************************************************************************}
  2823. constructor tai_align_abstract.Create(b: byte);
  2824. begin
  2825. inherited Create;
  2826. typ:=ait_align;
  2827. {$ifdef EXTDEBUG}
  2828. if upper(classname)='TAI_ALIGN_ABSTRACT' then
  2829. internalerror(200709191);
  2830. {$endif EXTDEBUG}
  2831. if b in [1,2,4,8,16,32,64] then
  2832. aligntype := b
  2833. else
  2834. aligntype := 1;
  2835. fillsize:=0;
  2836. fillop:=0;
  2837. use_op:=false;
  2838. maxbytes:=aligntype;
  2839. end;
  2840. constructor tai_align_abstract.Create_op(b: byte; _op: byte);
  2841. begin
  2842. inherited Create;
  2843. typ:=ait_align;
  2844. if b in [1,2,4,8,16,32] then
  2845. aligntype := b
  2846. else
  2847. aligntype := 1;
  2848. fillsize:=0;
  2849. fillop:=_op;
  2850. use_op:=true;
  2851. maxbytes:=aligntype;
  2852. end;
  2853. constructor tai_align_abstract.create_max(b : byte; max : byte);
  2854. begin
  2855. inherited Create;
  2856. typ:=ait_align;
  2857. if b in [1,2,4,8,16,32] then
  2858. aligntype := b
  2859. else
  2860. aligntype := 1;
  2861. maxbytes:=max;
  2862. fillsize:=0;
  2863. fillop:=0;
  2864. use_op:=false;
  2865. end;
  2866. constructor tai_align_abstract.Create_zeros(b: byte);
  2867. begin
  2868. inherited Create;
  2869. typ:=ait_align;
  2870. if b in [1,2,4,8,16,32] then
  2871. aligntype := b
  2872. else
  2873. aligntype := 1;
  2874. use_op:=true;
  2875. fillsize:=0;
  2876. fillop:=0;
  2877. maxbytes:=aligntype;
  2878. end;
  2879. function tai_align_abstract.calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;
  2880. begin
  2881. if fillsize>sizeof(buf) then
  2882. internalerror(200404293);
  2883. fillchar(buf,high(buf),fillop);
  2884. calculatefillbuf:=pchar(@buf);
  2885. end;
  2886. constructor tai_align_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  2887. begin
  2888. inherited ppuload(t,ppufile);
  2889. aligntype:=ppufile.getbyte;
  2890. fillsize:=0;
  2891. fillop:=ppufile.getbyte;
  2892. use_op:=ppufile.getboolean;
  2893. maxbytes:=ppufile.getbyte;
  2894. end;
  2895. procedure tai_align_abstract.ppuwrite(ppufile:tcompilerppufile);
  2896. begin
  2897. inherited ppuwrite(ppufile);
  2898. ppufile.putbyte(aligntype);
  2899. ppufile.putbyte(fillop);
  2900. ppufile.putboolean(use_op);
  2901. ppufile.putbyte(maxbytes);
  2902. end;
  2903. {****************************************************************************
  2904. tai_seh_directive
  2905. ****************************************************************************}
  2906. const
  2907. datatypemap: array[TAsmSehDirective] of TSehDirectiveDatatype=(
  2908. sd_string, { proc }
  2909. sd_none, { endproc }
  2910. sd_none, { endprologue }
  2911. sd_string, { handler }
  2912. sd_none, { handlerdata }
  2913. sd_none,sd_none,sd_none, { eh, 32, no32 }
  2914. sd_regoffset, { setframe }
  2915. sd_offset, { stackalloc }
  2916. sd_reg, { pushreg }
  2917. sd_regoffset, { savereg }
  2918. sd_regoffset, { savereg_x }
  2919. sd_regoffset, { saveregp }
  2920. sd_regoffset, { saveregp_x }
  2921. sd_regoffset, { savexmm }
  2922. sd_regoffset, { savefreg }
  2923. sd_regoffset, { savefreg_x }
  2924. sd_regoffset, { savefregp }
  2925. sd_regoffset, { savefregp_x }
  2926. sd_none, { pushframe }
  2927. sd_none, { setfp }
  2928. sd_none, { addfp }
  2929. sd_offset, { savefplr }
  2930. sd_offset, { savefplr_x }
  2931. sd_none, { nop }
  2932. sd_reg, { pushnv }
  2933. sd_none { savenv }
  2934. );
  2935. constructor tai_seh_directive.create(_kind:TAsmSehDirective);
  2936. begin
  2937. inherited Create;
  2938. typ:=ait_seh_directive;
  2939. kind:=_kind;
  2940. data.typ:=datatypemap[_kind];
  2941. end;
  2942. constructor tai_seh_directive.create_name(_kind:TAsmSehDirective;const _name:string);
  2943. begin
  2944. create(_kind);
  2945. data.name:=stringdup(_name);
  2946. end;
  2947. constructor tai_seh_directive.create_reg(_kind:TAsmSehDirective;r:TRegister);
  2948. begin
  2949. create(_kind);
  2950. data.reg:=r;
  2951. end;
  2952. constructor tai_seh_directive.create_offset(_kind:TAsmSehDirective;ofs:dword);
  2953. begin
  2954. create(_kind);
  2955. data.offset:=ofs;
  2956. end;
  2957. constructor tai_seh_directive.create_reg_offset(_kind:TAsmSehDirective;
  2958. r:TRegister;ofs:dword);
  2959. begin
  2960. create(_kind);
  2961. data.offset:=ofs;
  2962. data.reg:=r;
  2963. end;
  2964. constructor tai_seh_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  2965. begin
  2966. inherited ppuload(t, ppufile);
  2967. kind:=TAsmSehDirective(ppufile.getbyte);
  2968. data.typ:=datatypemap[kind];
  2969. case data.typ of
  2970. sd_none: ;
  2971. sd_string:
  2972. begin
  2973. data.name:=ppufile.getpshortstring;
  2974. data.flags:=ppufile.getbyte;
  2975. end;
  2976. sd_reg,sd_offset,sd_regoffset:
  2977. begin
  2978. ppufile.getdata(data.reg,sizeof(TRegister));
  2979. data.offset:=ppufile.getdword;
  2980. end;
  2981. end;
  2982. end;
  2983. destructor tai_seh_directive.destroy;
  2984. begin
  2985. if data.typ=sd_string then
  2986. stringdispose(data.name);
  2987. inherited destroy;
  2988. end;
  2989. procedure tai_seh_directive.ppuwrite(ppufile:tcompilerppufile);
  2990. begin
  2991. inherited ppuwrite(ppufile);
  2992. ppufile.putbyte(ord(kind));
  2993. case data.typ of
  2994. sd_none: ;
  2995. sd_string:
  2996. begin
  2997. ppufile.putstring(data.name^);
  2998. ppufile.putbyte(data.flags);
  2999. end;
  3000. sd_reg,sd_offset,sd_regoffset:
  3001. begin
  3002. ppufile.putdata(data.reg,sizeof(TRegister));
  3003. ppufile.putdword(data.offset);
  3004. end;
  3005. end;
  3006. end;
  3007. procedure tai_seh_directive.generate_code(objdata:TObjData);
  3008. begin
  3009. end;
  3010. {****************************************************************************
  3011. tai_eabi_attribute
  3012. ****************************************************************************}
  3013. constructor tai_eabi_attribute.create(atag,avalue : dword);
  3014. begin
  3015. inherited Create;
  3016. typ:=ait_eabi_attribute;
  3017. eattr_typ:=eattrtype_dword;
  3018. tag:=atag;
  3019. value:=avalue;
  3020. end;
  3021. constructor tai_eabi_attribute.create(atag: dword; const avalue: string);
  3022. begin
  3023. inherited Create;
  3024. typ:=ait_eabi_attribute;
  3025. eattr_typ:=eattrtype_ntbs;
  3026. tag:=atag;
  3027. valuestr:=NewStr(avalue);
  3028. end;
  3029. destructor tai_eabi_attribute.destroy;
  3030. begin
  3031. Inherited Destroy;
  3032. end;
  3033. constructor tai_eabi_attribute.ppuload(t:taitype;ppufile:tcompilerppufile);
  3034. begin
  3035. end;
  3036. procedure tai_eabi_attribute.ppuwrite(ppufile:tcompilerppufile);
  3037. begin
  3038. inherited ppuwrite(ppufile);
  3039. ppufile.putdword(tag);
  3040. ppufile.putdword(value);
  3041. end;
  3042. {$ifdef JVM}
  3043. {****************************************************************************
  3044. tai_jvar
  3045. ****************************************************************************}
  3046. constructor tai_jvar.Create(_stackslot: longint; const _desc: shortstring; _startlab, _stoplab: TAsmSymbol);
  3047. begin
  3048. Inherited create;
  3049. typ:=ait_jvar;
  3050. stackslot:=_stackslot;
  3051. desc:=stringdup(_desc);
  3052. startlab:=_startlab;
  3053. stoplab:=_stoplab;
  3054. end;
  3055. constructor tai_jvar.ppuload(t: taitype; ppufile: tcompilerppufile);
  3056. begin
  3057. inherited ppuload(t, ppufile);
  3058. stackslot:=ppufile.getlongint;
  3059. desc:=ppufile.getpshortstring;
  3060. startlab:=ppufile.getasmsymbol;
  3061. stoplab:=ppufile.getasmsymbol;
  3062. end;
  3063. procedure tai_jvar.ppuwrite(ppufile: tcompilerppufile);
  3064. begin
  3065. inherited ppuwrite(ppufile);
  3066. ppufile.putlongint(stackslot);
  3067. ppufile.putstring(desc^);
  3068. ppufile.putasmsymbol(startlab);
  3069. ppufile.putasmsymbol(stoplab);
  3070. end;
  3071. destructor tai_jvar.destroy;
  3072. begin
  3073. stringdispose(desc);
  3074. inherited destroy;
  3075. end;
  3076. {****************************************************************************
  3077. tai_jcatch
  3078. ****************************************************************************}
  3079. constructor tai_jcatch.Create(const _name: shortstring; _startlab, _stoplab, _handlerlab: TAsmSymbol);
  3080. begin
  3081. Inherited create;
  3082. typ:=ait_jcatch;
  3083. name:=stringdup(_name);
  3084. startlab:=_startlab;
  3085. startlab.increfs;
  3086. stoplab:=_stoplab;
  3087. stoplab.increfs;
  3088. handlerlab:=_handlerlab;
  3089. handlerlab.increfs;
  3090. end;
  3091. destructor tai_jcatch.destroy;
  3092. begin
  3093. stringdispose(name);
  3094. inherited destroy;
  3095. end;
  3096. constructor tai_jcatch.ppuload(t: taitype; ppufile: tcompilerppufile);
  3097. begin
  3098. inherited ppuload(t, ppufile);
  3099. name:=ppufile.getpshortstring;
  3100. startlab:=ppufile.getasmsymbol;
  3101. startlab.increfs;
  3102. stoplab:=ppufile.getasmsymbol;
  3103. stoplab.increfs;
  3104. handlerlab:=ppufile.getasmsymbol;
  3105. handlerlab.increfs;
  3106. end;
  3107. procedure tai_jcatch.ppuwrite(ppufile: tcompilerppufile);
  3108. begin
  3109. inherited ppuwrite(ppufile);
  3110. ppufile.putstring(name^);
  3111. ppufile.putasmsymbol(startlab);
  3112. ppufile.putasmsymbol(stoplab);
  3113. ppufile.putasmsymbol(handlerlab);
  3114. end;
  3115. {$endif JVM}
  3116. begin
  3117. {$push}{$warnings off}
  3118. { taitype should fit into a 4 byte set for speed reasons }
  3119. if ord(high(taitype))>31 then
  3120. internalerror(201108181);
  3121. {$pop}
  3122. end.