aasmtai.pas 112 KB

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