aasmtai.pas 97 KB

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