2
0

aasmtai.pas 111 KB

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