aasmtai.pas 69 KB

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