aasmtai.pas 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements an abstract asmoutput class for all processor types
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. { @abstract(This unit implements an abstract asm output class for all processor types)
  19. This unit implements an abstract assembler output class for all processors, these
  20. are then overriden for each assembler writer to actually write the data in these
  21. classes to an assembler file.
  22. }
  23. unit aasmtai;
  24. {$i fpcdefs.inc}
  25. interface
  26. uses
  27. cutils,cclasses,
  28. globtype,globals,systems,
  29. cpuinfo,cpubase,
  30. cgbase,
  31. symppu,symtype,
  32. aasmbase;
  33. type
  34. taitype = (
  35. ait_none,
  36. ait_align,
  37. ait_section,
  38. ait_comment,
  39. ait_direct,
  40. ait_string,
  41. ait_instruction,
  42. ait_datablock,
  43. ait_symbol,
  44. ait_symbol_end, { needed to calc the size of a symbol }
  45. ait_label,
  46. ait_const_32bit,
  47. ait_const_16bit,
  48. ait_const_8bit,
  49. ait_const_symbol,
  50. { the following is only used by the win32 version of the compiler }
  51. { and only the GNU AS Win32 is able to write it }
  52. ait_const_rva,
  53. ait_real_32bit,
  54. ait_real_64bit,
  55. ait_real_80bit,
  56. ait_comp_64bit,
  57. ait_real_128bit,
  58. {$ifdef GDB}
  59. ait_stabn,
  60. ait_stabs,
  61. ait_force_line,
  62. ait_stab_function_name,
  63. {$endif GDB}
  64. {$ifdef alpha}
  65. { the follow is for the DEC Alpha }
  66. ait_frame,
  67. ait_ent,
  68. {$endif alpha}
  69. {$ifdef ia64}
  70. ait_bundle,
  71. ait_stop,
  72. {$endif ia64}
  73. {$ifdef m68k}
  74. ait_labeled_instruction,
  75. {$endif m68k}
  76. ait_cut, { used to split into tiny assembler files }
  77. ait_regalloc,
  78. ait_tempalloc,
  79. ait_marker { used to mark assembler blocks and inlined functions }
  80. );
  81. const
  82. taitypestr : array[taitype] of string[14] = (
  83. '<none>',
  84. 'align',
  85. 'section',
  86. 'comment',
  87. 'direct',
  88. 'string',
  89. 'instruction',
  90. 'datablock',
  91. 'symbol',
  92. 'symbol_end',
  93. 'label',
  94. 'const_32bit',
  95. 'const_16bit',
  96. 'const_8bit',
  97. 'const_symbol',
  98. 'const_rva',
  99. 'real_32bit',
  100. 'real_64bit',
  101. 'real_80bit',
  102. 'comp_64bit',
  103. 'real_128bit',
  104. {$ifdef GDB}
  105. 'stabn',
  106. 'stabs',
  107. 'force_line',
  108. 'stab_funcname',
  109. {$endif GDB}
  110. {$ifdef alpha}
  111. { the follow is for the DEC Alpha }
  112. 'frame',
  113. 'ent',
  114. {$endif alpha}
  115. {$ifdef ia64}
  116. 'bundle',
  117. 'stop',
  118. {$endif ia64}
  119. {$ifdef m68k}
  120. 'labeled_instr',
  121. {$endif m68k}
  122. 'cut',
  123. 'regalloc',
  124. 'tempalloc',
  125. 'marker'
  126. );
  127. type
  128. { Types of operand }
  129. toptype=(top_none,top_reg,top_ref,top_const,top_symbol,top_bool,top_local,
  130. { ARM only }
  131. top_regset,
  132. { ARM only }
  133. top_shifterop);
  134. { kinds of operations that an instruction can perform on an operand }
  135. topertype = (operand_read,operand_write,operand_readwrite);
  136. toper=record
  137. ot : longint;
  138. case typ : toptype of
  139. top_none : ();
  140. top_reg : (reg:tregister);
  141. top_ref : (ref:preference);
  142. top_const : (val:aword);
  143. top_symbol : (sym:tasmsymbol;symofs:longint);
  144. top_bool : (b:boolean);
  145. { local varsym that will be inserted in pass_2 }
  146. top_local : (localsym:pointer;localsymderef:tderef;localsymofs:longint;localindexreg:tregister;
  147. localscale:byte;localgetoffset:boolean);
  148. {$ifdef arm}
  149. top_regset : (regset:^tcpuregisterset);
  150. top_shifterop : (shifterop : pshifterop);
  151. {$endif arm}
  152. end;
  153. poper=^toper;
  154. { ait_* types which don't result in executable code or which don't influence }
  155. { the way the program runs/behaves, but which may be encountered by the }
  156. { optimizer (= if it's sometimes added to the exprasm list). Update if you add }
  157. { a new ait type! }
  158. const
  159. SkipInstr = [ait_comment, ait_symbol,ait_section
  160. {$ifdef GDB}
  161. ,ait_stabs, ait_stabn, ait_stab_function_name, ait_force_line
  162. {$endif GDB}
  163. ,ait_regalloc, ait_tempalloc, ait_symbol_end];
  164. { ait_* types which do not have line information (and hence which are of type
  165. tai, otherwise, they are of type tailineinfo }
  166. { ait_* types which do not have line information (and hence which are of type
  167. tai, otherwise, they are of type tailineinfo }
  168. SkipLineInfo =[ait_label,
  169. ait_regalloc,ait_tempalloc,
  170. {$ifdef GDB}
  171. ait_stabn,ait_stabs,ait_stab_function_name,
  172. {$endif GDB}
  173. ait_cut,ait_marker,ait_align,ait_section,ait_comment,
  174. ait_const_8bit,ait_const_16bit,ait_const_32bit,
  175. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_real_128bit
  176. ];
  177. type
  178. { cut type, required for alphanumeric ordering of the assembler filenames }
  179. TCutPlace=(cut_normal,cut_begin,cut_end);
  180. TMarker = (NoPropInfoStart,NoPropInfoEnd,
  181. AsmBlockStart,AsmBlockEnd,
  182. InlineStart,InlineEnd,marker_blockstart,
  183. marker_position);
  184. { Buffer type used for alignment }
  185. tfillbuffer = array[0..63] of char;
  186. Tspill_temp_list=array[tsuperregister] of Treference;
  187. { abstract assembler item }
  188. tai = class(TLinkedListItem)
  189. {$ifndef NOOPT}
  190. { pointer to record with optimizer info about this tai object }
  191. optinfo : pointer;
  192. {$endif NOOPT}
  193. typ : taitype;
  194. constructor Create;
  195. constructor ppuload(t:taitype;ppufile:tcompilerppufile);virtual;
  196. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  197. procedure buildderefimpl;virtual;
  198. procedure derefimpl;virtual;
  199. end;
  200. { abstract assembler item with line information }
  201. tailineinfo = class(tai)
  202. fileinfo : tfileposinfo;
  203. constructor Create;
  204. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  205. procedure ppuwrite(ppufile:tcompilerppufile);override;
  206. end;
  207. taiclass = class of tai;
  208. taiclassarray = array[taitype] of taiclass;
  209. { Generates an assembler string }
  210. tai_string = class(tailineinfo)
  211. str : pchar;
  212. { extra len so the string can contain an \0 }
  213. len : longint;
  214. constructor Create(const _str : string);
  215. constructor Create_pchar(_str : pchar);
  216. constructor Create_length_pchar(_str : pchar;length : longint);
  217. destructor Destroy;override;
  218. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  219. procedure ppuwrite(ppufile:tcompilerppufile);override;
  220. function getcopy:tlinkedlistitem;override;
  221. end;
  222. { Generates a common label }
  223. tai_symbol = class(tailineinfo)
  224. is_global : boolean;
  225. sym : tasmsymbol;
  226. size : longint;
  227. constructor Create(_sym:tasmsymbol;siz:longint);
  228. constructor Create_Global(_sym:tasmsymbol;siz:longint);
  229. constructor Createname(const _name : string;siz:longint);
  230. constructor Createname_global(const _name : string;siz:longint);
  231. constructor Createdataname(const _name : string;siz:longint);
  232. constructor Createdataname_global(const _name : string;siz:longint);
  233. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  234. procedure ppuwrite(ppufile:tcompilerppufile);override;
  235. procedure derefimpl;override;
  236. end;
  237. tai_symbol_end = class(tailineinfo)
  238. sym : tasmsymbol;
  239. constructor Create(_sym:tasmsymbol);
  240. constructor Createname(const _name : string);
  241. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  242. procedure ppuwrite(ppufile:tcompilerppufile);override;
  243. procedure derefimpl;override;
  244. end;
  245. { Generates an assembler label }
  246. tai_label = class(tai)
  247. is_global : boolean;
  248. l : tasmlabel;
  249. constructor Create(_l : tasmlabel);
  250. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  251. procedure ppuwrite(ppufile:tcompilerppufile);override;
  252. procedure derefimpl;override;
  253. end;
  254. { Directly output data to final assembler file }
  255. tai_direct = class(tailineinfo)
  256. str : pchar;
  257. constructor Create(_str : pchar);
  258. destructor Destroy; override;
  259. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  260. procedure ppuwrite(ppufile:tcompilerppufile);override;
  261. function getcopy:tlinkedlistitem;override;
  262. end;
  263. { Generates an assembler comment }
  264. tai_comment = class(tai)
  265. str : pchar;
  266. constructor Create(_str : pchar);
  267. destructor Destroy; override;
  268. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  269. procedure ppuwrite(ppufile:tcompilerppufile);override;
  270. function getcopy:tlinkedlistitem;override;
  271. end;
  272. { Generates a section / segment directive }
  273. tai_section = class(tai)
  274. sec : TSection;
  275. constructor Create(s : TSection);
  276. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  277. procedure ppuwrite(ppufile:tcompilerppufile);override;
  278. end;
  279. { Generates an uninitializised data block }
  280. tai_datablock = class(tailineinfo)
  281. is_global : boolean;
  282. sym : tasmsymbol;
  283. size : longint;
  284. constructor Create(const _name : string;_size : longint);
  285. constructor Create_global(const _name : string;_size : longint);
  286. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  287. procedure ppuwrite(ppufile:tcompilerppufile);override;
  288. procedure derefimpl;override;
  289. end;
  290. { Generates a long integer (32 bit) }
  291. tai_const = class(tai)
  292. value : cardinal;
  293. constructor Create_32bit(_value : cardinal);
  294. constructor Create_16bit(_value : word);
  295. constructor Create_8bit(_value : byte);
  296. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  297. procedure ppuwrite(ppufile:tcompilerppufile);override;
  298. end;
  299. tai_const_symbol = class(tailineinfo)
  300. sym : tasmsymbol;
  301. offset : longint;
  302. constructor Create(_sym:tasmsymbol);
  303. constructor Create_offset(_sym:tasmsymbol;ofs:longint);
  304. constructor Create_rva(_sym:tasmsymbol);
  305. constructor Createname(const name:string);
  306. constructor Createname_offset(const name:string;ofs:longint);
  307. constructor Createname_rva(const name:string);
  308. constructor Createdataname(const name:string);
  309. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  310. procedure ppuwrite(ppufile:tcompilerppufile);override;
  311. procedure derefimpl;override;
  312. function getcopy:tlinkedlistitem;override;
  313. end;
  314. { Generates a single float (32 bit real) }
  315. tai_real_32bit = class(tai)
  316. value : ts32real;
  317. constructor Create(_value : ts32real);
  318. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  319. procedure ppuwrite(ppufile:tcompilerppufile);override;
  320. end;
  321. { Generates a double float (64 bit real) }
  322. tai_real_64bit = class(tai)
  323. value : ts64real;
  324. constructor Create(_value : ts64real);
  325. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  326. procedure ppuwrite(ppufile:tcompilerppufile);override;
  327. end;
  328. { Generates an extended float (80 bit real) }
  329. tai_real_80bit = class(tai)
  330. value : ts80real;
  331. constructor Create(_value : ts80real);
  332. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  333. procedure ppuwrite(ppufile:tcompilerppufile);override;
  334. end;
  335. { Generates an extended float (128 bit real) }
  336. tai_real_128bit = class(tai)
  337. value : ts128real;
  338. constructor Create(_value : ts128real);
  339. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  340. procedure ppuwrite(ppufile:tcompilerppufile);override;
  341. end;
  342. { Generates a comp int (integer over 64 bits)
  343. This is Intel 80x86 specific, and is not
  344. really supported on other processors.
  345. }
  346. tai_comp_64bit = class(tai)
  347. value : ts64comp;
  348. constructor Create(_value : ts64comp);
  349. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  350. procedure ppuwrite(ppufile:tcompilerppufile);override;
  351. end;
  352. { Insert a cut to split assembler into several smaller files }
  353. tai_cut = class(tai)
  354. place : tcutplace;
  355. constructor Create;
  356. constructor Create_begin;
  357. constructor Create_end;
  358. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  359. procedure ppuwrite(ppufile:tcompilerppufile);override;
  360. end;
  361. { Insert a marker for assembler and inline blocks }
  362. tai_marker = class(tai)
  363. Kind: TMarker;
  364. Constructor Create(_Kind: TMarker);
  365. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  366. procedure ppuwrite(ppufile:tcompilerppufile);override;
  367. end;
  368. tai_tempalloc = class(tai)
  369. allocation : boolean;
  370. {$ifdef EXTDEBUG}
  371. problem : pstring;
  372. {$endif EXTDEBUG}
  373. temppos,
  374. tempsize : longint;
  375. constructor alloc(pos,size:longint);
  376. constructor dealloc(pos,size:longint);
  377. {$ifdef EXTDEBUG}
  378. constructor allocinfo(pos,size:longint;const st:string);
  379. {$endif EXTDEBUG}
  380. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  381. destructor destroy;override;
  382. procedure ppuwrite(ppufile:tcompilerppufile);override;
  383. end;
  384. tai_regalloc = class(tai)
  385. allocation : boolean;
  386. reg : tregister;
  387. constructor alloc(r : tregister);
  388. constructor dealloc(r : tregister);
  389. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  390. procedure ppuwrite(ppufile:tcompilerppufile);override;
  391. end;
  392. Taasmoutput=class;
  393. tadd_reg_instruction_proc=procedure(instr:Tai;r:tregister) of object;
  394. Trggetproc=procedure(list:Taasmoutput;position:Tai;subreg:Tsubregister;var result:Tregister) of object;
  395. Trgungetproc=procedure(list:Taasmoutput;position:Tai;r:Tregister) of object;
  396. { Class template for assembler instructions
  397. }
  398. taicpu_abstract = class(tailineinfo)
  399. protected
  400. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);virtual;abstract;
  401. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);virtual;abstract;
  402. procedure ppubuildderefimploper(var o:toper);virtual;abstract;
  403. procedure ppuderefoper(var o:toper);virtual;abstract;
  404. public
  405. { Condition flags for instruction }
  406. condition : TAsmCond;
  407. { Number of operands to instruction }
  408. ops : byte;
  409. { Number of allocate oper structures }
  410. opercnt : byte;
  411. { Operands of instruction }
  412. oper : array[0..max_operands-1] of poper;
  413. { Actual opcode of instruction }
  414. opcode : tasmop;
  415. {$ifdef x86}
  416. segprefix : tregister;
  417. {$endif x86}
  418. { true if instruction is a jmp }
  419. is_jmp : boolean; { is this instruction a jump? (needed for optimizer) }
  420. Constructor Create(op : tasmop);
  421. Destructor Destroy;override;
  422. function getcopy:TLinkedListItem;override;
  423. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  424. procedure ppuwrite(ppufile:tcompilerppufile);override;
  425. procedure buildderefimpl;override;
  426. procedure derefimpl;override;
  427. procedure SetCondition(const c:TAsmCond);
  428. procedure allocate_oper(opers:longint);
  429. procedure loadconst(opidx:longint;l:aword);
  430. procedure loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  431. procedure loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset:boolean);
  432. procedure loadref(opidx:longint;const r:treference);
  433. procedure loadreg(opidx:longint;r:tregister);
  434. procedure loadoper(opidx:longint;o:toper);
  435. procedure clearop(opidx:longint);
  436. function is_reg_move:boolean;virtual;abstract;
  437. function is_same_reg_move:boolean;virtual;abstract;
  438. { register allocator }
  439. function get_insert_pos(p:Tai;huntfor1,huntfor2,huntfor3:Tsuperregister;var live_registers_int:Tsuperregisterworklist):Tai;
  440. procedure forward_allocation(p:Tai;var live_registers_int:Tsuperregisterworklist);
  441. function spill_registers(list:Taasmoutput;
  442. rt:Tregistertype;
  443. rgget:Trggetproc;
  444. rgunget:Trgungetproc;
  445. const r:tsuperregisterset;
  446. var live_registers_int:Tsuperregisterworklist;
  447. const spilltemplist:Tspill_temp_list):boolean;virtual;
  448. function spilling_decode_loadstore(op: tasmop; var counterpart: tasmop; var wasload: boolean): boolean;virtual;abstract;
  449. function spilling_create_loadstore(op: tasmop; r:tregister; const ref:treference): tai;virtual;abstract;
  450. function spilling_create_load(const ref:treference;r:tregister): tai;virtual;abstract;
  451. function spilling_create_store(r:tregister; const ref:treference): tai;virtual;abstract;
  452. function spilling_get_operation_type(opnr: longint): topertype;virtual;abstract;
  453. end;
  454. { alignment for operator }
  455. tai_align_abstract = class(tai)
  456. aligntype : byte; { 1 = no align, 2 = word align, 4 = dword align }
  457. fillsize : byte; { real size to fill }
  458. fillop : byte; { value to fill with - optional }
  459. use_op : boolean;
  460. constructor Create(b:byte);
  461. constructor Create_op(b: byte; _op: byte);
  462. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  463. procedure ppuwrite(ppufile:tcompilerppufile);override;
  464. function calculatefillbuf(var buf : tfillbuffer):pchar;virtual;
  465. end;
  466. taasmoutput = class(tlinkedlist)
  467. constructor create;
  468. function getlasttaifilepos : pfileposinfo;
  469. procedure InsertAfter(Item,Loc : TLinkedListItem);
  470. end;
  471. var
  472. { array with all class types for tais }
  473. aiclass : taiclassarray;
  474. { Current expression list }
  475. exprasmlist : taasmoutput;
  476. { labels for BREAK and CONTINUE }
  477. aktbreaklabel,aktcontinuelabel : tasmlabel;
  478. { label when the result is true or false }
  479. truelabel,falselabel : tasmlabel;
  480. { hook to notify uses of registers }
  481. add_reg_instruction_hook : tadd_reg_instruction_proc;
  482. { default lists }
  483. datasegment,codesegment,bsssegment,
  484. debuglist,withdebuglist,consts,
  485. importssection,exportssection,
  486. resourcesection,rttilist,
  487. resourcestringlist : taasmoutput;
  488. function ppuloadai(ppufile:tcompilerppufile):tai;
  489. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  490. implementation
  491. uses
  492. {$ifdef delphi}
  493. sysutils,
  494. {$else}
  495. strings,
  496. {$endif}
  497. verbose;
  498. const
  499. pputaimarker = 254;
  500. {****************************************************************************
  501. Helpers
  502. ****************************************************************************}
  503. function ppuloadai(ppufile:tcompilerppufile):tai;
  504. var
  505. b : byte;
  506. t : taitype;
  507. begin
  508. { marker }
  509. b:=ppufile.getbyte;
  510. if b<>pputaimarker then
  511. internalerror(200208181);
  512. { load nodetype }
  513. t:=taitype(ppufile.getbyte);
  514. if t<>ait_none then
  515. begin
  516. if t>high(taitype) then
  517. internalerror(200208182);
  518. if not assigned(aiclass[t]) then
  519. internalerror(200208183);
  520. {writeln('taiload: ',taitypestr[t]);}
  521. { generate tai of the correct class }
  522. ppuloadai:=aiclass[t].ppuload(t,ppufile);
  523. end
  524. else
  525. ppuloadai:=nil;
  526. end;
  527. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  528. begin
  529. { marker, read by ppuloadnode }
  530. ppufile.putbyte(pputaimarker);
  531. if assigned(n) then
  532. begin
  533. { type, read by ppuloadnode }
  534. ppufile.putbyte(byte(n.typ));
  535. {writeln('taiwrite: ',taitypestr[n.typ]);}
  536. n.ppuwrite(ppufile);
  537. end
  538. else
  539. ppufile.putbyte(byte(ait_none));
  540. end;
  541. {****************************************************************************
  542. TAI
  543. ****************************************************************************}
  544. constructor tai.Create;
  545. begin
  546. {$ifndef NOOPT}
  547. optinfo:=nil;
  548. {$endif NOOPT}
  549. end;
  550. constructor tai.ppuload(t:taitype;ppufile:tcompilerppufile);
  551. begin
  552. typ:=t;
  553. {$ifndef NOOPT}
  554. optinfo:=nil;
  555. {$endif}
  556. end;
  557. procedure tai.ppuwrite(ppufile:tcompilerppufile);
  558. begin
  559. end;
  560. procedure tai.buildderefimpl;
  561. begin
  562. end;
  563. procedure tai.derefimpl;
  564. begin
  565. end;
  566. {****************************************************************************
  567. TAILINEINFO
  568. ****************************************************************************}
  569. constructor tailineinfo.create;
  570. begin
  571. inherited create;
  572. fileinfo:=aktfilepos;
  573. end;
  574. constructor tailineinfo.ppuload(t:taitype;ppufile:tcompilerppufile);
  575. begin
  576. inherited ppuload(t,ppufile);
  577. ppufile.getposinfo(fileinfo);
  578. end;
  579. procedure tailineinfo.ppuwrite(ppufile:tcompilerppufile);
  580. begin
  581. inherited ppuwrite(ppufile);
  582. ppufile.putposinfo(fileinfo);
  583. end;
  584. {****************************************************************************
  585. TAI_SECTION
  586. ****************************************************************************}
  587. constructor tai_section.Create(s : TSection);
  588. begin
  589. inherited Create;
  590. typ:=ait_section;
  591. sec:=s;
  592. end;
  593. constructor tai_section.ppuload(t:taitype;ppufile:tcompilerppufile);
  594. begin
  595. inherited ppuload(t,ppufile);
  596. sec:=tsection(ppufile.getbyte);
  597. end;
  598. procedure tai_section.ppuwrite(ppufile:tcompilerppufile);
  599. begin
  600. inherited ppuwrite(ppufile);
  601. ppufile.putbyte(byte(sec));
  602. end;
  603. {****************************************************************************
  604. TAI_DATABLOCK
  605. ****************************************************************************}
  606. constructor tai_datablock.Create(const _name : string;_size : longint);
  607. begin
  608. inherited Create;
  609. typ:=ait_datablock;
  610. sym:=objectlibrary.newasmsymboltype(_name,AB_LOCAL,AT_DATA);
  611. { keep things aligned }
  612. if _size<=0 then
  613. _size:=4;
  614. size:=_size;
  615. is_global:=false;
  616. end;
  617. constructor tai_datablock.Create_global(const _name : string;_size : longint);
  618. begin
  619. inherited Create;
  620. typ:=ait_datablock;
  621. sym:=objectlibrary.newasmsymboltype(_name,AB_GLOBAL,AT_DATA);
  622. { keep things aligned }
  623. if _size<=0 then
  624. _size:=4;
  625. size:=_size;
  626. is_global:=true;
  627. end;
  628. constructor tai_datablock.ppuload(t:taitype;ppufile:tcompilerppufile);
  629. begin
  630. inherited Create;
  631. sym:=ppufile.getasmsymbol;
  632. size:=ppufile.getlongint;
  633. is_global:=boolean(ppufile.getbyte);
  634. end;
  635. procedure tai_datablock.ppuwrite(ppufile:tcompilerppufile);
  636. begin
  637. inherited ppuwrite(ppufile);
  638. ppufile.putasmsymbol(sym);
  639. ppufile.putlongint(size);
  640. ppufile.putbyte(byte(is_global));
  641. end;
  642. procedure tai_datablock.derefimpl;
  643. begin
  644. objectlibrary.DerefAsmsymbol(sym);
  645. end;
  646. {****************************************************************************
  647. TAI_SYMBOL
  648. ****************************************************************************}
  649. constructor tai_symbol.Create(_sym:tasmsymbol;siz:longint);
  650. begin
  651. inherited Create;
  652. typ:=ait_symbol;
  653. sym:=_sym;
  654. size:=siz;
  655. sym.defbind:=AB_LOCAL;
  656. is_global:=false;
  657. end;
  658. constructor tai_symbol.Create_global(_sym:tasmsymbol;siz:longint);
  659. begin
  660. inherited Create;
  661. typ:=ait_symbol;
  662. sym:=_sym;
  663. size:=siz;
  664. sym.defbind:=AB_GLOBAL;
  665. is_global:=true;
  666. end;
  667. constructor tai_symbol.Createname(const _name : string;siz:longint);
  668. begin
  669. inherited Create;
  670. typ:=ait_symbol;
  671. sym:=objectlibrary.newasmsymboltype(_name,AB_LOCAL,AT_FUNCTION);
  672. size:=siz;
  673. is_global:=false;
  674. end;
  675. constructor tai_symbol.Createname_global(const _name : string;siz:longint);
  676. begin
  677. inherited Create;
  678. typ:=ait_symbol;
  679. sym:=objectlibrary.newasmsymboltype(_name,AB_GLOBAL,AT_FUNCTION);
  680. size:=siz;
  681. is_global:=true;
  682. end;
  683. constructor tai_symbol.Createdataname(const _name : string;siz:longint);
  684. begin
  685. inherited Create;
  686. typ:=ait_symbol;
  687. sym:=objectlibrary.newasmsymboltype(_name,AB_LOCAL,AT_DATA);
  688. size:=siz;
  689. is_global:=false;
  690. end;
  691. constructor tai_symbol.Createdataname_global(const _name : string;siz:longint);
  692. begin
  693. inherited Create;
  694. typ:=ait_symbol;
  695. sym:=objectlibrary.newasmsymboltype(_name,AB_GLOBAL,AT_DATA);
  696. size:=siz;
  697. is_global:=true;
  698. end;
  699. constructor tai_symbol.ppuload(t:taitype;ppufile:tcompilerppufile);
  700. begin
  701. inherited ppuload(t,ppufile);
  702. sym:=ppufile.getasmsymbol;
  703. size:=ppufile.getlongint;
  704. is_global:=boolean(ppufile.getbyte);
  705. end;
  706. procedure tai_symbol.ppuwrite(ppufile:tcompilerppufile);
  707. begin
  708. inherited ppuwrite(ppufile);
  709. ppufile.putasmsymbol(sym);
  710. ppufile.putlongint(size);
  711. ppufile.putbyte(byte(is_global));
  712. end;
  713. procedure tai_symbol.derefimpl;
  714. begin
  715. objectlibrary.DerefAsmsymbol(sym);
  716. end;
  717. {****************************************************************************
  718. TAI_SYMBOL
  719. ****************************************************************************}
  720. constructor tai_symbol_end.Create(_sym:tasmsymbol);
  721. begin
  722. inherited Create;
  723. typ:=ait_symbol_end;
  724. sym:=_sym;
  725. end;
  726. constructor tai_symbol_end.Createname(const _name : string);
  727. begin
  728. inherited Create;
  729. typ:=ait_symbol_end;
  730. sym:=objectlibrary.newasmsymboltype(_name,AB_GLOBAL,AT_NONE);
  731. end;
  732. constructor tai_symbol_end.ppuload(t:taitype;ppufile:tcompilerppufile);
  733. begin
  734. inherited ppuload(t,ppufile);
  735. sym:=ppufile.getasmsymbol;
  736. end;
  737. procedure tai_symbol_end.ppuwrite(ppufile:tcompilerppufile);
  738. begin
  739. inherited ppuwrite(ppufile);
  740. ppufile.putasmsymbol(sym);
  741. end;
  742. procedure tai_symbol_end.derefimpl;
  743. begin
  744. objectlibrary.DerefAsmsymbol(sym);
  745. end;
  746. {****************************************************************************
  747. TAI_CONST
  748. ****************************************************************************}
  749. constructor tai_const.Create_32bit(_value : cardinal);
  750. begin
  751. inherited Create;
  752. typ:=ait_const_32bit;
  753. value:=_value;
  754. end;
  755. constructor tai_const.Create_16bit(_value : word);
  756. begin
  757. inherited Create;
  758. typ:=ait_const_16bit;
  759. value:=_value;
  760. end;
  761. constructor tai_const.Create_8bit(_value : byte);
  762. begin
  763. inherited Create;
  764. typ:=ait_const_8bit;
  765. value:=_value;
  766. end;
  767. constructor tai_const.ppuload(t:taitype;ppufile:tcompilerppufile);
  768. begin
  769. inherited ppuload(t,ppufile);
  770. value:=ppufile.getlongint;
  771. end;
  772. procedure tai_const.ppuwrite(ppufile:tcompilerppufile);
  773. begin
  774. inherited ppuwrite(ppufile);
  775. ppufile.putlongint(value);
  776. end;
  777. {****************************************************************************
  778. TAI_CONST_SYMBOL
  779. ****************************************************************************}
  780. constructor tai_const_symbol.Create(_sym:tasmsymbol);
  781. begin
  782. inherited Create;
  783. typ:=ait_const_symbol;
  784. sym:=_sym;
  785. offset:=0;
  786. { update sym info }
  787. sym.increfs;
  788. end;
  789. constructor tai_const_symbol.Create_offset(_sym:tasmsymbol;ofs:longint);
  790. begin
  791. inherited Create;
  792. typ:=ait_const_symbol;
  793. sym:=_sym;
  794. offset:=ofs;
  795. { update sym info }
  796. sym.increfs;
  797. end;
  798. constructor tai_const_symbol.Create_rva(_sym:tasmsymbol);
  799. begin
  800. inherited Create;
  801. typ:=ait_const_rva;
  802. sym:=_sym;
  803. offset:=0;
  804. { update sym info }
  805. sym.increfs;
  806. end;
  807. constructor tai_const_symbol.Createname(const name:string);
  808. begin
  809. inherited Create;
  810. typ:=ait_const_symbol;
  811. sym:=objectlibrary.newasmsymbol(name);
  812. offset:=0;
  813. { update sym info }
  814. sym.increfs;
  815. end;
  816. constructor tai_const_symbol.Createname_offset(const name:string;ofs:longint);
  817. begin
  818. inherited Create;
  819. typ:=ait_const_symbol;
  820. sym:=objectlibrary.newasmsymbol(name);
  821. offset:=ofs;
  822. { update sym info }
  823. sym.increfs;
  824. end;
  825. constructor tai_const_symbol.Createname_rva(const name:string);
  826. begin
  827. inherited Create;
  828. typ:=ait_const_rva;
  829. sym:=objectlibrary.newasmsymbol(name);
  830. offset:=0;
  831. { update sym info }
  832. sym.increfs;
  833. end;
  834. constructor tai_const_symbol.Createdataname(const name:string);
  835. begin
  836. inherited Create;
  837. typ:=ait_const_symbol;
  838. sym:=objectlibrary.newasmsymboltype(name,AB_EXTERNAL,AT_DATA);
  839. offset:=0;
  840. { update sym info }
  841. sym.increfs;
  842. end;
  843. constructor tai_const_symbol.ppuload(t:taitype;ppufile:tcompilerppufile);
  844. begin
  845. inherited ppuload(t,ppufile);
  846. sym:=ppufile.getasmsymbol;
  847. offset:=ppufile.getlongint;
  848. end;
  849. procedure tai_const_symbol.ppuwrite(ppufile:tcompilerppufile);
  850. begin
  851. inherited ppuwrite(ppufile);
  852. ppufile.putasmsymbol(sym);
  853. ppufile.putlongint(offset);
  854. end;
  855. procedure tai_const_symbol.derefimpl;
  856. begin
  857. objectlibrary.DerefAsmsymbol(sym);
  858. end;
  859. function tai_const_symbol.getcopy:tlinkedlistitem;
  860. begin
  861. getcopy:=inherited getcopy;
  862. { we need to increase the reference number }
  863. sym.increfs;
  864. end;
  865. {****************************************************************************
  866. TAI_real_32bit
  867. ****************************************************************************}
  868. constructor tai_real_32bit.Create(_value : ts32real);
  869. begin
  870. inherited Create;
  871. typ:=ait_real_32bit;
  872. value:=_value;
  873. end;
  874. constructor tai_real_32bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  875. begin
  876. inherited ppuload(t,ppufile);
  877. value:=ppufile.getreal;
  878. end;
  879. procedure tai_real_32bit.ppuwrite(ppufile:tcompilerppufile);
  880. begin
  881. inherited ppuwrite(ppufile);
  882. ppufile.putreal(value);
  883. end;
  884. {****************************************************************************
  885. TAI_real_64bit
  886. ****************************************************************************}
  887. constructor tai_real_64bit.Create(_value : ts64real);
  888. begin
  889. inherited Create;
  890. typ:=ait_real_64bit;
  891. value:=_value;
  892. end;
  893. constructor tai_real_64bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  894. begin
  895. inherited ppuload(t,ppufile);
  896. value:=ppufile.getreal;
  897. end;
  898. procedure tai_real_64bit.ppuwrite(ppufile:tcompilerppufile);
  899. begin
  900. inherited ppuwrite(ppufile);
  901. ppufile.putreal(value);
  902. end;
  903. {****************************************************************************
  904. TAI_real_80bit
  905. ****************************************************************************}
  906. constructor tai_real_80bit.Create(_value : ts80real);
  907. begin
  908. inherited Create;
  909. typ:=ait_real_80bit;
  910. value:=_value;
  911. end;
  912. constructor tai_real_80bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  913. begin
  914. inherited ppuload(t,ppufile);
  915. value:=ppufile.getreal;
  916. end;
  917. procedure tai_real_80bit.ppuwrite(ppufile:tcompilerppufile);
  918. begin
  919. inherited ppuwrite(ppufile);
  920. ppufile.putreal(value);
  921. end;
  922. {****************************************************************************
  923. TAI_real_80bit
  924. ****************************************************************************}
  925. constructor tai_real_128bit.Create(_value : ts128real);
  926. begin
  927. inherited Create;
  928. typ:=ait_real_128bit;
  929. value:=_value;
  930. end;
  931. constructor tai_real_128bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  932. begin
  933. inherited ppuload(t,ppufile);
  934. value:=ppufile.getreal;
  935. end;
  936. procedure tai_real_128bit.ppuwrite(ppufile:tcompilerppufile);
  937. begin
  938. inherited ppuwrite(ppufile);
  939. ppufile.putreal(value);
  940. end;
  941. {****************************************************************************
  942. Tai_comp_64bit
  943. ****************************************************************************}
  944. constructor tai_comp_64bit.Create(_value : ts64comp);
  945. begin
  946. inherited Create;
  947. typ:=ait_comp_64bit;
  948. value:=_value;
  949. end;
  950. constructor tai_comp_64bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  951. begin
  952. inherited ppuload(t,ppufile);
  953. ppufile.putdata(value,sizeof(value));
  954. end;
  955. procedure tai_comp_64bit.ppuwrite(ppufile:tcompilerppufile);
  956. begin
  957. inherited ppuwrite(ppufile);
  958. ppufile.getdata(value,sizeof(value));
  959. end;
  960. {****************************************************************************
  961. TAI_STRING
  962. ****************************************************************************}
  963. constructor tai_string.Create(const _str : string);
  964. begin
  965. inherited Create;
  966. typ:=ait_string;
  967. len:=length(_str);
  968. getmem(str,len+1);
  969. strpcopy(str,_str);
  970. end;
  971. constructor tai_string.Create_pchar(_str : pchar);
  972. begin
  973. inherited Create;
  974. typ:=ait_string;
  975. str:=_str;
  976. len:=strlen(_str);
  977. end;
  978. constructor tai_string.Create_length_pchar(_str : pchar;length : longint);
  979. begin
  980. inherited Create;
  981. typ:=ait_string;
  982. str:=_str;
  983. len:=length;
  984. end;
  985. destructor tai_string.destroy;
  986. begin
  987. { you can have #0 inside the strings so }
  988. if str<>nil then
  989. freemem(str,len+1);
  990. inherited Destroy;
  991. end;
  992. constructor tai_string.ppuload(t:taitype;ppufile:tcompilerppufile);
  993. begin
  994. inherited ppuload(t,ppufile);
  995. len:=ppufile.getlongint;
  996. getmem(str,len+1);
  997. ppufile.getdata(str^,len);
  998. str[len]:=#0;
  999. end;
  1000. procedure tai_string.ppuwrite(ppufile:tcompilerppufile);
  1001. begin
  1002. inherited ppuwrite(ppufile);
  1003. ppufile.putlongint(len);
  1004. ppufile.putdata(str^,len);
  1005. end;
  1006. function tai_string.getcopy : tlinkedlistitem;
  1007. var
  1008. p : tlinkedlistitem;
  1009. begin
  1010. p:=inherited getcopy;
  1011. getmem(tai_string(p).str,len+1);
  1012. move(str^,tai_string(p).str^,len+1);
  1013. getcopy:=p;
  1014. end;
  1015. {****************************************************************************
  1016. TAI_LABEL
  1017. ****************************************************************************}
  1018. constructor tai_label.create(_l : tasmlabel);
  1019. begin
  1020. inherited Create;
  1021. typ:=ait_label;
  1022. l:=_l;
  1023. l.is_set:=true;
  1024. is_global:=(l.defbind=AB_GLOBAL);
  1025. end;
  1026. constructor tai_label.ppuload(t:taitype;ppufile:tcompilerppufile);
  1027. begin
  1028. inherited ppuload(t,ppufile);
  1029. l:=tasmlabel(ppufile.getasmsymbol);
  1030. is_global:=boolean(ppufile.getbyte);
  1031. end;
  1032. procedure tai_label.ppuwrite(ppufile:tcompilerppufile);
  1033. begin
  1034. inherited ppuwrite(ppufile);
  1035. ppufile.putasmsymbol(l);
  1036. ppufile.putbyte(byte(is_global));
  1037. end;
  1038. procedure tai_label.derefimpl;
  1039. begin
  1040. objectlibrary.DerefAsmsymbol(tasmsymbol(l));
  1041. l.is_set:=true;
  1042. end;
  1043. {****************************************************************************
  1044. TAI_DIRECT
  1045. ****************************************************************************}
  1046. constructor tai_direct.Create(_str : pchar);
  1047. begin
  1048. inherited Create;
  1049. typ:=ait_direct;
  1050. str:=_str;
  1051. end;
  1052. destructor tai_direct.destroy;
  1053. begin
  1054. strdispose(str);
  1055. inherited Destroy;
  1056. end;
  1057. constructor tai_direct.ppuload(t:taitype;ppufile:tcompilerppufile);
  1058. var
  1059. len : longint;
  1060. begin
  1061. inherited ppuload(t,ppufile);
  1062. len:=ppufile.getlongint;
  1063. getmem(str,len+1);
  1064. ppufile.getdata(str^,len);
  1065. str[len]:=#0;
  1066. end;
  1067. procedure tai_direct.ppuwrite(ppufile:tcompilerppufile);
  1068. var
  1069. len : longint;
  1070. begin
  1071. inherited ppuwrite(ppufile);
  1072. len:=strlen(str);
  1073. ppufile.putlongint(len);
  1074. ppufile.putdata(str^,len);
  1075. end;
  1076. function tai_direct.getcopy : tlinkedlistitem;
  1077. var
  1078. p : tlinkedlistitem;
  1079. begin
  1080. p:=inherited getcopy;
  1081. getmem(tai_direct(p).str,strlen(str)+1);
  1082. move(str^,tai_direct(p).str^,strlen(str)+1);
  1083. getcopy:=p;
  1084. end;
  1085. {****************************************************************************
  1086. tai_comment comment to be inserted in the assembler file
  1087. ****************************************************************************}
  1088. constructor tai_comment.Create(_str : pchar);
  1089. begin
  1090. inherited Create;
  1091. typ:=ait_comment;
  1092. str:=_str;
  1093. end;
  1094. destructor tai_comment.destroy;
  1095. begin
  1096. strdispose(str);
  1097. inherited Destroy;
  1098. end;
  1099. constructor tai_comment.ppuload(t:taitype;ppufile:tcompilerppufile);
  1100. var
  1101. len : longint;
  1102. begin
  1103. inherited ppuload(t,ppufile);
  1104. len:=ppufile.getlongint;
  1105. getmem(str,len+1);
  1106. ppufile.getdata(str^,len);
  1107. str[len]:=#0;
  1108. end;
  1109. procedure tai_comment.ppuwrite(ppufile:tcompilerppufile);
  1110. var
  1111. len : longint;
  1112. begin
  1113. inherited ppuwrite(ppufile);
  1114. len:=strlen(str);
  1115. ppufile.putlongint(len);
  1116. ppufile.putdata(str^,len);
  1117. end;
  1118. function tai_comment.getcopy : tlinkedlistitem;
  1119. var
  1120. p : tlinkedlistitem;
  1121. begin
  1122. p:=inherited getcopy;
  1123. getmem(tai_comment(p).str,strlen(str)+1);
  1124. move(str^,tai_comment(p).str^,strlen(str)+1);
  1125. getcopy:=p;
  1126. end;
  1127. {****************************************************************************
  1128. TAI_CUT
  1129. ****************************************************************************}
  1130. constructor tai_cut.Create;
  1131. begin
  1132. inherited Create;
  1133. typ:=ait_cut;
  1134. place:=cut_normal;
  1135. end;
  1136. constructor tai_cut.Create_begin;
  1137. begin
  1138. inherited Create;
  1139. typ:=ait_cut;
  1140. place:=cut_begin;
  1141. end;
  1142. constructor tai_cut.Create_end;
  1143. begin
  1144. inherited Create;
  1145. typ:=ait_cut;
  1146. place:=cut_end;
  1147. end;
  1148. constructor tai_cut.ppuload(t:taitype;ppufile:tcompilerppufile);
  1149. begin
  1150. inherited ppuload(t,ppufile);
  1151. place:=TCutPlace(ppufile.getbyte);
  1152. end;
  1153. procedure tai_cut.ppuwrite(ppufile:tcompilerppufile);
  1154. begin
  1155. inherited ppuwrite(ppufile);
  1156. ppufile.putbyte(byte(place));
  1157. end;
  1158. {****************************************************************************
  1159. Tai_Marker
  1160. ****************************************************************************}
  1161. constructor Tai_Marker.Create(_Kind: TMarker);
  1162. begin
  1163. Inherited Create;
  1164. typ := ait_marker;
  1165. Kind := _Kind;
  1166. end;
  1167. constructor Tai_Marker.ppuload(t:taitype;ppufile:tcompilerppufile);
  1168. begin
  1169. inherited ppuload(t,ppufile);
  1170. kind:=TMarker(ppufile.getbyte);
  1171. end;
  1172. procedure Tai_Marker.ppuwrite(ppufile:tcompilerppufile);
  1173. begin
  1174. inherited ppuwrite(ppufile);
  1175. ppufile.putbyte(byte(kind));
  1176. end;
  1177. {*****************************************************************************
  1178. tai_tempalloc
  1179. *****************************************************************************}
  1180. constructor tai_tempalloc.alloc(pos,size:longint);
  1181. begin
  1182. inherited Create;
  1183. typ:=ait_tempalloc;
  1184. allocation:=true;
  1185. temppos:=pos;
  1186. tempsize:=size;
  1187. {$ifdef EXTDEBUG}
  1188. problem:=nil;
  1189. {$endif EXTDEBUG}
  1190. end;
  1191. destructor tai_tempalloc.destroy;
  1192. begin
  1193. {$ifdef EXTDEBUG}
  1194. stringdispose(problem);
  1195. {$endif EXTDEBUG}
  1196. inherited destroy;
  1197. end;
  1198. constructor tai_tempalloc.dealloc(pos,size:longint);
  1199. begin
  1200. inherited Create;
  1201. typ:=ait_tempalloc;
  1202. allocation:=false;
  1203. temppos:=pos;
  1204. tempsize:=size;
  1205. {$ifdef EXTDEBUG}
  1206. problem:=nil;
  1207. {$endif EXTDEBUG}
  1208. end;
  1209. {$ifdef EXTDEBUG}
  1210. constructor tai_tempalloc.allocinfo(pos,size:longint;const st:string);
  1211. begin
  1212. inherited Create;
  1213. typ:=ait_tempalloc;
  1214. allocation:=false;
  1215. temppos:=pos;
  1216. tempsize:=size;
  1217. problem:=stringdup(st);
  1218. end;
  1219. {$endif EXTDEBUG}
  1220. constructor tai_tempalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1221. begin
  1222. inherited ppuload(t,ppufile);
  1223. temppos:=ppufile.getlongint;
  1224. tempsize:=ppufile.getlongint;
  1225. allocation:=boolean(ppufile.getbyte);
  1226. {$ifdef EXTDEBUG}
  1227. problem:=nil;
  1228. {$endif EXTDEBUG}
  1229. end;
  1230. procedure tai_tempalloc.ppuwrite(ppufile:tcompilerppufile);
  1231. begin
  1232. inherited ppuwrite(ppufile);
  1233. ppufile.putlongint(temppos);
  1234. ppufile.putlongint(tempsize);
  1235. ppufile.putbyte(byte(allocation));
  1236. end;
  1237. {*****************************************************************************
  1238. tai_regalloc
  1239. *****************************************************************************}
  1240. constructor tai_regalloc.alloc(r : tregister);
  1241. begin
  1242. inherited create;
  1243. typ:=ait_regalloc;
  1244. allocation:=true;
  1245. reg:=r;
  1246. end;
  1247. constructor tai_regalloc.dealloc(r : tregister);
  1248. begin
  1249. inherited create;
  1250. typ:=ait_regalloc;
  1251. allocation:=false;
  1252. reg:=r;
  1253. end;
  1254. constructor tai_regalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1255. begin
  1256. inherited ppuload(t,ppufile);
  1257. ppufile.getdata(reg,sizeof(Tregister));
  1258. allocation:=boolean(ppufile.getbyte);
  1259. end;
  1260. procedure tai_regalloc.ppuwrite(ppufile:tcompilerppufile);
  1261. begin
  1262. inherited ppuwrite(ppufile);
  1263. ppufile.putdata(reg,sizeof(Tregister));
  1264. ppufile.putbyte(byte(allocation));
  1265. end;
  1266. {*****************************************************************************
  1267. TaiInstruction
  1268. *****************************************************************************}
  1269. constructor taicpu_abstract.Create(op : tasmop);
  1270. begin
  1271. inherited create;
  1272. typ:=ait_instruction;
  1273. is_jmp:=false;
  1274. opcode:=op;
  1275. ops:=0;
  1276. fillchar(condition,sizeof(condition),0);
  1277. fillchar(oper,sizeof(oper),0);
  1278. end;
  1279. destructor taicpu_abstract.Destroy;
  1280. var
  1281. i : integer;
  1282. begin
  1283. for i:=0 to opercnt-1 do
  1284. begin
  1285. with oper[i]^ do
  1286. begin
  1287. case typ of
  1288. top_ref:
  1289. dispose(ref);
  1290. {$ifdef ARM}
  1291. top_shifterop:
  1292. dispose(shifterop);
  1293. {$endif ARM}
  1294. end;
  1295. end;
  1296. dispose(oper[i]);
  1297. end;
  1298. inherited destroy;
  1299. end;
  1300. { ---------------------------------------------------------------------
  1301. Loading of operands.
  1302. ---------------------------------------------------------------------}
  1303. procedure taicpu_abstract.allocate_oper(opers:longint);
  1304. begin
  1305. while (opers>opercnt) do
  1306. begin
  1307. new(oper[opercnt]);
  1308. fillchar(oper[opercnt]^,sizeof(toper),0);
  1309. inc(opercnt);
  1310. end;
  1311. end;
  1312. procedure taicpu_abstract.loadconst(opidx:longint;l:aword);
  1313. begin
  1314. allocate_oper(opidx+1);
  1315. with oper[opidx]^ do
  1316. begin
  1317. if typ<>top_const then
  1318. clearop(opidx);
  1319. val:=l;
  1320. typ:=top_const;
  1321. end;
  1322. end;
  1323. procedure taicpu_abstract.loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  1324. begin
  1325. if not assigned(s) then
  1326. internalerror(200204251);
  1327. allocate_oper(opidx+1);
  1328. with oper[opidx]^ do
  1329. begin
  1330. if typ<>top_symbol then
  1331. clearop(opidx);
  1332. sym:=s;
  1333. symofs:=sofs;
  1334. typ:=top_symbol;
  1335. end;
  1336. s.increfs;
  1337. end;
  1338. procedure taicpu_abstract.loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset:boolean);
  1339. begin
  1340. if not assigned(s) then
  1341. internalerror(200204251);
  1342. allocate_oper(opidx+1);
  1343. with oper[opidx]^ do
  1344. begin
  1345. if typ<>top_local then
  1346. clearop(opidx);
  1347. localsym:=s;
  1348. localsymofs:=sofs;
  1349. localindexreg:=indexreg;
  1350. localscale:=scale;
  1351. localgetoffset:=getoffset;
  1352. typ:=top_local;
  1353. end;
  1354. end;
  1355. procedure taicpu_abstract.loadref(opidx:longint;const r:treference);
  1356. begin
  1357. allocate_oper(opidx+1);
  1358. with oper[opidx]^ do
  1359. begin
  1360. if typ<>top_ref then
  1361. begin
  1362. clearop(opidx);
  1363. new(ref);
  1364. end;
  1365. ref^:=r;
  1366. {$ifdef i386}
  1367. { We allow this exception for i386, since overloading this would be
  1368. too much of a a speed penalty}
  1369. if (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  1370. segprefix:=ref^.segment;
  1371. {$endif}
  1372. typ:=top_ref;
  1373. if assigned(add_reg_instruction_hook) then
  1374. begin
  1375. add_reg_instruction_hook(self,ref^.base);
  1376. add_reg_instruction_hook(self,ref^.index);
  1377. end;
  1378. { mark symbol as used }
  1379. if assigned(ref^.symbol) then
  1380. ref^.symbol.increfs;
  1381. end;
  1382. end;
  1383. procedure taicpu_abstract.loadreg(opidx:longint;r:tregister);
  1384. begin
  1385. allocate_oper(opidx+1);
  1386. with oper[opidx]^ do
  1387. begin
  1388. if typ<>top_reg then
  1389. clearop(opidx);
  1390. reg:=r;
  1391. typ:=top_reg;
  1392. end;
  1393. if assigned(add_reg_instruction_hook) then
  1394. add_reg_instruction_hook(self,r);
  1395. {$ifdef ARM}
  1396. { R15 is the PC on the ARM thus moves to R15 are jumps.
  1397. Due to speed considerations we don't use a virtual overridden method here.
  1398. Because the pc/r15 isn't handled by the reg. allocator this should never cause
  1399. problems with iregs getting r15.
  1400. }
  1401. is_jmp:=(opcode=A_MOV) and (opidx=0) and (r=NR_R15);
  1402. {$endif ARM}
  1403. end;
  1404. procedure taicpu_abstract.loadoper(opidx:longint;o:toper);
  1405. begin
  1406. allocate_oper(opidx+1);
  1407. clearop(opidx);
  1408. oper[opidx]^:=o;
  1409. { copy also the reference }
  1410. with oper[opidx]^ do
  1411. begin
  1412. case typ of
  1413. top_reg:
  1414. begin
  1415. if assigned(add_reg_instruction_hook) then
  1416. add_reg_instruction_hook(self,reg);
  1417. end;
  1418. top_ref:
  1419. begin
  1420. new(ref);
  1421. ref^:=o.ref^;
  1422. if assigned(add_reg_instruction_hook) then
  1423. begin
  1424. add_reg_instruction_hook(self,ref^.base);
  1425. add_reg_instruction_hook(self,ref^.index);
  1426. end;
  1427. end;
  1428. {$ifdef ARM}
  1429. top_shifterop:
  1430. begin
  1431. new(shifterop);
  1432. shifterop^:=o.shifterop^;
  1433. end;
  1434. {$endif ARM}
  1435. end;
  1436. end;
  1437. end;
  1438. procedure taicpu_abstract.clearop(opidx:longint);
  1439. begin
  1440. with oper[opidx]^ do
  1441. begin
  1442. case typ of
  1443. top_ref:
  1444. dispose(ref);
  1445. {$ifdef ARM}
  1446. top_shifterop:
  1447. dispose(shifterop);
  1448. top_regset:
  1449. dispose(regset);
  1450. {$endif ARM}
  1451. end;
  1452. typ:=top_none;
  1453. end;
  1454. end;
  1455. { ---------------------------------------------------------------------
  1456. Register allocator methods.
  1457. ---------------------------------------------------------------------}
  1458. function taicpu_abstract.get_insert_pos(p:Tai;huntfor1,huntfor2,huntfor3:Tsuperregister;
  1459. var live_registers_int:Tsuperregisterworklist):Tai;
  1460. var
  1461. back : Tsuperregisterworklist;
  1462. supreg : tsuperregister;
  1463. begin
  1464. back.copyfrom(live_registers_int);
  1465. get_insert_pos:=p;
  1466. while (p<>nil) and (p.typ=ait_regalloc) do
  1467. begin
  1468. supreg:=getsupreg(Tai_regalloc(p).reg);
  1469. {Rewind the register allocation.}
  1470. if Tai_regalloc(p).allocation then
  1471. live_registers_int.delete(supreg)
  1472. else
  1473. begin
  1474. live_registers_int.add(supreg);
  1475. if supreg=huntfor1 then
  1476. begin
  1477. get_insert_pos:=Tai(p.previous);
  1478. back.done;
  1479. back.copyfrom(live_registers_int);
  1480. end;
  1481. if supreg=huntfor2 then
  1482. begin
  1483. get_insert_pos:=Tai(p.previous);
  1484. back.done;
  1485. back.copyfrom(live_registers_int);
  1486. end;
  1487. if supreg=huntfor3 then
  1488. begin
  1489. get_insert_pos:=Tai(p.previous);
  1490. back.done;
  1491. back.copyfrom(live_registers_int);
  1492. end;
  1493. end;
  1494. p:=Tai(p.previous);
  1495. end;
  1496. live_registers_int.done;
  1497. live_registers_int.copyfrom(back);
  1498. end;
  1499. procedure taicpu_abstract.forward_allocation(p:Tai;var live_registers_int:Tsuperregisterworklist);
  1500. begin
  1501. {Forward the register allocation again.}
  1502. while (p<>self) do
  1503. begin
  1504. if p.typ<>ait_regalloc then
  1505. internalerror(200305311);
  1506. if Tai_regalloc(p).allocation then
  1507. live_registers_int.add(getsupreg(Tai_regalloc(p).reg))
  1508. else
  1509. live_registers_int.delete(getsupreg(Tai_regalloc(p).reg));
  1510. p:=Tai(p.next);
  1511. end;
  1512. end;
  1513. function taicpu_abstract.spill_registers(list:Taasmoutput;
  1514. rt:Tregistertype;
  1515. rgget:Trggetproc;
  1516. rgunget:Trgungetproc;
  1517. const r:Tsuperregisterset;
  1518. var live_registers_int:Tsuperregisterworklist;
  1519. const spilltemplist:Tspill_temp_list): boolean;
  1520. type
  1521. tspillreginfo = record
  1522. orgreg: tsuperregister;
  1523. newreg: tregister;
  1524. regread,regwritten, mustbespilled: boolean;
  1525. end;
  1526. var
  1527. counter, regindex: longint;
  1528. pos: tai;
  1529. regs: array[0..2] of tspillreginfo;
  1530. supreg: tsuperregister;
  1531. spilled: boolean;
  1532. procedure DoSpillRead(pos: tai; regidx: longint);
  1533. var
  1534. helpins: tai;
  1535. begin
  1536. helpins:=spilling_create_load(spilltemplist[regs[regidx].orgreg],regs[regidx].newreg);
  1537. if pos=nil then
  1538. list.insertafter(helpins,list.first)
  1539. else
  1540. list.insertafter(helpins,pos.next);
  1541. rgunget(list,self,regs[regidx].newreg);
  1542. forward_allocation(tai(helpins.next),live_registers_int);
  1543. end;
  1544. procedure DoSpillWritten(pos: tai; regidx: longint);
  1545. var
  1546. helpins: tai;
  1547. begin
  1548. helpins:=spilling_create_store(regs[regidx].newreg,spilltemplist[regs[regidx].orgreg]);
  1549. list.insertafter(helpins,self);
  1550. rgunget(list,helpins,regs[regidx].newreg);
  1551. end;
  1552. procedure DoSpillReadWritten(pos: tai; regidx: longint);
  1553. var
  1554. helpins1, helpins2: tai;
  1555. begin
  1556. helpins1:=spilling_create_load(spilltemplist[regs[regidx].orgreg],regs[regidx].newreg);
  1557. if pos=nil then
  1558. list.insertafter(helpins1,list.first)
  1559. else
  1560. list.insertafter(helpins1,pos.next);
  1561. helpins2:=spilling_create_store(regs[regidx].newreg,spilltemplist[regs[regidx].orgreg]);
  1562. list.insertafter(helpins2,self);
  1563. rgunget(list,helpins2,regs[regidx].newreg);
  1564. forward_allocation(tai(helpins1.next),live_registers_int);
  1565. end;
  1566. procedure addreginfo(reg: tsuperregister; operation: topertype);
  1567. var
  1568. i, tmpindex: longint;
  1569. begin
  1570. tmpindex := regindex;
  1571. // did we already encounter this register?
  1572. for i := 0 to pred(regindex) do
  1573. if (regs[i].orgreg = reg) then
  1574. begin
  1575. tmpindex := i;
  1576. break;
  1577. end;
  1578. if tmpindex > high(regs) then
  1579. internalerror(2003120301);
  1580. regs[tmpindex].orgreg := reg;
  1581. if supregset_in(r,reg) then
  1582. begin
  1583. // add/update info on this register
  1584. regs[tmpindex].mustbespilled := true;
  1585. case operation of
  1586. operand_read:
  1587. regs[tmpindex].regread := true;
  1588. operand_write:
  1589. regs[tmpindex].regwritten := true;
  1590. operand_readwrite:
  1591. begin
  1592. regs[tmpindex].regread := true;
  1593. regs[tmpindex].regwritten := true;
  1594. end;
  1595. end;
  1596. spilled := true;
  1597. end;
  1598. inc(regindex,ord(regindex=tmpindex));
  1599. end;
  1600. procedure tryreplacereg(var reg: tregister);
  1601. var
  1602. i: longint;
  1603. supreg: tsuperregister;
  1604. begin
  1605. if (getregtype(reg) = R_INTREGISTER) then
  1606. begin
  1607. supreg := getsupreg(reg);
  1608. for i := 0 to pred(regindex) do
  1609. if (regs[i].mustbespilled) and
  1610. (regs[i].orgreg = supreg) then
  1611. begin
  1612. reg := regs[i].newreg;
  1613. break;
  1614. end;
  1615. end;
  1616. end;
  1617. begin
  1618. result := false;
  1619. fillchar(regs,sizeof(regs),0);
  1620. for counter := low(regs) to high(regs) do
  1621. regs[counter].orgreg := RS_INVALID;
  1622. spilled := false;
  1623. regindex := 0;
  1624. // check whether and if so which and how (read/written) this instructions contains
  1625. // registers that must be spilled
  1626. for counter := 0 to ops-1 do
  1627. begin
  1628. case oper[counter]^.typ of
  1629. top_reg:
  1630. if (getregtype(oper[counter]^.reg) = R_INTREGISTER) then
  1631. begin
  1632. addreginfo(getsupreg(oper[counter]^.reg),spilling_get_operation_type(counter));
  1633. end;
  1634. top_ref:
  1635. begin
  1636. if (oper[counter]^.ref^.base <> NR_NO) then
  1637. begin
  1638. addreginfo(getsupreg(oper[counter]^.ref^.base),operand_read);
  1639. end;
  1640. if (oper[counter]^.ref^.index <> NR_NO) then
  1641. begin
  1642. addreginfo(getsupreg(oper[counter]^.ref^.index),operand_read);
  1643. end;
  1644. end;
  1645. end;
  1646. end;
  1647. // generate the spilling code
  1648. if spilled then
  1649. begin
  1650. result := true;
  1651. for counter := 0 to pred(regindex) do
  1652. begin
  1653. if regs[counter].mustbespilled then
  1654. begin
  1655. supreg := regs[counter].orgreg;
  1656. pos := get_insert_pos(Tai(previous),regs[0].orgreg,regs[1].orgreg,regs[2].orgreg,live_registers_int);
  1657. rgget(list,pos,R_SUBWHOLE,regs[counter].newreg);
  1658. if regs[counter].regread then
  1659. if regs[counter].regwritten then
  1660. DoSpillReadWritten(pos,counter)
  1661. else
  1662. DoSpillRead(pos,counter)
  1663. else
  1664. DoSpillWritten(pos,counter)
  1665. end;
  1666. end;
  1667. end
  1668. else
  1669. exit;
  1670. // substitute registers
  1671. for counter := 0 to ops-1 do
  1672. begin
  1673. case oper[counter]^.typ of
  1674. top_reg:
  1675. begin
  1676. tryreplacereg(oper[counter]^.reg);
  1677. end;
  1678. top_ref:
  1679. begin
  1680. tryreplacereg(oper[counter]^.ref^.base);
  1681. tryreplacereg(oper[counter]^.ref^.index);
  1682. end;
  1683. end;
  1684. end;
  1685. end;
  1686. { ---------------------------------------------------------------------
  1687. Miscellaneous methods.
  1688. ---------------------------------------------------------------------}
  1689. procedure taicpu_abstract.SetCondition(const c:TAsmCond);
  1690. begin
  1691. condition:=c;
  1692. end;
  1693. Function taicpu_abstract.getcopy:TLinkedListItem;
  1694. var
  1695. i : longint;
  1696. p : taicpu_abstract;
  1697. begin
  1698. p:=taicpu_abstract(inherited getcopy);
  1699. { make a copy of the references }
  1700. p.opercnt:=0;
  1701. p.allocate_oper(ops);
  1702. for i:=0 to ops-1 do
  1703. begin
  1704. p.oper[i]^:=oper[i]^;
  1705. if (oper[i]^.typ=top_ref) then
  1706. begin
  1707. new(p.oper[i]^.ref);
  1708. p.oper[i]^.ref^:=oper[i]^.ref^;
  1709. end;
  1710. end;
  1711. getcopy:=p;
  1712. end;
  1713. constructor taicpu_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  1714. var
  1715. i : integer;
  1716. begin
  1717. inherited ppuload(t,ppufile);
  1718. { hopefully, we don't get problems with big/litte endian here when cross compiling :/ }
  1719. ppufile.getdata(condition,sizeof(tasmcond));
  1720. allocate_oper(ppufile.getbyte);
  1721. for i:=0 to ops-1 do
  1722. ppuloadoper(ppufile,oper[i]^);
  1723. opcode:=tasmop(ppufile.getword);
  1724. {$ifdef i386}
  1725. ppufile.getdata(segprefix,sizeof(Tregister));
  1726. {$endif i386}
  1727. is_jmp:=boolean(ppufile.getbyte);
  1728. end;
  1729. procedure taicpu_abstract.ppuwrite(ppufile:tcompilerppufile);
  1730. var
  1731. i : integer;
  1732. begin
  1733. inherited ppuwrite(ppufile);
  1734. ppufile.putdata(condition,sizeof(tasmcond));
  1735. ppufile.putbyte(ops);
  1736. for i:=0 to ops-1 do
  1737. ppuwriteoper(ppufile,oper[i]^);
  1738. ppufile.putword(word(opcode));
  1739. {$ifdef i386}
  1740. ppufile.putdata(segprefix,sizeof(Tregister));
  1741. {$endif i386}
  1742. ppufile.putbyte(byte(is_jmp));
  1743. end;
  1744. procedure taicpu_abstract.buildderefimpl;
  1745. var
  1746. i : integer;
  1747. begin
  1748. for i:=0 to ops-1 do
  1749. ppubuildderefimploper(oper[i]^);
  1750. end;
  1751. procedure taicpu_abstract.derefimpl;
  1752. var
  1753. i : integer;
  1754. begin
  1755. for i:=0 to ops-1 do
  1756. ppuderefoper(oper[i]^);
  1757. end;
  1758. {****************************************************************************
  1759. tai_align_abstract
  1760. ****************************************************************************}
  1761. constructor tai_align_abstract.Create(b: byte);
  1762. begin
  1763. inherited Create;
  1764. typ:=ait_align;
  1765. if b in [1,2,4,8,16,32] then
  1766. aligntype := b
  1767. else
  1768. aligntype := 1;
  1769. fillsize:=0;
  1770. fillop:=0;
  1771. use_op:=false;
  1772. end;
  1773. constructor tai_align_abstract.Create_op(b: byte; _op: byte);
  1774. begin
  1775. inherited Create;
  1776. typ:=ait_align;
  1777. if b in [1,2,4,8,16,32] then
  1778. aligntype := b
  1779. else
  1780. aligntype := 1;
  1781. fillsize:=0;
  1782. fillop:=_op;
  1783. use_op:=true;
  1784. end;
  1785. function tai_align_abstract.calculatefillbuf(var buf : tfillbuffer):pchar;
  1786. begin
  1787. fillchar(buf,high(buf),fillop);
  1788. calculatefillbuf:=pchar(@buf);
  1789. end;
  1790. constructor tai_align_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  1791. begin
  1792. inherited ppuload(t,ppufile);
  1793. aligntype:=ppufile.getbyte;
  1794. fillsize:=0;
  1795. fillop:=ppufile.getbyte;
  1796. use_op:=boolean(ppufile.getbyte);
  1797. end;
  1798. procedure tai_align_abstract.ppuwrite(ppufile:tcompilerppufile);
  1799. begin
  1800. inherited ppuwrite(ppufile);
  1801. ppufile.putbyte(aligntype);
  1802. ppufile.putbyte(fillop);
  1803. ppufile.putbyte(byte(use_op));
  1804. end;
  1805. {*****************************************************************************
  1806. TAAsmOutput
  1807. *****************************************************************************}
  1808. constructor taasmoutput.create;
  1809. begin
  1810. inherited create;
  1811. { make sure the optimizer won't remove the first tai of this list}
  1812. insert(tai_marker.create(marker_blockstart));
  1813. end;
  1814. function taasmoutput.getlasttaifilepos : pfileposinfo;
  1815. var
  1816. hp : tlinkedlistitem;
  1817. begin
  1818. getlasttaifilepos := nil;
  1819. if assigned(last) then
  1820. begin
  1821. { find the last file information record }
  1822. if not (tai(last).typ in SkipLineInfo) then
  1823. getlasttaifilepos:=@tailineinfo(last).fileinfo
  1824. else
  1825. { go through list backwards to find the first entry
  1826. with line information
  1827. }
  1828. begin
  1829. hp:=tai(last);
  1830. while assigned(hp) and (tai(hp).typ in SkipLineInfo) do
  1831. hp:=hp.Previous;
  1832. { found entry }
  1833. if assigned(hp) then
  1834. getlasttaifilepos:=@tailineinfo(hp).fileinfo
  1835. end;
  1836. end;
  1837. end;
  1838. procedure Taasmoutput.InsertAfter(Item,Loc : TLinkedListItem);
  1839. begin
  1840. { This is not possible because it is not sure that the
  1841. tai at Loc has taifileinfo as parent }
  1842. {if assigned(Loc) then
  1843. tailineinfo(Item).fileinfo:=tailineinfo(Loc).fileinfo;}
  1844. inherited InsertAfter(Item,Loc);
  1845. end;
  1846. end.
  1847. {
  1848. $Log$
  1849. Revision 1.62 2003-12-26 14:02:30 peter
  1850. * sparc updates
  1851. * use registertype in spill_register
  1852. Revision 1.61 2003/12/15 21:25:48 peter
  1853. * reg allocations for imaginary register are now inserted just
  1854. before reg allocation
  1855. * tregister changed to enum to allow compile time check
  1856. * fixed several tregister-tsuperregister errors
  1857. Revision 1.60 2003/12/14 20:24:28 daniel
  1858. * Register allocator speed optimizations
  1859. - Worklist no longer a ringbuffer
  1860. - No find operations are left
  1861. - Simplify now done in constant time
  1862. - unusedregs is now a Tsuperregisterworklist
  1863. - Microoptimizations
  1864. Revision 1.59 2003/12/08 22:34:24 peter
  1865. * tai_const.create_32bit changed to cardinal
  1866. Revision 1.58 2003/12/06 22:16:12 jonas
  1867. * completely overhauled and fixed generic spilling code. New method:
  1868. spilling_get_operation_type(operand_number): returns the operation
  1869. performed by the instruction on the operand: read/write/read+write.
  1870. See powerpc/aasmcpu.pas for an example
  1871. Revision 1.57 2003/12/03 17:39:04 florian
  1872. * fixed several arm calling conventions issues
  1873. * fixed reference reading in the assembler reader
  1874. * fixed a_loadaddr_ref_reg
  1875. Revision 1.55 2003/11/12 16:05:39 florian
  1876. * assembler readers OOPed
  1877. + typed currency constants
  1878. + typed 128 bit float constants if the CPU supports it
  1879. Revision 1.54 2003/11/07 15:58:32 florian
  1880. * Florian's culmutative nr. 1; contains:
  1881. - invalid calling conventions for a certain cpu are rejected
  1882. - arm softfloat calling conventions
  1883. - -Sp for cpu dependend code generation
  1884. - several arm fixes
  1885. - remaining code for value open array paras on heap
  1886. Revision 1.53 2003/10/30 19:59:00 peter
  1887. * support scalefactor for opr_local
  1888. * support reference with opr_local set, fixes tw2631
  1889. Revision 1.52 2003/10/29 21:06:39 jonas
  1890. * allow more than 3 args in the spilling routine
  1891. Revision 1.51 2003/10/29 15:40:20 peter
  1892. * support indexing and offset retrieval for locals
  1893. Revision 1.50 2003/10/29 14:42:14 mazen
  1894. * code reformatted
  1895. Revision 1.49 2003/10/29 14:05:45 mazen
  1896. * Splling function devided to sub functions to make it easy to understand.
  1897. This commit is just to allow easy diffs to validate the migration (hint use -w)
  1898. Revision 1.48 2003/10/24 17:39:41 peter
  1899. * asmnode.get_position now inserts a marker
  1900. Revision 1.47 2003/10/23 14:44:07 peter
  1901. * splitted buildderef and buildderefimpl to fix interface crc
  1902. calculation
  1903. Revision 1.46 2003/10/22 20:39:59 peter
  1904. * write derefdata in a separate ppu entry
  1905. Revision 1.45 2003/10/21 15:15:35 peter
  1906. * taicpu_abstract.oper[] changed to pointers
  1907. Revision 1.44 2003/10/17 14:38:32 peter
  1908. * 64k registers supported
  1909. * fixed some memory leaks
  1910. Revision 1.43 2003/10/11 16:06:42 florian
  1911. * fixed some MMX<->SSE
  1912. * started to fix ppc, needs an overhaul
  1913. + stabs info improve for spilling, not sure if it works correctly/completly
  1914. - MMX_SUPPORT removed from Makefile.fpc
  1915. Revision 1.42 2003/10/10 17:48:13 peter
  1916. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  1917. * tregisteralloctor renamed to trgobj
  1918. * removed rgobj from a lot of units
  1919. * moved location_* and reference_* to cgobj
  1920. * first things for mmx register allocation
  1921. Revision 1.41 2003/10/01 20:34:48 peter
  1922. * procinfo unit contains tprocinfo
  1923. * cginfo renamed to cgbase
  1924. * moved cgmessage to verbose
  1925. * fixed ppc and sparc compiles
  1926. Revision 1.40 2003/09/23 17:56:05 peter
  1927. * locals and paras are allocated in the code generation
  1928. * tvarsym.localloc contains the location of para/local when
  1929. generating code for the current procedure
  1930. Revision 1.39 2003/09/07 22:09:34 peter
  1931. * preparations for different default calling conventions
  1932. * various RA fixes
  1933. Revision 1.38 2003/09/04 00:15:28 florian
  1934. * first bunch of adaptions of arm compiler for new register type
  1935. Revision 1.37 2003/09/03 15:55:00 peter
  1936. * NEWRA branch merged
  1937. Revision 1.36 2003/09/03 11:18:36 florian
  1938. * fixed arm concatcopy
  1939. + arm support in the common compiler sources added
  1940. * moved some generic cg code around
  1941. + tfputype added
  1942. * ...
  1943. Revision 1.35.2.5 2003/08/31 21:08:16 peter
  1944. * first batch of sparc fixes
  1945. Revision 1.35.2.4 2003/08/29 17:28:59 peter
  1946. * next batch of updates
  1947. Revision 1.35.2.3 2003/08/28 18:35:07 peter
  1948. * tregister changed to cardinal
  1949. Revision 1.35.2.2 2003/08/27 20:23:55 peter
  1950. * remove old ra code
  1951. Revision 1.35.2.1 2003/08/27 19:55:54 peter
  1952. * first tregister patch
  1953. Revision 1.35 2003/08/21 14:47:41 peter
  1954. * remove convert_registers
  1955. Revision 1.34 2003/08/20 20:29:06 daniel
  1956. * Some more R_NO changes
  1957. * Preventive code to loadref added
  1958. Revision 1.33 2003/08/17 20:47:47 daniel
  1959. * Notranslation changed into -sr functionality
  1960. Revision 1.32 2003/08/17 16:59:20 jonas
  1961. * fixed regvars so they work with newra (at least for ppc)
  1962. * fixed some volatile register bugs
  1963. + -dnotranslation option for -dnewra, which causes the registers not to
  1964. be translated from virtual to normal registers. Requires support in
  1965. the assembler writer as well, which is only implemented in aggas/
  1966. agppcgas currently
  1967. Revision 1.31 2003/08/11 21:18:20 peter
  1968. * start of sparc support for newra
  1969. Revision 1.30 2003/07/02 16:43:48 jonas
  1970. * always add dummy marker object at the start of an assembler list, so
  1971. the optimizer can't remove the first object
  1972. Revision 1.29 2003/06/03 13:01:59 daniel
  1973. * Register allocator finished
  1974. Revision 1.28 2003/05/12 18:13:57 peter
  1975. * create rtti label using newasmsymboldata and update binding
  1976. only when calling tai_symbol.create
  1977. * tai_symbol.create_global added
  1978. Revision 1.27 2003/04/25 20:59:33 peter
  1979. * removed funcretn,funcretsym, function result is now in varsym
  1980. and aliases for result and function name are added using absolutesym
  1981. * vs_hidden parameter for funcret passed in parameter
  1982. * vs_hidden fixes
  1983. * writenode changed to printnode and released from extdebug
  1984. * -vp option added to generate a tree.log with the nodetree
  1985. * nicer printnode for statements, callnode
  1986. Revision 1.26 2002/04/25 16:12:09 florian
  1987. * fixed more problems with cpubase and x86-64
  1988. Revision 1.25 2003/04/25 08:25:26 daniel
  1989. * Ifdefs around a lot of calls to cleartempgen
  1990. * Fixed registers that are allocated but not freed in several nodes
  1991. * Tweak to register allocator to cause less spills
  1992. * 8-bit registers now interfere with esi,edi and ebp
  1993. Compiler can now compile rtl successfully when using new register
  1994. allocator
  1995. Revision 1.24 2003/04/24 13:03:01 florian
  1996. * comp is now written with its bit pattern to the ppu instead as an extended
  1997. Revision 1.23 2003/04/22 14:33:38 peter
  1998. * removed some notes/hints
  1999. Revision 1.22 2003/04/22 10:09:34 daniel
  2000. + Implemented the actual register allocator
  2001. + Scratch registers unavailable when new register allocator used
  2002. + maybe_save/maybe_restore unavailable when new register allocator used
  2003. Revision 1.21 2003/02/19 22:00:14 daniel
  2004. * Code generator converted to new register notation
  2005. - Horribily outdated todo.txt removed
  2006. Revision 1.20 2003/01/30 21:46:20 peter
  2007. * tai_const_symbol.createdataname added
  2008. Revision 1.19 2003/01/21 08:48:08 daniel
  2009. * Another 200301081 fixed
  2010. Revision 1.18 2003/01/09 20:40:59 daniel
  2011. * Converted some code in cgx86.pas to new register numbering
  2012. Revision 1.17 2003/01/09 15:49:56 daniel
  2013. * Added register conversion
  2014. Revision 1.16 2003/01/08 18:43:56 daniel
  2015. * Tregister changed into a record
  2016. Revision 1.15 2003/01/05 13:36:53 florian
  2017. * x86-64 compiles
  2018. + very basic support for float128 type (x86-64 only)
  2019. Revision 1.14 2002/12/06 17:50:21 peter
  2020. * symbol count fix merged
  2021. Revision 1.13 2002/11/17 16:31:55 carl
  2022. * memory optimization (3-4%) : cleanup of tai fields,
  2023. cleanup of tdef and tsym fields.
  2024. * make it work for m68k
  2025. Revision 1.12 2002/11/15 16:29:30 peter
  2026. * made tasmsymbol.refs private (merged)
  2027. Revision 1.11 2002/11/15 01:58:45 peter
  2028. * merged changes from 1.0.7 up to 04-11
  2029. - -V option for generating bug report tracing
  2030. - more tracing for option parsing
  2031. - errors for cdecl and high()
  2032. - win32 import stabs
  2033. - win32 records<=8 are returned in eax:edx (turned off by default)
  2034. - heaptrc update
  2035. - more info for temp management in .s file with EXTDEBUG
  2036. Revision 1.10 2002/11/09 15:38:03 carl
  2037. + NOOPT removed the optinfo field
  2038. Revision 1.9 2002/10/05 12:43:23 carl
  2039. * fixes for Delphi 6 compilation
  2040. (warning : Some features do not work under Delphi)
  2041. Revision 1.8 2002/08/19 19:36:42 peter
  2042. * More fixes for cross unit inlining, all tnodes are now implemented
  2043. * Moved pocall_internconst to po_internconst because it is not a
  2044. calling type at all and it conflicted when inlining of these small
  2045. functions was requested
  2046. Revision 1.7 2002/08/18 20:06:23 peter
  2047. * inlining is now also allowed in interface
  2048. * renamed write/load to ppuwrite/ppuload
  2049. * tnode storing in ppu
  2050. * nld,ncon,nbas are already updated for storing in ppu
  2051. Revision 1.6 2002/08/16 05:21:09 florian
  2052. * powerpc compilation fix
  2053. Revision 1.5 2002/08/15 19:10:35 peter
  2054. * first things tai,tnode storing in ppu
  2055. Revision 1.4 2002/08/11 14:32:25 peter
  2056. * renamed current_library to objectlibrary
  2057. Revision 1.3 2002/08/11 13:24:10 peter
  2058. * saving of asmsymbols in ppu supported
  2059. * asmsymbollist global is removed and moved into a new class
  2060. tasmlibrarydata that will hold the info of a .a file which
  2061. corresponds with a single module. Added librarydata to tmodule
  2062. to keep the library info stored for the module. In the future the
  2063. objectfiles will also be stored to the tasmlibrarydata class
  2064. * all getlabel/newasmsymbol and friends are moved to the new class
  2065. Revision 1.2 2002/08/05 18:27:48 carl
  2066. + more more more documentation
  2067. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  2068. Revision 1.1 2002/07/01 18:46:20 peter
  2069. * internal linker
  2070. * reorganized aasm layer
  2071. Revision 1.27 2002/05/18 13:34:04 peter
  2072. * readded missing revisions
  2073. Revision 1.25 2002/05/14 19:34:38 peter
  2074. * removed old logs and updated copyright year
  2075. Revision 1.24 2002/05/14 17:28:08 peter
  2076. * synchronized cpubase between powerpc and i386
  2077. * moved more tables from cpubase to cpuasm
  2078. * tai_align_abstract moved to tainst, cpuasm must define
  2079. the tai_align class now, which may be empty
  2080. Revision 1.23 2002/04/15 18:54:34 carl
  2081. - removed tcpuflags
  2082. Revision 1.22 2002/04/07 13:18:19 carl
  2083. + more documentation
  2084. Revision 1.21 2002/04/07 10:17:40 carl
  2085. - remove packenumfixed (requires version 1.0.2 or later to compile now!)
  2086. + changing some comments so its commented automatically
  2087. Revision 1.20 2002/03/24 19:04:31 carl
  2088. + patch for SPARC from Mazen NEIFER
  2089. }