aasmtai.pas 69 KB

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