aasmtai.pas 68 KB

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