aasmtai.pas 110 KB

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