aasmtai.pas 111 KB

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