aasmtai.pas 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563
  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. cgbase,cgutils,
  30. symtype,
  31. aasmbase,aasmdata,ogbase
  32. {$ifdef jvm}
  33. ,widestr
  34. {$endif jvm}
  35. ;
  36. type
  37. { keep the number of elements in this enumeration less or equal than 32 as long
  38. as FPC knows only 4 byte and 32 byte sets (FK) }
  39. taitype = (
  40. ait_none,
  41. ait_align,
  42. ait_section,
  43. ait_comment,
  44. ait_string,
  45. ait_instruction,
  46. ait_datablock,
  47. ait_symbol,
  48. { needed to calc the size of a symbol }
  49. ait_symbol_end,
  50. ait_directive,
  51. ait_label,
  52. ait_const,
  53. ait_real_32bit,
  54. ait_real_64bit,
  55. ait_real_80bit,
  56. ait_comp_64bit,
  57. ait_real_128bit,
  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. ait_ent,
  65. {$endif alpha}
  66. {$ifdef ia64}
  67. ait_bundle,
  68. ait_stop,
  69. {$endif ia64}
  70. {$ifdef m68k}
  71. ait_labeled_instruction,
  72. {$endif m68k}
  73. {$ifdef arm}
  74. ait_thumb_func,
  75. {$endif arm}
  76. { used to split into tiny assembler files }
  77. ait_cutobject,
  78. ait_regalloc,
  79. ait_tempalloc,
  80. { used to mark assembler blocks and inlined functions }
  81. ait_marker,
  82. { JVM only }
  83. ait_jvar, { debug information for a local variable }
  84. ait_jcatch { exception catch clause }
  85. );
  86. taiconst_type = (
  87. aitconst_128bit,
  88. aitconst_64bit,
  89. aitconst_32bit,
  90. aitconst_16bit,
  91. aitconst_8bit,
  92. aitconst_sleb128bit,
  93. aitconst_uleb128bit,
  94. { win32 only }
  95. aitconst_rva_symbol,
  96. aitconst_secrel32_symbol,
  97. { darwin only }
  98. { From gcc/config/darwin.c (darwin_asm_output_dwarf_delta):
  99. ***
  100. Output a difference of two labels that will be an assembly time
  101. constant if the two labels are local. (.long lab1-lab2 will be
  102. very different if lab1 is at the boundary between two sections; it
  103. will be relocated according to the second section, not the first,
  104. so one ends up with a difference between labels in different
  105. sections, which is bad in the dwarf2 eh context for instance.)
  106. ***
  107. We cannot use this everywhere, because older versions of the
  108. darwin assembler don't support the construct used for these
  109. relsyms (nor do they support dwarf, for that matter)
  110. }
  111. aitconst_darwin_dwarf_delta64,
  112. aitconst_darwin_dwarf_delta32,
  113. { ARM Thumb-2 only }
  114. aitconst_half16bit { used for table jumps. The actual value is the 16bit value shifted left once }
  115. );
  116. const
  117. {$ifdef cpu64bitaddr}
  118. aitconst_ptr = aitconst_64bit;
  119. {$else cpu64bitaddr}
  120. aitconst_ptr = aitconst_32bit;
  121. {$endif cpu64bitaddr}
  122. {$ifdef cpu64bitalu}
  123. aitconst_aint = aitconst_64bit;
  124. {$else cpu64bitaddr}
  125. aitconst_aint = aitconst_32bit;
  126. {$endif cpu64bitaddr}
  127. taitypestr : array[taitype] of string[24] = (
  128. '<none>',
  129. 'align',
  130. 'section',
  131. 'comment',
  132. 'string',
  133. 'instruction',
  134. 'datablock',
  135. 'symbol',
  136. 'symbol_end',
  137. 'symbol_directive',
  138. 'label',
  139. 'const',
  140. 'real_32bit',
  141. 'real_64bit',
  142. 'real_80bit',
  143. 'comp_64bit',
  144. 'real_128bit',
  145. 'stab',
  146. 'force_line',
  147. 'function_name',
  148. {$ifdef alpha}
  149. { the follow is for the DEC Alpha }
  150. 'frame',
  151. 'ent',
  152. {$endif alpha}
  153. {$ifdef ia64}
  154. 'bundle',
  155. 'stop',
  156. {$endif ia64}
  157. {$ifdef m68k}
  158. 'labeled_instr',
  159. {$endif m68k}
  160. {$ifdef arm}
  161. 'thumb_func',
  162. {$endif arm}
  163. 'cut',
  164. 'regalloc',
  165. 'tempalloc',
  166. 'marker',
  167. 'jvar',
  168. 'jcatch'
  169. );
  170. type
  171. { Types of operand }
  172. toptype=(top_none,top_reg,top_ref,top_const,top_bool,top_local
  173. {$ifdef arm}
  174. { ARM only }
  175. ,top_regset
  176. ,top_shifterop
  177. ,top_conditioncode
  178. ,top_modeflags
  179. {$endif arm}
  180. {$ifdef m68k}
  181. { m68k only }
  182. ,top_regset
  183. {$endif m68k}
  184. {$ifdef jvm}
  185. { jvm only}
  186. ,top_single
  187. ,top_double
  188. ,top_string
  189. ,top_wstring
  190. {$endif jvm}
  191. );
  192. { kinds of operations that an instruction can perform on an operand }
  193. topertype = (operand_read,operand_write,operand_readwrite);
  194. tlocaloper = record
  195. localsym : pointer;
  196. localsymderef : tderef;
  197. localsymofs : longint;
  198. localindexreg : tregister;
  199. localscale : byte;
  200. localgetoffset,
  201. localforceref : boolean
  202. end;
  203. plocaloper = ^tlocaloper;
  204. { please keep the size of this record <=12 bytes and keep it properly aligned }
  205. toper = record
  206. ot : longint;
  207. case typ : toptype of
  208. top_none : ();
  209. top_reg : (reg:tregister);
  210. top_ref : (ref:preference);
  211. top_const : (val:aint);
  212. top_bool : (b:boolean);
  213. { local varsym that will be inserted in pass_generate_code }
  214. top_local : (localoper:plocaloper);
  215. {$ifdef arm}
  216. top_regset : (regset:^tcpuregisterset; regtyp: tregistertype; subreg: tsubregister);
  217. top_shifterop : (shifterop : pshifterop);
  218. top_conditioncode : (cc : TAsmCond);
  219. top_modeflags : (modeflags : tcpumodeflags);
  220. {$endif arm}
  221. {$ifdef m68k}
  222. top_regset : (regset:^tcpuregisterset);
  223. {$endif m68k}
  224. {$ifdef jvm}
  225. top_single : (sval:single);
  226. top_double : (dval:double);
  227. top_string : (pcvallen: aint; pcval: pchar);
  228. top_wstring : (pwstrval: pcompilerwidestring);
  229. {$endif jvm}
  230. end;
  231. poper=^toper;
  232. const
  233. { ait_* types which don't result in executable code or which don't influence
  234. the way the program runs/behaves, but which may be encountered by the
  235. optimizer (= if it's sometimes added to the exprasm list). Update if you add
  236. a new ait type! }
  237. SkipInstr = [ait_comment, ait_symbol,ait_section
  238. ,ait_stab, ait_function_name, ait_force_line
  239. ,ait_regalloc, ait_tempalloc, ait_symbol_end, ait_directive
  240. ,ait_jvar, ait_jcatch];
  241. { ait_* types which do not have line information (and hence which are of type
  242. tai, otherwise, they are of type tailineinfo }
  243. SkipLineInfo =[ait_label,
  244. ait_regalloc,ait_tempalloc,
  245. ait_stab,ait_function_name,
  246. ait_cutobject,ait_marker,ait_align,ait_section,ait_comment,
  247. ait_const,ait_directive,
  248. {$ifdef arm}
  249. ait_thumb_func,
  250. {$endif arm}
  251. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_real_128bit,
  252. ait_symbol,
  253. ait_jvar,ait_jcatch
  254. ];
  255. type
  256. { cut type, required for alphanumeric ordering of the assembler filenames }
  257. TCutPlace=(cut_normal,cut_begin,cut_end);
  258. TAsmMarker = (
  259. mark_NoPropInfoStart,mark_NoPropInfoEnd,
  260. mark_AsmBlockStart,mark_AsmBlockEnd,
  261. mark_NoLineInfoStart,mark_NoLineInfoEnd,mark_BlockStart,
  262. mark_Position
  263. );
  264. TRegAllocType = (ra_alloc,ra_dealloc,ra_sync,ra_resize);
  265. TStabType = (stab_stabs,stab_stabn,stab_stabd);
  266. TAsmDirective=(
  267. asd_indirect_symbol,
  268. asd_extern,asd_nasm_import, asd_toc_entry,
  269. asd_reference,asd_no_dead_strip,asd_weak_reference,asd_lazy_reference,
  270. asd_weak_definition,
  271. { for Jasmin }
  272. asd_jclass,asd_jinterface,asd_jsuper,asd_jfield,asd_jlimit
  273. );
  274. const
  275. regallocstr : array[tregalloctype] of string[10]=('allocated','released','sync','resized');
  276. tempallocstr : array[boolean] of string[10]=('released','allocated');
  277. stabtypestr : array[TStabType] of string[5]=('stabs','stabn','stabd');
  278. directivestr : array[TAsmDirective] of string[23]=(
  279. 'indirect_symbol',
  280. 'extern','nasm_import', 'tc', 'reference',
  281. 'no_dead_strip','weak_reference','lazy_reference','weak_definition',
  282. { for Jasmin }
  283. 'class','interface','super','field','limit'
  284. );
  285. type
  286. { abstract assembler item }
  287. tai = class(TLinkedListItem)
  288. {$ifndef NOOPT}
  289. { pointer to record with optimizer info about this tai object }
  290. optinfo : pointer;
  291. {$endif NOOPT}
  292. typ : taitype;
  293. constructor Create;
  294. constructor ppuload(t:taitype;ppufile:tcompilerppufile);virtual;
  295. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  296. procedure buildderefimpl;virtual;
  297. procedure derefimpl;virtual;
  298. end;
  299. { abstract assembler item with line information }
  300. tailineinfo = class(tai)
  301. fileinfo : tfileposinfo;
  302. constructor Create;
  303. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  304. procedure ppuwrite(ppufile:tcompilerppufile);override;
  305. end;
  306. tai_simple = class(tai)
  307. constructor create(_typ : taitype);
  308. end;
  309. taiclass = class of tai;
  310. taiclassarray = array[taitype] of taiclass;
  311. { Generates an assembler string }
  312. tai_string = class(tailineinfo)
  313. str : pchar;
  314. { extra len so the string can contain an \0 }
  315. len : longint;
  316. constructor Create(const _str : string);
  317. constructor Create_pchar(_str : pchar;length : longint);
  318. destructor Destroy;override;
  319. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  320. procedure ppuwrite(ppufile:tcompilerppufile);override;
  321. function getcopy:tlinkedlistitem;override;
  322. end;
  323. { Generates a common label }
  324. tai_symbol = class(tai)
  325. sym : tasmsymbol;
  326. value : puint;
  327. size : longint;
  328. is_global,
  329. has_value : boolean;
  330. constructor Create(_sym:tasmsymbol;siz:longint);
  331. constructor Create_Global(_sym:tasmsymbol;siz:longint);
  332. constructor Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  333. constructor Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  334. constructor Createname_global_value(const _name : string;_symtyp:Tasmsymtype;siz:longint;val:ptruint);
  335. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  336. procedure ppuwrite(ppufile:tcompilerppufile);override;
  337. procedure derefimpl;override;
  338. end;
  339. tai_symbol_end = class(tailineinfo)
  340. sym : tasmsymbol;
  341. constructor Create(_sym:tasmsymbol);
  342. constructor Createname(const _name : string);
  343. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  344. procedure ppuwrite(ppufile:tcompilerppufile);override;
  345. procedure derefimpl;override;
  346. end;
  347. tai_directive = class(tailineinfo)
  348. name : pshortstring;
  349. directive : TAsmDirective;
  350. constructor Create(_directive:TAsmDirective;const _name:string);
  351. destructor Destroy;override;
  352. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  353. procedure ppuwrite(ppufile:tcompilerppufile);override;
  354. end;
  355. { Generates an assembler label }
  356. tai_label = class(tai)
  357. is_global : boolean;
  358. labsym : tasmlabel;
  359. constructor Create(_labsym : tasmlabel);
  360. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  361. procedure ppuwrite(ppufile:tcompilerppufile);override;
  362. procedure derefimpl;override;
  363. end;
  364. { Generates an assembler comment }
  365. tai_comment = class(tai)
  366. str : pchar;
  367. constructor Create(_str : pchar);
  368. destructor Destroy; override;
  369. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  370. procedure ppuwrite(ppufile:tcompilerppufile);override;
  371. function getcopy:tlinkedlistitem;override;
  372. end;
  373. { Generates a section / segment directive }
  374. tai_section = class(tai)
  375. sectype : TAsmSectiontype;
  376. secorder : TasmSectionorder;
  377. secalign : byte;
  378. name : pshortstring;
  379. sec : TObjSection; { used in binary writer }
  380. destructor Destroy;override;
  381. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  382. procedure ppuwrite(ppufile:tcompilerppufile);override;
  383. private
  384. { this constructor is made private on purpose }
  385. { because sections should be created via new_section() }
  386. constructor Create(Asectype:TAsmSectiontype;Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  387. end;
  388. { Generates an uninitializised data block }
  389. tai_datablock = class(tailineinfo)
  390. is_global : boolean;
  391. sym : tasmsymbol;
  392. size : asizeint;
  393. constructor Create(const _name : string;_size : aint);
  394. constructor Create_global(const _name : string;_size : asizeint);
  395. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  396. procedure ppuwrite(ppufile:tcompilerppufile);override;
  397. procedure derefimpl;override;
  398. end;
  399. { Generates an integer const }
  400. tai_const = class(tai)
  401. sym,
  402. endsym : tasmsymbol;
  403. { if symbols and offset are provided the symofs is used,
  404. the value is calculated during assembling }
  405. symofs,
  406. value : int64;
  407. consttype : taiconst_type;
  408. { we use for the 128bit int64/qword for now because I can't imagine a
  409. case where we need 128 bit now (FK) }
  410. constructor Create(_typ:taiconst_type;_value : int64);
  411. constructor Create_128bit(_value : int64);
  412. constructor Create_64bit(_value : int64);
  413. constructor Create_32bit(_value : longint);
  414. constructor Create_16bit(_value : word);
  415. constructor Create_8bit(_value : byte);
  416. constructor Create_char(size: integer; _value: dword);
  417. constructor Create_sleb128bit(_value : int64);
  418. constructor Create_uleb128bit(_value : qword);
  419. constructor Create_aint(_value : aint);
  420. constructor Create_pint(_value : pint);
  421. constructor Create_sym(_sym:tasmsymbol);
  422. constructor Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  423. constructor Create_sym_offset(_sym:tasmsymbol;ofs:aint);
  424. constructor Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  425. constructor Create_rva_sym(_sym:tasmsymbol);
  426. constructor Createname(const name:string;ofs:aint);
  427. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  428. procedure ppuwrite(ppufile:tcompilerppufile);override;
  429. procedure derefimpl;override;
  430. function getcopy:tlinkedlistitem;override;
  431. function size:longint;
  432. end;
  433. { Generates a single float (32 bit real) }
  434. tai_real_32bit = class(tai)
  435. value : ts32real;
  436. constructor Create(_value : ts32real);
  437. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  438. procedure ppuwrite(ppufile:tcompilerppufile);override;
  439. end;
  440. tformatoptions = (fo_none,fo_hiloswapped);
  441. { Generates a double float (64 bit real) }
  442. tai_real_64bit = class(tai)
  443. value : ts64real;
  444. {$ifdef ARM}
  445. formatoptions : tformatoptions;
  446. constructor Create_hiloswapped(_value : ts64real);
  447. {$endif ARM}
  448. constructor Create(_value : ts64real);
  449. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  450. procedure ppuwrite(ppufile:tcompilerppufile);override;
  451. end;
  452. { Generates an extended float (80 bit real) }
  453. tai_real_80bit = class(tai)
  454. value : ts80real;
  455. savesize : byte;
  456. constructor Create(_value : ts80real; _savesize: byte);
  457. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  458. procedure ppuwrite(ppufile:tcompilerppufile);override;
  459. end;
  460. { Generates an float128 (128 bit real) }
  461. tai_real_128bit = class(tai)
  462. value : ts128real;
  463. constructor Create(_value : ts128real);
  464. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  465. procedure ppuwrite(ppufile:tcompilerppufile);override;
  466. end;
  467. { Generates a comp int (integer over 64 bits)
  468. This is Intel 80x86 specific, and is not
  469. really supported on other processors.
  470. }
  471. tai_comp_64bit = class(tai)
  472. value : ts64comp;
  473. constructor Create(_value : ts64comp);
  474. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  475. procedure ppuwrite(ppufile:tcompilerppufile);override;
  476. end;
  477. { tai_stab }
  478. tai_stab = class(tai)
  479. str : pchar;
  480. stabtype : TStabType;
  481. constructor Create(_stabtype:TStabType;_str : pchar);
  482. constructor Create_str(_stabtype:TStabType;const s:string);
  483. constructor create_ansistr(_stabtype: TStabType; const s: ansistring);
  484. destructor Destroy;override;
  485. end;
  486. tai_force_line = class(tailineinfo)
  487. constructor Create;
  488. end;
  489. tai_function_name = class(tai)
  490. funcname : pshortstring;
  491. constructor create(const s:string);
  492. destructor destroy;override;
  493. end;
  494. { Insert a cut to split assembler into several smaller files }
  495. tai_cutobject = class(tai)
  496. place : tcutplace;
  497. constructor Create;
  498. constructor Create_begin;
  499. constructor Create_end;
  500. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  501. procedure ppuwrite(ppufile:tcompilerppufile);override;
  502. end;
  503. { Insert a marker for assembler and inline blocks }
  504. tai_marker = class(tai)
  505. Kind: TAsmMarker;
  506. Constructor Create(_Kind: TAsmMarker);
  507. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  508. procedure ppuwrite(ppufile:tcompilerppufile);override;
  509. end;
  510. tai_tempalloc = class(tai)
  511. allocation : boolean;
  512. {$ifdef EXTDEBUG}
  513. problem : pshortstring;
  514. {$endif EXTDEBUG}
  515. temppos,
  516. tempsize : longint;
  517. constructor alloc(pos,size:longint);
  518. constructor dealloc(pos,size:longint);
  519. {$ifdef EXTDEBUG}
  520. constructor allocinfo(pos,size:longint;const st:string);
  521. {$endif EXTDEBUG}
  522. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  523. destructor destroy;override;
  524. procedure ppuwrite(ppufile:tcompilerppufile);override;
  525. end;
  526. tai_regalloc = class(tai)
  527. reg : tregister;
  528. ratype : TRegAllocType;
  529. { reg(de)alloc belongs to this instruction, this
  530. is only used for automatic inserted (de)alloc for
  531. imaginary register and required for spilling code }
  532. instr : tai;
  533. constructor alloc(r : tregister;ainstr:tai);
  534. constructor dealloc(r : tregister;ainstr:tai);
  535. constructor sync(r : tregister);
  536. constructor resize(r : tregister);
  537. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  538. procedure ppuwrite(ppufile:tcompilerppufile);override;
  539. end;
  540. tadd_reg_instruction_proc=procedure(instr:Tai;r:tregister) of object;
  541. { Class template for assembler instructions
  542. }
  543. tai_cpu_abstract = class(tailineinfo)
  544. protected
  545. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);virtual;
  546. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);virtual;
  547. procedure ppubuildderefimploper(var o:toper);virtual;abstract;
  548. procedure ppuderefoper(var o:toper);virtual;abstract;
  549. public
  550. { Condition flags for instruction }
  551. condition : TAsmCond;
  552. { Number of operands to instruction }
  553. ops : byte;
  554. { Number of allocate oper structures }
  555. opercnt : byte;
  556. { Operands of instruction }
  557. oper : array[0..max_operands-1] of poper;
  558. { Actual opcode of instruction }
  559. opcode : tasmop;
  560. {$ifdef x86}
  561. segprefix : tregister;
  562. {$endif x86}
  563. { true if instruction is a jmp }
  564. is_jmp : boolean; { is this instruction a jump? (needed for optimizer) }
  565. Constructor Create(op : tasmop);virtual;
  566. Destructor Destroy;override;
  567. function getcopy:TLinkedListItem;override;
  568. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  569. procedure ppuwrite(ppufile:tcompilerppufile);override;
  570. procedure buildderefimpl;override;
  571. procedure derefimpl;override;
  572. procedure SetCondition(const c:TAsmCond);
  573. procedure allocate_oper(opers:longint);
  574. procedure loadconst(opidx:longint;l:aint);
  575. procedure loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  576. procedure loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  577. procedure loadref(opidx:longint;const r:treference);
  578. procedure loadreg(opidx:longint;r:tregister);
  579. procedure loadoper(opidx:longint;o:toper);
  580. procedure clearop(opidx:longint);
  581. procedure freeop(opidx:longint);
  582. { register allocator }
  583. function is_same_reg_move(regtype: Tregistertype):boolean;virtual;
  584. function spilling_get_operation_type(opnr: longint): topertype;virtual;
  585. function spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;virtual;
  586. function Pass1(objdata:TObjData):longint;virtual;
  587. procedure Pass2(objdata:TObjData);virtual;
  588. procedure resetpass1; virtual;
  589. procedure resetpass2; virtual;
  590. end;
  591. tai_cpu_class = class of tai_cpu_abstract;
  592. { Buffer type used for alignment }
  593. tfillbuffer = array[0..63] of char;
  594. { alignment for operator }
  595. tai_align_abstract = class(tai)
  596. aligntype : byte; { 1 = no align, 2 = word align, 4 = dword align }
  597. fillsize : byte; { real size to fill }
  598. fillop : byte; { value to fill with - optional }
  599. use_op : boolean;
  600. constructor Create(b:byte);virtual;
  601. constructor Create_op(b: byte; _op: byte);virtual;
  602. constructor Create_zeros(b:byte);
  603. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  604. procedure ppuwrite(ppufile:tcompilerppufile);override;
  605. function calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;virtual;
  606. end;
  607. tai_align_class = class of tai_align_abstract;
  608. { JVM variable live range description }
  609. tai_jvar = class(tai)
  610. stackslot: longint;
  611. desc: pshortstring;
  612. startlab,stoplab: tasmsymbol;
  613. constructor Create(_stackslot: longint; const _desc: shortstring; _startlab, _stoplab: TAsmSymbol);
  614. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  615. procedure ppuwrite(ppufile:tcompilerppufile);override;
  616. end;
  617. tai_jvar_class = class of tai_jvar;
  618. { JVM exception catch description }
  619. tai_jcatch = class(tai)
  620. name: pshortstring;
  621. startlab,stoplab,handlerlab: tasmsymbol;
  622. constructor Create(const _name: shortstring; _startlab, _stoplab, _handlerlab: TAsmSymbol);
  623. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  624. procedure ppuwrite(ppufile:tcompilerppufile);override;
  625. end;
  626. tai_jcatch_class = class of tai_jcatch;
  627. var
  628. { array with all class types for tais }
  629. aiclass : taiclassarray;
  630. { target specific tais, possibly overwritten in target specific aasmcpu }
  631. cai_align : tai_align_class = tai_align_abstract;
  632. cai_cpu : tai_cpu_class = tai_cpu_abstract;
  633. { hook to notify uses of registers }
  634. add_reg_instruction_hook : tadd_reg_instruction_proc;
  635. procedure maybe_new_object_file(list:TAsmList);
  636. procedure new_section(list:TAsmList;Asectype:TAsmSectiontype;Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  637. procedure section_symbol_start(list:TAsmList;const Aname:string;Asymtyp:Tasmsymtype;
  638. Aglobal:boolean;Asectype:TAsmSectiontype;Aalign:byte);
  639. procedure section_symbol_end(list:TAsmList;const Aname:string);
  640. function ppuloadai(ppufile:tcompilerppufile):tai;
  641. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  642. implementation
  643. uses
  644. SysUtils,
  645. verbose,
  646. globals,
  647. fmodule;
  648. const
  649. pputaimarker = 254;
  650. {****************************************************************************
  651. Helpers
  652. ****************************************************************************}
  653. procedure maybe_new_object_file(list:TAsmList);
  654. begin
  655. if create_smartlink_library then
  656. list.concat(tai_cutobject.create);
  657. end;
  658. procedure new_section(list:TAsmList;Asectype:TAsmSectiontype;Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  659. begin
  660. list.concat(tai_section.create(Asectype,Aname,Aalign,Asecorder));
  661. list.concat(cai_align.create(Aalign));
  662. end;
  663. procedure section_symbol_start(list:TAsmList;const Aname:string;Asymtyp:Tasmsymtype;
  664. Aglobal:boolean;Asectype:TAsmSectiontype;Aalign:byte);
  665. begin
  666. maybe_new_object_file(list);
  667. new_section(list,Asectype,Aname,Aalign);
  668. if Aglobal or
  669. create_smartlink then
  670. list.concat(tai_symbol.createname_global(Aname,Asymtyp,0))
  671. else
  672. list.concat(tai_symbol.createname(Aname,Asymtyp,0));
  673. end;
  674. procedure section_symbol_end(list:TAsmList;const Aname:string);
  675. begin
  676. list.concat(tai_symbol_end.createname(Aname));
  677. end;
  678. function ppuloadai(ppufile:tcompilerppufile):tai;
  679. var
  680. b : byte;
  681. t : taitype;
  682. begin
  683. { marker }
  684. b:=ppufile.getbyte;
  685. if b<>pputaimarker then
  686. internalerror(200208181);
  687. { load nodetype }
  688. t:=taitype(ppufile.getbyte);
  689. if t<>ait_none then
  690. begin
  691. if t>high(taitype) then
  692. internalerror(200208182);
  693. if not assigned(aiclass[t]) then
  694. internalerror(200208183);
  695. {writeln('taiload: ',taitypestr[t]);}
  696. { generate tai of the correct class }
  697. ppuloadai:=aiclass[t].ppuload(t,ppufile);
  698. end
  699. else
  700. ppuloadai:=nil;
  701. end;
  702. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  703. begin
  704. { marker, read by ppuloadnode }
  705. ppufile.putbyte(pputaimarker);
  706. if assigned(n) then
  707. begin
  708. { type, read by ppuloadnode }
  709. ppufile.putbyte(byte(n.typ));
  710. {writeln('taiwrite: ',taitypestr[n.typ]);}
  711. n.ppuwrite(ppufile);
  712. end
  713. else
  714. ppufile.putbyte(byte(ait_none));
  715. end;
  716. {****************************************************************************
  717. TAI
  718. ****************************************************************************}
  719. constructor tai.Create;
  720. begin
  721. {$ifndef NOOPT}
  722. optinfo:=nil;
  723. {$endif NOOPT}
  724. end;
  725. constructor tai.ppuload(t:taitype;ppufile:tcompilerppufile);
  726. begin
  727. typ:=t;
  728. {$ifndef NOOPT}
  729. optinfo:=nil;
  730. {$endif}
  731. end;
  732. procedure tai.ppuwrite(ppufile:tcompilerppufile);
  733. begin
  734. end;
  735. procedure tai.buildderefimpl;
  736. begin
  737. end;
  738. procedure tai.derefimpl;
  739. begin
  740. end;
  741. {****************************************************************************
  742. TAILINEINFO
  743. ****************************************************************************}
  744. constructor tailineinfo.create;
  745. begin
  746. inherited create;
  747. fileinfo:=current_filepos;
  748. end;
  749. constructor tailineinfo.ppuload(t:taitype;ppufile:tcompilerppufile);
  750. begin
  751. inherited ppuload(t,ppufile);
  752. ppufile.getposinfo(fileinfo);
  753. end;
  754. procedure tailineinfo.ppuwrite(ppufile:tcompilerppufile);
  755. begin
  756. inherited ppuwrite(ppufile);
  757. ppufile.putposinfo(fileinfo);
  758. end;
  759. {****************************************************************************
  760. TAI_SIMPLE
  761. ****************************************************************************}
  762. constructor tai_simple.create(_typ : taitype);
  763. begin
  764. inherited create;
  765. typ:=_typ;
  766. end;
  767. {****************************************************************************
  768. TAI_SECTION
  769. ****************************************************************************}
  770. constructor tai_section.Create(Asectype:TAsmSectiontype;Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  771. begin
  772. inherited Create;
  773. typ:=ait_section;
  774. sectype:=asectype;
  775. secalign:=Aalign;
  776. secorder:=Asecorder;
  777. name:=stringdup(Aname);
  778. sec:=nil;
  779. end;
  780. constructor tai_section.ppuload(t:taitype;ppufile:tcompilerppufile);
  781. begin
  782. inherited ppuload(t,ppufile);
  783. sectype:=TAsmSectiontype(ppufile.getbyte);
  784. secalign:=ppufile.getbyte;
  785. name:=stringdup(ppufile.getstring);
  786. sec:=nil;
  787. end;
  788. destructor tai_section.Destroy;
  789. begin
  790. stringdispose(name);
  791. end;
  792. procedure tai_section.ppuwrite(ppufile:tcompilerppufile);
  793. begin
  794. inherited ppuwrite(ppufile);
  795. ppufile.putbyte(byte(sectype));
  796. ppufile.putbyte(secalign);
  797. ppufile.putstring(name^);
  798. end;
  799. {****************************************************************************
  800. TAI_DATABLOCK
  801. ****************************************************************************}
  802. constructor tai_datablock.Create(const _name : string;_size : aint);
  803. begin
  804. inherited Create;
  805. typ:=ait_datablock;
  806. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,AT_DATA);
  807. { keep things aligned }
  808. if _size<=0 then
  809. _size:=sizeof(aint);
  810. size:=_size;
  811. is_global:=false;
  812. end;
  813. constructor tai_datablock.Create_global(const _name : string;_size : asizeint);
  814. begin
  815. inherited Create;
  816. typ:=ait_datablock;
  817. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,AT_DATA);
  818. { keep things aligned }
  819. if _size<=0 then
  820. _size:=sizeof(aint);
  821. size:=_size;
  822. is_global:=true;
  823. end;
  824. constructor tai_datablock.ppuload(t:taitype;ppufile:tcompilerppufile);
  825. begin
  826. inherited Create;
  827. sym:=ppufile.getasmsymbol;
  828. size:=ppufile.getaint;
  829. is_global:=boolean(ppufile.getbyte);
  830. end;
  831. procedure tai_datablock.ppuwrite(ppufile:tcompilerppufile);
  832. begin
  833. inherited ppuwrite(ppufile);
  834. ppufile.putasmsymbol(sym);
  835. ppufile.putaint(size);
  836. ppufile.putbyte(byte(is_global));
  837. end;
  838. procedure tai_datablock.derefimpl;
  839. begin
  840. end;
  841. {****************************************************************************
  842. TAI_SYMBOL
  843. ****************************************************************************}
  844. constructor tai_symbol.Create(_sym:tasmsymbol;siz:longint);
  845. begin
  846. inherited Create;
  847. typ:=ait_symbol;
  848. sym:=_sym;
  849. size:=siz;
  850. sym.bind:=AB_LOCAL;
  851. is_global:=false;
  852. end;
  853. constructor tai_symbol.Create_global(_sym:tasmsymbol;siz:longint);
  854. begin
  855. inherited Create;
  856. typ:=ait_symbol;
  857. sym:=_sym;
  858. size:=siz;
  859. { don't override PRIVATE_EXTERN with GLOBAL }
  860. if not(sym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN]) then
  861. sym.bind:=AB_GLOBAL;
  862. is_global:=true;
  863. end;
  864. constructor tai_symbol.Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  865. begin
  866. inherited Create;
  867. typ:=ait_symbol;
  868. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,_symtyp);
  869. size:=siz;
  870. is_global:=false;
  871. end;
  872. constructor tai_symbol.Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  873. begin
  874. inherited Create;
  875. typ:=ait_symbol;
  876. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,_symtyp);
  877. size:=siz;
  878. is_global:=true;
  879. end;
  880. constructor tai_symbol.createname_global_value(const _name: string;_symtyp: tasmsymtype; siz: longint; val: ptruint);
  881. begin
  882. Createname_global(_name,_symtyp,siz);
  883. value:=val;
  884. has_value:=true;
  885. end;
  886. constructor tai_symbol.ppuload(t:taitype;ppufile:tcompilerppufile);
  887. begin
  888. inherited ppuload(t,ppufile);
  889. sym:=ppufile.getasmsymbol;
  890. size:=ppufile.getlongint;
  891. is_global:=boolean(ppufile.getbyte);
  892. end;
  893. procedure tai_symbol.ppuwrite(ppufile:tcompilerppufile);
  894. begin
  895. inherited ppuwrite(ppufile);
  896. ppufile.putasmsymbol(sym);
  897. ppufile.putlongint(size);
  898. ppufile.putbyte(byte(is_global));
  899. end;
  900. procedure tai_symbol.derefimpl;
  901. begin
  902. end;
  903. {****************************************************************************
  904. TAI_SYMBOL_END
  905. ****************************************************************************}
  906. constructor tai_symbol_end.Create(_sym:tasmsymbol);
  907. begin
  908. inherited Create;
  909. typ:=ait_symbol_end;
  910. sym:=_sym;
  911. end;
  912. constructor tai_symbol_end.Createname(const _name : string);
  913. begin
  914. inherited Create;
  915. typ:=ait_symbol_end;
  916. sym:=current_asmdata.RefAsmSymbol(_name);
  917. end;
  918. constructor tai_symbol_end.ppuload(t:taitype;ppufile:tcompilerppufile);
  919. begin
  920. inherited ppuload(t,ppufile);
  921. sym:=ppufile.getasmsymbol;
  922. end;
  923. procedure tai_symbol_end.ppuwrite(ppufile:tcompilerppufile);
  924. begin
  925. inherited ppuwrite(ppufile);
  926. ppufile.putasmsymbol(sym);
  927. end;
  928. procedure tai_symbol_end.derefimpl;
  929. begin
  930. end;
  931. {****************************************************************************
  932. TAI_SYMBOL_END
  933. ****************************************************************************}
  934. constructor tai_directive.Create(_directive:TAsmDirective;const _name:string);
  935. begin
  936. inherited Create;
  937. typ:=ait_directive;
  938. name:=stringdup(_name);
  939. directive:=_directive;
  940. end;
  941. destructor tai_directive.Destroy;
  942. begin
  943. stringdispose(name);
  944. end;
  945. constructor tai_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  946. begin
  947. inherited ppuload(t,ppufile);
  948. name:=stringdup(ppufile.getstring);
  949. directive:=TAsmDirective(ppufile.getbyte);
  950. end;
  951. procedure tai_directive.ppuwrite(ppufile:tcompilerppufile);
  952. begin
  953. inherited ppuwrite(ppufile);
  954. ppufile.putstring(name^);
  955. ppufile.putbyte(byte(directive));
  956. end;
  957. {****************************************************************************
  958. TAI_CONST
  959. ****************************************************************************}
  960. constructor tai_const.Create(_typ:taiconst_type;_value : int64);
  961. begin
  962. inherited Create;
  963. typ:=ait_const;
  964. consttype:=_typ;
  965. value:=_value;
  966. sym:=nil;
  967. endsym:=nil;
  968. end;
  969. constructor tai_const.Create_128bit(_value : int64);
  970. begin
  971. inherited Create;
  972. typ:=ait_const;
  973. consttype:=aitconst_128bit;
  974. value:=_value;
  975. sym:=nil;
  976. endsym:=nil;
  977. end;
  978. constructor tai_const.Create_64bit(_value : int64);
  979. begin
  980. inherited Create;
  981. typ:=ait_const;
  982. consttype:=aitconst_64bit;
  983. value:=_value;
  984. sym:=nil;
  985. endsym:=nil;
  986. end;
  987. constructor tai_const.Create_32bit(_value : longint);
  988. begin
  989. inherited Create;
  990. typ:=ait_const;
  991. consttype:=aitconst_32bit;
  992. value:=_value;
  993. sym:=nil;
  994. endsym:=nil;
  995. end;
  996. constructor tai_const.Create_16bit(_value : word);
  997. begin
  998. inherited Create;
  999. typ:=ait_const;
  1000. consttype:=aitconst_16bit;
  1001. value:=_value;
  1002. sym:=nil;
  1003. endsym:=nil;
  1004. end;
  1005. constructor tai_const.Create_8bit(_value : byte);
  1006. begin
  1007. inherited Create;
  1008. typ:=ait_const;
  1009. consttype:=aitconst_8bit;
  1010. value:=_value;
  1011. sym:=nil;
  1012. endsym:=nil;
  1013. end;
  1014. constructor tai_const.Create_char(size: integer; _value: dword);
  1015. begin
  1016. inherited Create;
  1017. typ:=ait_const;
  1018. case size of
  1019. 1:
  1020. begin
  1021. consttype:=aitconst_8bit;
  1022. value:=byte(_value)
  1023. end;
  1024. 2:
  1025. begin
  1026. consttype:=aitconst_16bit;
  1027. value:=word(_value)
  1028. end
  1029. else
  1030. InternalError(2010030701)
  1031. end
  1032. end;
  1033. constructor tai_const.Create_sleb128bit(_value : int64);
  1034. begin
  1035. inherited Create;
  1036. typ:=ait_const;
  1037. consttype:=aitconst_sleb128bit;
  1038. value:=_value;
  1039. sym:=nil;
  1040. endsym:=nil;
  1041. end;
  1042. constructor tai_const.Create_uleb128bit(_value : qword);
  1043. begin
  1044. inherited Create;
  1045. typ:=ait_const;
  1046. consttype:=aitconst_uleb128bit;
  1047. value:=int64(_value);
  1048. sym:=nil;
  1049. endsym:=nil;
  1050. end;
  1051. constructor tai_const.Create_aint(_value : aint);
  1052. begin
  1053. inherited Create;
  1054. typ:=ait_const;
  1055. consttype:=aitconst_aint;
  1056. value:=_value;
  1057. sym:=nil;
  1058. endsym:=nil;
  1059. end;
  1060. constructor tai_const.Create_pint(_value : pint);
  1061. begin
  1062. inherited Create;
  1063. typ:=ait_const;
  1064. consttype:=aitconst_ptr;
  1065. value:=_value;
  1066. sym:=nil;
  1067. endsym:=nil;
  1068. end;
  1069. constructor tai_const.Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  1070. begin
  1071. inherited Create;
  1072. typ:=ait_const;
  1073. consttype:=_typ;
  1074. sym:=_sym;
  1075. endsym:=nil;
  1076. value:=0;
  1077. { update sym info }
  1078. if assigned(sym) then
  1079. sym.increfs;
  1080. end;
  1081. constructor tai_const.Create_sym(_sym:tasmsymbol);
  1082. begin
  1083. self.create_sym_offset(_sym,0);
  1084. end;
  1085. constructor tai_const.Create_sym_offset(_sym:tasmsymbol;ofs:aint);
  1086. begin
  1087. inherited Create;
  1088. typ:=ait_const;
  1089. consttype:=aitconst_ptr;
  1090. { sym is allowed to be nil, this is used to write nil pointers }
  1091. sym:=_sym;
  1092. endsym:=nil;
  1093. { store the original offset in symofs so that we can recalculate the
  1094. value field in the assembler }
  1095. symofs:=ofs;
  1096. value:=ofs;
  1097. { update sym info }
  1098. if assigned(sym) then
  1099. sym.increfs;
  1100. end;
  1101. constructor tai_const.Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  1102. begin
  1103. self.create_sym_offset(_sym,0);
  1104. consttype:=_typ;
  1105. endsym:=_endsym;
  1106. endsym.increfs;
  1107. end;
  1108. constructor tai_const.Create_rva_sym(_sym:tasmsymbol);
  1109. begin
  1110. self.create_sym_offset(_sym,0);
  1111. consttype:=aitconst_rva_symbol;
  1112. end;
  1113. constructor tai_const.Createname(const name:string;ofs:aint);
  1114. begin
  1115. self.create_sym_offset(current_asmdata.RefAsmSymbol(name),ofs);
  1116. end;
  1117. constructor tai_const.ppuload(t:taitype;ppufile:tcompilerppufile);
  1118. begin
  1119. inherited ppuload(t,ppufile);
  1120. consttype:=taiconst_type(ppufile.getbyte);
  1121. sym:=ppufile.getasmsymbol;
  1122. endsym:=ppufile.getasmsymbol;
  1123. value:=ppufile.getint64;
  1124. end;
  1125. procedure tai_const.ppuwrite(ppufile:tcompilerppufile);
  1126. begin
  1127. inherited ppuwrite(ppufile);
  1128. ppufile.putbyte(byte(consttype));
  1129. ppufile.putasmsymbol(sym);
  1130. ppufile.putasmsymbol(endsym);
  1131. ppufile.putint64(value);
  1132. end;
  1133. procedure tai_const.derefimpl;
  1134. begin
  1135. end;
  1136. function tai_const.getcopy:tlinkedlistitem;
  1137. begin
  1138. getcopy:=inherited getcopy;
  1139. { we need to increase the reference number }
  1140. sym.increfs;
  1141. if assigned(endsym) then
  1142. endsym.increfs;
  1143. end;
  1144. function tai_const.size:longint;
  1145. begin
  1146. case consttype of
  1147. aitconst_8bit :
  1148. result:=1;
  1149. aitconst_16bit :
  1150. result:=2;
  1151. aitconst_32bit,aitconst_darwin_dwarf_delta32:
  1152. result:=4;
  1153. aitconst_64bit,aitconst_darwin_dwarf_delta64:
  1154. result:=8;
  1155. aitconst_secrel32_symbol,
  1156. aitconst_rva_symbol :
  1157. if target_info.system=system_x86_64_win64 then
  1158. result:=sizeof(longint)
  1159. else
  1160. result:=sizeof(pint);
  1161. aitconst_uleb128bit :
  1162. result:=LengthUleb128(qword(value));
  1163. aitconst_sleb128bit :
  1164. result:=LengthSleb128(value);
  1165. aitconst_half16bit:
  1166. result:=2;
  1167. else
  1168. internalerror(200603253);
  1169. end;
  1170. end;
  1171. {****************************************************************************
  1172. TAI_real_32bit
  1173. ****************************************************************************}
  1174. constructor tai_real_32bit.Create(_value : ts32real);
  1175. begin
  1176. inherited Create;
  1177. typ:=ait_real_32bit;
  1178. value:=_value;
  1179. end;
  1180. constructor tai_real_32bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1181. begin
  1182. inherited ppuload(t,ppufile);
  1183. value:=ppufile.getreal;
  1184. end;
  1185. procedure tai_real_32bit.ppuwrite(ppufile:tcompilerppufile);
  1186. begin
  1187. inherited ppuwrite(ppufile);
  1188. ppufile.putreal(value);
  1189. end;
  1190. {****************************************************************************
  1191. TAI_real_64bit
  1192. ****************************************************************************}
  1193. constructor tai_real_64bit.Create(_value : ts64real);
  1194. begin
  1195. inherited Create;
  1196. typ:=ait_real_64bit;
  1197. value:=_value;
  1198. end;
  1199. {$ifdef ARM}
  1200. constructor tai_real_64bit.Create_hiloswapped(_value : ts64real);
  1201. begin
  1202. inherited Create;
  1203. typ:=ait_real_64bit;
  1204. value:=_value;
  1205. formatoptions:=fo_hiloswapped;
  1206. end;
  1207. {$endif ARM}
  1208. constructor tai_real_64bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1209. begin
  1210. inherited ppuload(t,ppufile);
  1211. value:=ppufile.getreal;
  1212. {$ifdef ARM}
  1213. formatoptions:=tformatoptions(ppufile.getbyte);
  1214. {$endif ARM}
  1215. end;
  1216. procedure tai_real_64bit.ppuwrite(ppufile:tcompilerppufile);
  1217. begin
  1218. inherited ppuwrite(ppufile);
  1219. ppufile.putreal(value);
  1220. {$ifdef ARM}
  1221. ppufile.putbyte(byte(formatoptions));
  1222. {$endif ARM}
  1223. end;
  1224. {****************************************************************************
  1225. TAI_real_80bit
  1226. ****************************************************************************}
  1227. constructor tai_real_80bit.Create(_value : ts80real; _savesize: byte);
  1228. begin
  1229. inherited Create;
  1230. typ:=ait_real_80bit;
  1231. value:=_value;
  1232. savesize:=_savesize;
  1233. end;
  1234. constructor tai_real_80bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1235. begin
  1236. inherited ppuload(t,ppufile);
  1237. value:=ppufile.getreal;
  1238. savesize:=ppufile.getbyte;
  1239. end;
  1240. procedure tai_real_80bit.ppuwrite(ppufile:tcompilerppufile);
  1241. begin
  1242. inherited ppuwrite(ppufile);
  1243. ppufile.putreal(value);
  1244. ppufile.putbyte(savesize);
  1245. end;
  1246. {****************************************************************************
  1247. TAI_real_80bit
  1248. ****************************************************************************}
  1249. constructor tai_real_128bit.Create(_value : ts128real);
  1250. begin
  1251. inherited Create;
  1252. typ:=ait_real_128bit;
  1253. value:=_value;
  1254. end;
  1255. constructor tai_real_128bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1256. begin
  1257. inherited ppuload(t,ppufile);
  1258. value:=ppufile.getreal;
  1259. end;
  1260. procedure tai_real_128bit.ppuwrite(ppufile:tcompilerppufile);
  1261. begin
  1262. inherited ppuwrite(ppufile);
  1263. ppufile.putreal(value);
  1264. end;
  1265. {****************************************************************************
  1266. Tai_comp_64bit
  1267. ****************************************************************************}
  1268. constructor tai_comp_64bit.Create(_value : ts64comp);
  1269. begin
  1270. inherited Create;
  1271. typ:=ait_comp_64bit;
  1272. value:=_value;
  1273. end;
  1274. constructor tai_comp_64bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1275. begin
  1276. inherited ppuload(t,ppufile);
  1277. ppufile.putdata(value,sizeof(value));
  1278. end;
  1279. procedure tai_comp_64bit.ppuwrite(ppufile:tcompilerppufile);
  1280. begin
  1281. inherited ppuwrite(ppufile);
  1282. ppufile.getdata(value,sizeof(value));
  1283. end;
  1284. {****************************************************************************
  1285. TAI_STRING
  1286. ****************************************************************************}
  1287. constructor tai_string.Create(const _str : string);
  1288. begin
  1289. inherited Create;
  1290. typ:=ait_string;
  1291. len:=length(_str);
  1292. getmem(str,len+1);
  1293. strpcopy(str,_str);
  1294. end;
  1295. constructor tai_string.Create_pchar(_str : pchar;length : longint);
  1296. begin
  1297. inherited Create;
  1298. typ:=ait_string;
  1299. str:=_str;
  1300. len:=length;
  1301. end;
  1302. destructor tai_string.destroy;
  1303. begin
  1304. if str<>nil then
  1305. freemem(str);
  1306. inherited Destroy;
  1307. end;
  1308. constructor tai_string.ppuload(t:taitype;ppufile:tcompilerppufile);
  1309. begin
  1310. inherited ppuload(t,ppufile);
  1311. len:=ppufile.getlongint;
  1312. getmem(str,len);
  1313. ppufile.getdata(str^,len);
  1314. end;
  1315. procedure tai_string.ppuwrite(ppufile:tcompilerppufile);
  1316. begin
  1317. inherited ppuwrite(ppufile);
  1318. ppufile.putlongint(len);
  1319. ppufile.putdata(str^,len);
  1320. end;
  1321. function tai_string.getcopy : tlinkedlistitem;
  1322. var
  1323. p : tlinkedlistitem;
  1324. begin
  1325. p:=inherited getcopy;
  1326. getmem(tai_string(p).str,len);
  1327. move(str^,tai_string(p).str^,len);
  1328. getcopy:=p;
  1329. end;
  1330. {****************************************************************************
  1331. TAI_LABEL
  1332. ****************************************************************************}
  1333. constructor tai_label.create(_labsym : tasmlabel);
  1334. begin
  1335. inherited Create;
  1336. typ:=ait_label;
  1337. labsym:=_labsym;
  1338. labsym.is_set:=true;
  1339. is_global:=(labsym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN]);
  1340. end;
  1341. constructor tai_label.ppuload(t:taitype;ppufile:tcompilerppufile);
  1342. begin
  1343. inherited ppuload(t,ppufile);
  1344. labsym:=tasmlabel(ppufile.getasmsymbol);
  1345. is_global:=boolean(ppufile.getbyte);
  1346. end;
  1347. procedure tai_label.ppuwrite(ppufile:tcompilerppufile);
  1348. begin
  1349. inherited ppuwrite(ppufile);
  1350. ppufile.putasmsymbol(labsym);
  1351. ppufile.putbyte(byte(is_global));
  1352. end;
  1353. procedure tai_label.derefimpl;
  1354. begin
  1355. labsym.is_set:=true;
  1356. end;
  1357. {****************************************************************************
  1358. tai_comment comment to be inserted in the assembler file
  1359. ****************************************************************************}
  1360. constructor tai_comment.Create(_str : pchar);
  1361. begin
  1362. inherited Create;
  1363. typ:=ait_comment;
  1364. str:=_str;
  1365. end;
  1366. destructor tai_comment.destroy;
  1367. begin
  1368. freemem(str);
  1369. inherited Destroy;
  1370. end;
  1371. constructor tai_comment.ppuload(t:taitype;ppufile:tcompilerppufile);
  1372. var
  1373. len : longint;
  1374. begin
  1375. inherited ppuload(t,ppufile);
  1376. len:=ppufile.getlongint;
  1377. getmem(str,len+1);
  1378. ppufile.getdata(str^,len);
  1379. str[len]:=#0;
  1380. end;
  1381. procedure tai_comment.ppuwrite(ppufile:tcompilerppufile);
  1382. var
  1383. len : longint;
  1384. begin
  1385. inherited ppuwrite(ppufile);
  1386. len:=strlen(str);
  1387. ppufile.putlongint(len);
  1388. ppufile.putdata(str^,len);
  1389. end;
  1390. function tai_comment.getcopy : tlinkedlistitem;
  1391. var
  1392. p : tlinkedlistitem;
  1393. begin
  1394. p:=inherited getcopy;
  1395. getmem(tai_comment(p).str,strlen(str)+1);
  1396. move(str^,tai_comment(p).str^,strlen(str)+1);
  1397. getcopy:=p;
  1398. end;
  1399. {****************************************************************************
  1400. TAI_STABS
  1401. ****************************************************************************}
  1402. constructor tai_stab.create(_stabtype:TStabType;_str : pchar);
  1403. begin
  1404. inherited create;
  1405. typ:=ait_stab;
  1406. str:=_str;
  1407. stabtype:=_stabtype;
  1408. end;
  1409. constructor tai_stab.create_str(_stabtype:TStabType;const s:string);
  1410. begin
  1411. self.create(_stabtype,strpnew(s));
  1412. end;
  1413. constructor tai_stab.create_ansistr(_stabtype:TStabType;const s:ansistring);
  1414. begin
  1415. inherited create;
  1416. typ:=ait_stab;
  1417. stabtype:=_stabtype;
  1418. getmem(str,length(s)+1);
  1419. move(s[1],str^,length(s)+1);
  1420. end;
  1421. destructor tai_stab.destroy;
  1422. begin
  1423. freemem(str);
  1424. inherited destroy;
  1425. end;
  1426. {****************************************************************************
  1427. TAI_FORCE_LINE
  1428. ****************************************************************************}
  1429. constructor tai_force_line.create;
  1430. begin
  1431. inherited create;
  1432. typ:=ait_force_line;
  1433. end;
  1434. {****************************************************************************
  1435. TAI_FUNCTION_NAME
  1436. ****************************************************************************}
  1437. constructor tai_function_name.create(const s:string);
  1438. begin
  1439. inherited create;
  1440. typ:=ait_function_name;
  1441. funcname:=stringdup(s);
  1442. end;
  1443. destructor tai_function_name.destroy;
  1444. begin
  1445. stringdispose(funcname);
  1446. inherited destroy;
  1447. end;
  1448. {****************************************************************************
  1449. TAI_CUTOBJECT
  1450. ****************************************************************************}
  1451. constructor tai_cutobject.Create;
  1452. begin
  1453. inherited Create;
  1454. typ:=ait_cutobject;
  1455. place:=cut_normal;
  1456. end;
  1457. constructor tai_cutobject.Create_begin;
  1458. begin
  1459. inherited Create;
  1460. typ:=ait_cutobject;
  1461. place:=cut_begin;
  1462. end;
  1463. constructor tai_cutobject.Create_end;
  1464. begin
  1465. inherited Create;
  1466. typ:=ait_cutobject;
  1467. place:=cut_end;
  1468. end;
  1469. constructor tai_cutobject.ppuload(t:taitype;ppufile:tcompilerppufile);
  1470. begin
  1471. inherited ppuload(t,ppufile);
  1472. place:=TCutPlace(ppufile.getbyte);
  1473. end;
  1474. procedure tai_cutobject.ppuwrite(ppufile:tcompilerppufile);
  1475. begin
  1476. inherited ppuwrite(ppufile);
  1477. ppufile.putbyte(byte(place));
  1478. end;
  1479. {****************************************************************************
  1480. Tai_Marker
  1481. ****************************************************************************}
  1482. constructor Tai_Marker.Create(_Kind: TAsmMarker);
  1483. begin
  1484. Inherited Create;
  1485. typ := ait_marker;
  1486. Kind := _Kind;
  1487. end;
  1488. constructor Tai_Marker.ppuload(t:taitype;ppufile:tcompilerppufile);
  1489. begin
  1490. inherited ppuload(t,ppufile);
  1491. kind:=TAsmMarker(ppufile.getbyte);
  1492. end;
  1493. procedure Tai_Marker.ppuwrite(ppufile:tcompilerppufile);
  1494. begin
  1495. inherited ppuwrite(ppufile);
  1496. ppufile.putbyte(byte(kind));
  1497. end;
  1498. {*****************************************************************************
  1499. tai_tempalloc
  1500. *****************************************************************************}
  1501. constructor tai_tempalloc.alloc(pos,size:longint);
  1502. begin
  1503. inherited Create;
  1504. typ:=ait_tempalloc;
  1505. allocation:=true;
  1506. temppos:=pos;
  1507. tempsize:=size;
  1508. {$ifdef EXTDEBUG}
  1509. problem:=nil;
  1510. {$endif EXTDEBUG}
  1511. end;
  1512. destructor tai_tempalloc.destroy;
  1513. begin
  1514. {$ifdef EXTDEBUG}
  1515. stringdispose(problem);
  1516. {$endif EXTDEBUG}
  1517. inherited destroy;
  1518. end;
  1519. constructor tai_tempalloc.dealloc(pos,size:longint);
  1520. begin
  1521. inherited Create;
  1522. typ:=ait_tempalloc;
  1523. allocation:=false;
  1524. temppos:=pos;
  1525. tempsize:=size;
  1526. {$ifdef EXTDEBUG}
  1527. problem:=nil;
  1528. {$endif EXTDEBUG}
  1529. end;
  1530. {$ifdef EXTDEBUG}
  1531. constructor tai_tempalloc.allocinfo(pos,size:longint;const st:string);
  1532. begin
  1533. inherited Create;
  1534. typ:=ait_tempalloc;
  1535. allocation:=false;
  1536. temppos:=pos;
  1537. tempsize:=size;
  1538. problem:=stringdup(st);
  1539. end;
  1540. {$endif EXTDEBUG}
  1541. constructor tai_tempalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1542. begin
  1543. inherited ppuload(t,ppufile);
  1544. temppos:=ppufile.getlongint;
  1545. tempsize:=ppufile.getlongint;
  1546. allocation:=boolean(ppufile.getbyte);
  1547. {$ifdef EXTDEBUG}
  1548. problem:=nil;
  1549. {$endif EXTDEBUG}
  1550. end;
  1551. procedure tai_tempalloc.ppuwrite(ppufile:tcompilerppufile);
  1552. begin
  1553. inherited ppuwrite(ppufile);
  1554. ppufile.putlongint(temppos);
  1555. ppufile.putlongint(tempsize);
  1556. ppufile.putbyte(byte(allocation));
  1557. end;
  1558. {*****************************************************************************
  1559. tai_regalloc
  1560. *****************************************************************************}
  1561. constructor tai_regalloc.alloc(r : tregister;ainstr:tai);
  1562. begin
  1563. inherited create;
  1564. typ:=ait_regalloc;
  1565. ratype:=ra_alloc;
  1566. reg:=r;
  1567. { ainstr must be an instruction }
  1568. if assigned(ainstr) and
  1569. (ainstr.typ<>ait_instruction) then
  1570. internalerror(200411011);
  1571. instr:=ainstr;
  1572. end;
  1573. constructor tai_regalloc.dealloc(r : tregister;ainstr:tai);
  1574. begin
  1575. inherited create;
  1576. typ:=ait_regalloc;
  1577. ratype:=ra_dealloc;
  1578. reg:=r;
  1579. { ainstr must be an instruction }
  1580. if assigned(ainstr) and
  1581. (ainstr.typ<>ait_instruction) then
  1582. internalerror(200411012);
  1583. instr:=ainstr;
  1584. end;
  1585. constructor tai_regalloc.sync(r : tregister);
  1586. begin
  1587. inherited create;
  1588. typ:=ait_regalloc;
  1589. ratype:=ra_sync;
  1590. reg:=r;
  1591. end;
  1592. constructor tai_regalloc.resize(r : tregister);
  1593. begin
  1594. inherited create;
  1595. typ:=ait_regalloc;
  1596. ratype:=ra_resize;
  1597. reg:=r;
  1598. end;
  1599. constructor tai_regalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1600. begin
  1601. inherited ppuload(t,ppufile);
  1602. ppufile.getdata(reg,sizeof(Tregister));
  1603. ratype:=tregalloctype(ppufile.getbyte);
  1604. end;
  1605. procedure tai_regalloc.ppuwrite(ppufile:tcompilerppufile);
  1606. begin
  1607. inherited ppuwrite(ppufile);
  1608. ppufile.putdata(reg,sizeof(Tregister));
  1609. ppufile.putbyte(byte(ratype));
  1610. end;
  1611. {*****************************************************************************
  1612. TaiInstruction
  1613. *****************************************************************************}
  1614. constructor tai_cpu_abstract.Create(op : tasmop);
  1615. begin
  1616. inherited create;
  1617. typ:=ait_instruction;
  1618. is_jmp:=false;
  1619. opcode:=op;
  1620. ops:=0;
  1621. fillchar(condition,sizeof(condition),0);
  1622. fillchar(oper,sizeof(oper),0);
  1623. end;
  1624. destructor tai_cpu_abstract.Destroy;
  1625. var
  1626. i : integer;
  1627. begin
  1628. for i:=0 to opercnt-1 do
  1629. freeop(i);
  1630. inherited destroy;
  1631. end;
  1632. { ---------------------------------------------------------------------
  1633. Loading of operands.
  1634. ---------------------------------------------------------------------}
  1635. procedure tai_cpu_abstract.allocate_oper(opers:longint);
  1636. begin
  1637. while (opers>opercnt) do
  1638. begin
  1639. new(oper[opercnt]);
  1640. fillchar(oper[opercnt]^,sizeof(toper),0);
  1641. inc(opercnt);
  1642. end;
  1643. end;
  1644. procedure tai_cpu_abstract.loadconst(opidx:longint;l:aint);
  1645. begin
  1646. allocate_oper(opidx+1);
  1647. with oper[opidx]^ do
  1648. begin
  1649. if typ<>top_const then
  1650. clearop(opidx);
  1651. val:=l;
  1652. typ:=top_const;
  1653. end;
  1654. end;
  1655. procedure tai_cpu_abstract.loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  1656. var
  1657. r : treference;
  1658. begin
  1659. reference_reset_symbol(r,s,sofs,1);
  1660. r.refaddr:=addr_full;
  1661. loadref(opidx,r);
  1662. end;
  1663. procedure tai_cpu_abstract.loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  1664. begin
  1665. if not assigned(s) then
  1666. internalerror(200204251);
  1667. allocate_oper(opidx+1);
  1668. with oper[opidx]^ do
  1669. begin
  1670. if typ<>top_local then
  1671. begin
  1672. clearop(opidx);
  1673. new(localoper);
  1674. end;
  1675. with oper[opidx]^.localoper^ do
  1676. begin
  1677. localsym:=s;
  1678. localsymofs:=sofs;
  1679. localindexreg:=indexreg;
  1680. localscale:=scale;
  1681. localgetoffset:=getoffset;
  1682. localforceref:=forceref;
  1683. end;
  1684. typ:=top_local;
  1685. end;
  1686. end;
  1687. procedure tai_cpu_abstract.loadref(opidx:longint;const r:treference);
  1688. begin
  1689. allocate_oper(opidx+1);
  1690. with oper[opidx]^ do
  1691. begin
  1692. if typ<>top_ref then
  1693. begin
  1694. clearop(opidx);
  1695. new(ref);
  1696. end;
  1697. ref^:=r;
  1698. {$ifdef x86}
  1699. { We allow this exception for x86, since overloading this would be
  1700. too much of a a speed penalty}
  1701. if (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  1702. segprefix:=ref^.segment;
  1703. {$endif}
  1704. if (cs_create_pic in current_settings.moduleswitches) and
  1705. assigned(r.symbol) and
  1706. not assigned(r.relsymbol) and
  1707. (r.refaddr=addr_no)
  1708. {$ifdef ARM}
  1709. and not(r.base=NR_R15)
  1710. {$endif ARM}
  1711. then
  1712. internalerror(200502052);
  1713. typ:=top_ref;
  1714. if assigned(add_reg_instruction_hook) then
  1715. begin
  1716. add_reg_instruction_hook(self,ref^.base);
  1717. add_reg_instruction_hook(self,ref^.index);
  1718. end;
  1719. { mark symbol as used }
  1720. if assigned(ref^.symbol) then
  1721. ref^.symbol.increfs;
  1722. if assigned(ref^.relsymbol) then
  1723. ref^.relsymbol.increfs;
  1724. end;
  1725. end;
  1726. procedure tai_cpu_abstract.loadreg(opidx:longint;r:tregister);
  1727. begin
  1728. allocate_oper(opidx+1);
  1729. with oper[opidx]^ do
  1730. begin
  1731. if typ<>top_reg then
  1732. clearop(opidx);
  1733. reg:=r;
  1734. typ:=top_reg;
  1735. end;
  1736. if assigned(add_reg_instruction_hook) then
  1737. add_reg_instruction_hook(self,r);
  1738. {$ifdef ARM}
  1739. { R15 is the PC on the ARM thus moves to R15 are jumps.
  1740. Due to speed considerations we don't use a virtual overridden method here.
  1741. Because the pc/r15 isn't handled by the reg. allocator this should never cause
  1742. problems with iregs getting r15.
  1743. }
  1744. is_jmp:=(opcode=A_MOV) and (opidx=0) and (r=NR_R15);
  1745. {$endif ARM}
  1746. end;
  1747. procedure tai_cpu_abstract.loadoper(opidx:longint;o:toper);
  1748. begin
  1749. allocate_oper(opidx+1);
  1750. clearop(opidx);
  1751. oper[opidx]^:=o;
  1752. { copy also the reference }
  1753. with oper[opidx]^ do
  1754. begin
  1755. case typ of
  1756. top_reg:
  1757. begin
  1758. if assigned(add_reg_instruction_hook) then
  1759. add_reg_instruction_hook(self,reg);
  1760. end;
  1761. top_ref:
  1762. begin
  1763. new(ref);
  1764. ref^:=o.ref^;
  1765. if assigned(add_reg_instruction_hook) then
  1766. begin
  1767. add_reg_instruction_hook(self,ref^.base);
  1768. add_reg_instruction_hook(self,ref^.index);
  1769. end;
  1770. end;
  1771. {$ifdef ARM}
  1772. top_shifterop:
  1773. begin
  1774. new(shifterop);
  1775. shifterop^:=o.shifterop^;
  1776. if assigned(add_reg_instruction_hook) then
  1777. add_reg_instruction_hook(self,shifterop^.rs);
  1778. end;
  1779. {$endif ARM}
  1780. end;
  1781. end;
  1782. end;
  1783. procedure tai_cpu_abstract.clearop(opidx:longint);
  1784. begin
  1785. with oper[opidx]^ do
  1786. begin
  1787. case typ of
  1788. top_ref:
  1789. dispose(ref);
  1790. top_local:
  1791. dispose(localoper);
  1792. {$ifdef ARM}
  1793. top_shifterop:
  1794. dispose(shifterop);
  1795. top_regset:
  1796. dispose(regset);
  1797. {$endif ARM}
  1798. {$ifdef jvm}
  1799. top_string:
  1800. freemem(pcval);
  1801. top_wstring:
  1802. donewidestring(pwstrval);
  1803. {$endif jvm}
  1804. end;
  1805. typ:=top_none;
  1806. end;
  1807. end;
  1808. procedure tai_cpu_abstract.freeop(opidx:longint);
  1809. begin
  1810. clearop(opidx);
  1811. dispose(oper[opidx]);
  1812. end;
  1813. { ---------------------------------------------------------------------
  1814. Miscellaneous methods.
  1815. ---------------------------------------------------------------------}
  1816. procedure tai_cpu_abstract.SetCondition(const c:TAsmCond);
  1817. begin
  1818. condition:=c;
  1819. end;
  1820. Function tai_cpu_abstract.getcopy:TLinkedListItem;
  1821. var
  1822. i : longint;
  1823. p : tai_cpu_abstract;
  1824. begin
  1825. p:=tai_cpu_abstract(inherited getcopy);
  1826. { make a copy of the references }
  1827. p.opercnt:=0;
  1828. p.allocate_oper(ops);
  1829. for i:=0 to ops-1 do
  1830. begin
  1831. p.oper[i]^:=oper[i]^;
  1832. case oper[i]^.typ of
  1833. top_local :
  1834. begin
  1835. new(p.oper[i]^.localoper);
  1836. p.oper[i]^.localoper^:=oper[i]^.localoper^;
  1837. end;
  1838. top_ref :
  1839. begin
  1840. new(p.oper[i]^.ref);
  1841. p.oper[i]^.ref^:=oper[i]^.ref^;
  1842. end;
  1843. {$ifdef ARM}
  1844. top_shifterop:
  1845. begin
  1846. new(p.oper[i]^.shifterop);
  1847. p.oper[i]^.shifterop^:=oper[i]^.shifterop^;
  1848. end;
  1849. {$endif ARM}
  1850. end;
  1851. end;
  1852. getcopy:=p;
  1853. end;
  1854. function tai_cpu_abstract.is_same_reg_move(regtype: Tregistertype):boolean;
  1855. begin
  1856. { When the generic RA is used this needs to be overridden, we don't use
  1857. virtual;abstract; to prevent a lot of warnings of unimplemented abstract methods
  1858. when tai_cpu is created (PFV) }
  1859. internalerror(200404091);
  1860. result:=false;
  1861. end;
  1862. function tai_cpu_abstract.spilling_get_operation_type(opnr: longint): topertype;
  1863. begin
  1864. internalerror(200404091);
  1865. result:=operand_readwrite;
  1866. end;
  1867. function tai_cpu_abstract.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  1868. begin
  1869. result := operand_read;
  1870. end;
  1871. constructor tai_cpu_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  1872. var
  1873. i : integer;
  1874. begin
  1875. inherited ppuload(t,ppufile);
  1876. { hopefully, we don't get problems with big/litte endian here when cross compiling :/ }
  1877. ppufile.getdata(condition,sizeof(tasmcond));
  1878. allocate_oper(ppufile.getbyte);
  1879. for i:=0 to ops-1 do
  1880. ppuloadoper(ppufile,oper[i]^);
  1881. opcode:=tasmop(ppufile.getword);
  1882. {$ifdef x86}
  1883. ppufile.getdata(segprefix,sizeof(Tregister));
  1884. {$endif x86}
  1885. is_jmp:=boolean(ppufile.getbyte);
  1886. end;
  1887. procedure tai_cpu_abstract.ppuwrite(ppufile:tcompilerppufile);
  1888. var
  1889. i : integer;
  1890. begin
  1891. inherited ppuwrite(ppufile);
  1892. ppufile.putdata(condition,sizeof(tasmcond));
  1893. ppufile.putbyte(ops);
  1894. for i:=0 to ops-1 do
  1895. ppuwriteoper(ppufile,oper[i]^);
  1896. ppufile.putword(word(opcode));
  1897. {$ifdef x86}
  1898. ppufile.putdata(segprefix,sizeof(Tregister));
  1899. {$endif x86}
  1900. ppufile.putbyte(byte(is_jmp));
  1901. end;
  1902. procedure tai_cpu_abstract.buildderefimpl;
  1903. var
  1904. i : integer;
  1905. begin
  1906. for i:=0 to ops-1 do
  1907. ppubuildderefimploper(oper[i]^);
  1908. end;
  1909. procedure tai_cpu_abstract.derefimpl;
  1910. var
  1911. i : integer;
  1912. begin
  1913. for i:=0 to ops-1 do
  1914. ppuderefoper(oper[i]^);
  1915. end;
  1916. procedure tai_cpu_abstract.resetpass1;
  1917. begin
  1918. end;
  1919. procedure tai_cpu_abstract.resetpass2;
  1920. begin
  1921. end;
  1922. function tai_cpu_abstract.Pass1(objdata:TObjData):longint;
  1923. begin
  1924. result:=0;
  1925. end;
  1926. procedure tai_cpu_abstract.Pass2(objdata:TObjData);
  1927. begin
  1928. end;
  1929. procedure tai_cpu_abstract.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  1930. begin
  1931. o.typ:=toptype(ppufile.getbyte);
  1932. o.ot:=ppufile.getlongint;
  1933. case o.typ of
  1934. top_reg :
  1935. ppufile.getdata(o.reg,sizeof(Tregister));
  1936. top_ref :
  1937. begin
  1938. new(o.ref);
  1939. {$ifdef x86}
  1940. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  1941. {$endif x86}
  1942. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  1943. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  1944. ppufile.getdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  1945. o.ref^.scalefactor:=ppufile.getbyte;
  1946. o.ref^.offset:=ppufile.getaint;
  1947. o.ref^.symbol:=ppufile.getasmsymbol;
  1948. o.ref^.relsymbol:=ppufile.getasmsymbol;
  1949. end;
  1950. top_const :
  1951. o.val:=ppufile.getaint;
  1952. top_local :
  1953. begin
  1954. new(o.localoper);
  1955. with o.localoper^ do
  1956. begin
  1957. ppufile.getderef(localsymderef);
  1958. localsymofs:=ppufile.getaint;
  1959. localindexreg:=tregister(ppufile.getlongint);
  1960. localscale:=ppufile.getbyte;
  1961. localgetoffset:=(ppufile.getbyte<>0);
  1962. end;
  1963. end;
  1964. else
  1965. internalerror(2007010210);
  1966. end;
  1967. end;
  1968. procedure tai_cpu_abstract.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  1969. begin
  1970. ppufile.putbyte(byte(o.typ));
  1971. ppufile.putlongint(o.ot);
  1972. case o.typ of
  1973. top_reg :
  1974. ppufile.putdata(o.reg,sizeof(Tregister));
  1975. top_ref :
  1976. begin
  1977. {$ifdef x86}
  1978. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  1979. {$endif x86}
  1980. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  1981. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  1982. ppufile.putdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  1983. ppufile.putbyte(o.ref^.scalefactor);
  1984. ppufile.putaint(o.ref^.offset);
  1985. ppufile.putasmsymbol(o.ref^.symbol);
  1986. ppufile.putasmsymbol(o.ref^.relsymbol);
  1987. end;
  1988. top_const :
  1989. ppufile.putaint(o.val);
  1990. top_local :
  1991. begin
  1992. with o.localoper^ do
  1993. begin
  1994. ppufile.putderef(localsymderef);
  1995. ppufile.putaint(localsymofs);
  1996. ppufile.putlongint(longint(localindexreg));
  1997. ppufile.putbyte(localscale);
  1998. ppufile.putbyte(byte(localgetoffset));
  1999. end;
  2000. end;
  2001. else
  2002. internalerror(2007010211);
  2003. end;
  2004. end;
  2005. {****************************************************************************
  2006. tai_align_abstract
  2007. ****************************************************************************}
  2008. constructor tai_align_abstract.Create(b: byte);
  2009. begin
  2010. inherited Create;
  2011. typ:=ait_align;
  2012. {$ifdef EXTDEBUG}
  2013. if upper(classname)='TAI_ALIGN_ABSTRACT' then
  2014. internalerror(200709191);
  2015. {$endif EXTDEBUG}
  2016. if b in [1,2,4,8,16,32] then
  2017. aligntype := b
  2018. else
  2019. aligntype := 1;
  2020. fillsize:=0;
  2021. fillop:=0;
  2022. use_op:=false;
  2023. end;
  2024. constructor tai_align_abstract.Create_op(b: byte; _op: byte);
  2025. begin
  2026. inherited Create;
  2027. typ:=ait_align;
  2028. if b in [1,2,4,8,16,32] then
  2029. aligntype := b
  2030. else
  2031. aligntype := 1;
  2032. fillsize:=0;
  2033. fillop:=_op;
  2034. use_op:=true;
  2035. end;
  2036. constructor tai_align_abstract.Create_zeros(b: byte);
  2037. begin
  2038. inherited Create;
  2039. typ:=ait_align;
  2040. if b in [1,2,4,8,16,32] then
  2041. aligntype := b
  2042. else
  2043. aligntype := 1;
  2044. use_op:=true;
  2045. fillsize:=0;
  2046. fillop:=0;
  2047. end;
  2048. function tai_align_abstract.calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;
  2049. begin
  2050. if fillsize>sizeof(buf) then
  2051. internalerror(200404293);
  2052. fillchar(buf,high(buf),fillop);
  2053. calculatefillbuf:=pchar(@buf);
  2054. end;
  2055. constructor tai_align_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  2056. begin
  2057. inherited ppuload(t,ppufile);
  2058. aligntype:=ppufile.getbyte;
  2059. fillsize:=0;
  2060. fillop:=ppufile.getbyte;
  2061. use_op:=boolean(ppufile.getbyte);
  2062. end;
  2063. procedure tai_align_abstract.ppuwrite(ppufile:tcompilerppufile);
  2064. begin
  2065. inherited ppuwrite(ppufile);
  2066. ppufile.putbyte(aligntype);
  2067. ppufile.putbyte(fillop);
  2068. ppufile.putbyte(byte(use_op));
  2069. end;
  2070. {****************************************************************************
  2071. tai_jvar
  2072. ****************************************************************************}
  2073. constructor tai_jvar.Create(_stackslot: longint; const _desc: shortstring; _startlab, _stoplab: TAsmSymbol);
  2074. begin
  2075. Inherited create;
  2076. typ:=ait_jvar;
  2077. stackslot:=_stackslot;
  2078. desc:=stringdup(_desc);
  2079. startlab:=_startlab;
  2080. stoplab:=_stoplab;
  2081. end;
  2082. constructor tai_jvar.ppuload(t: taitype; ppufile: tcompilerppufile);
  2083. begin
  2084. inherited ppuload(t, ppufile);
  2085. stackslot:=ppufile.getlongint;
  2086. desc:=stringdup(ppufile.getstring);
  2087. startlab:=ppufile.getasmsymbol;
  2088. stoplab:=ppufile.getasmsymbol;
  2089. end;
  2090. procedure tai_jvar.ppuwrite(ppufile: tcompilerppufile);
  2091. begin
  2092. inherited ppuwrite(ppufile);
  2093. ppufile.putlongint(stackslot);
  2094. ppufile.putstring(desc^);
  2095. ppufile.putasmsymbol(startlab);
  2096. ppufile.putasmsymbol(stoplab);
  2097. end;
  2098. {****************************************************************************
  2099. tai_jvar
  2100. ****************************************************************************}
  2101. constructor tai_jcatch.Create(const _name: shortstring; _startlab, _stoplab, _handlerlab: TAsmSymbol);
  2102. begin
  2103. Inherited create;
  2104. typ:=ait_jcatch;
  2105. name:=stringdup(_name);
  2106. startlab:=_startlab;
  2107. startlab.increfs;
  2108. stoplab:=_stoplab;
  2109. stoplab.increfs;
  2110. handlerlab:=_handlerlab;
  2111. handlerlab.increfs;
  2112. end;
  2113. constructor tai_jcatch.ppuload(t: taitype; ppufile: tcompilerppufile);
  2114. begin
  2115. inherited ppuload(t, ppufile);
  2116. name:=stringdup(ppufile.getstring);
  2117. startlab:=ppufile.getasmsymbol;
  2118. startlab.increfs;
  2119. stoplab:=ppufile.getasmsymbol;
  2120. stoplab.increfs;
  2121. handlerlab:=ppufile.getasmsymbol;
  2122. handlerlab.increfs;
  2123. end;
  2124. procedure tai_jcatch.ppuwrite(ppufile: tcompilerppufile);
  2125. begin
  2126. inherited ppuwrite(ppufile);
  2127. ppufile.putstring(name^);
  2128. ppufile.putasmsymbol(startlab);
  2129. ppufile.putasmsymbol(stoplab);
  2130. ppufile.putasmsymbol(handlerlab);
  2131. end;
  2132. end.