aasmtai.pas 97 KB

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