aasmtai.pas 95 KB

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