aasmtai.pas 111 KB

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