aasmtai.pas 92 KB

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