aasmtai.pas 95 KB

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