aasmtai.pas 68 KB

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