aasmtai.pas 77 KB

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