aasmtai.pas 94 KB

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