aasmtai.pas 113 KB

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