aasmtai.pas 72 KB

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