aasmtai.pas 94 KB

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