aasmtai.pas 74 KB

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