aasmtai.pas 69 KB

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