aasmtai.pas 70 KB

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