aasmtai.pas 69 KB

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