aasmtai.pas 77 KB

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