aasmtai.pas 69 KB

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