aasmtai.pas 94 KB

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