aasmtai.pas 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225
  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. {$ifdef llvm}
  30. { overrides max_operands }
  31. llvmbase,
  32. {$endif llvm}
  33. cgbase,cgutils,
  34. symtype,
  35. aasmbase,aasmdata,ogbase
  36. {$ifdef jvm}
  37. ,widestr
  38. {$endif jvm}
  39. ;
  40. type
  41. { keep the number of elements in this enumeration less or equal than 32 as long
  42. as FPC knows only 4 byte and 32 byte sets (FK) }
  43. taitype = (
  44. ait_none,
  45. ait_align,
  46. ait_section,
  47. ait_comment,
  48. ait_string,
  49. ait_instruction,
  50. ait_datablock,
  51. ait_symbol,
  52. { needed to calc the size of a symbol }
  53. ait_symbol_end,
  54. ait_directive,
  55. ait_label,
  56. ait_const,
  57. ait_realconst,
  58. ait_typedconst,
  59. ait_stab,
  60. ait_force_line,
  61. ait_function_name,
  62. ait_symbolpair,
  63. { used to split into tiny assembler files }
  64. ait_cutobject,
  65. ait_regalloc,
  66. ait_tempalloc,
  67. { used to mark assembler blocks and inlined functions }
  68. ait_marker,
  69. { used to describe a new location of a variable }
  70. ait_varloc,
  71. {$ifdef JVM}
  72. { JVM only }
  73. ait_jvar, { debug information for a local variable }
  74. ait_jcatch, { exception catch clause }
  75. {$endif JVM}
  76. {$ifdef llvm}
  77. ait_llvmins, { llvm instruction }
  78. ait_llvmalias, { alias for a symbol }
  79. ait_llvmdecl, { llvm symbol declaration (global/external variable, external procdef) }
  80. {$endif}
  81. { SEH directives used in ARM,MIPS and x86_64 COFF targets }
  82. ait_seh_directive
  83. );
  84. taiconst_type = (
  85. aitconst_128bit,
  86. aitconst_64bit,
  87. aitconst_32bit,
  88. aitconst_16bit,
  89. aitconst_8bit,
  90. aitconst_sleb128bit,
  91. aitconst_uleb128bit,
  92. { win32 only }
  93. aitconst_rva_symbol,
  94. aitconst_secrel32_symbol,
  95. { darwin only }
  96. { From gcc/config/darwin.c (darwin_asm_output_dwarf_delta):
  97. ***
  98. Output a difference of two labels that will be an assembly time
  99. constant if the two labels are local. (.long lab1-lab2 will be
  100. very different if lab1 is at the boundary between two sections; it
  101. will be relocated according to the second section, not the first,
  102. so one ends up with a difference between labels in different
  103. sections, which is bad in the dwarf2 eh context for instance.)
  104. ***
  105. We cannot use this everywhere, because older versions of the
  106. darwin assembler don't support the construct used for these
  107. relsyms (nor do they support dwarf, for that matter)
  108. }
  109. aitconst_darwin_dwarf_delta64,
  110. aitconst_darwin_dwarf_delta32,
  111. { ARM Thumb-2 only }
  112. aitconst_half16bit, { used for table jumps. The actual value is the 16bit value shifted left once }
  113. { AVR }
  114. aitconst_gs, { Upper 16-bit of 17-bit constant }
  115. { for use by dwarf debugger information }
  116. aitconst_16bit_unaligned,
  117. aitconst_32bit_unaligned,
  118. aitconst_64bit_unaligned,
  119. { i8086 far pointer; emits: 'DW symbol, SEG symbol' }
  120. aitconst_farptr,
  121. { i8086 segment of symbol; emits: 'DW SEG symbol' }
  122. aitconst_seg,
  123. { i8086 data segment group; emits: 'DW dgroup'
  124. generated by the this inline asm:
  125. DW SEG @DATA
  126. in all memory models, except huge }
  127. aitconst_dgroup,
  128. { i8086 far data segment of the current pascal module (unit or program);
  129. emits: 'DW CURRENTMODULENAME_DATA'
  130. generated by the this inline asm:
  131. DW SEG @DATA
  132. in the huge memory model }
  133. aitconst_fardataseg,
  134. { offset of symbol's GOT slot in GOT }
  135. aitconst_got,
  136. { offset of symbol itself from GOT }
  137. aitconst_gotoff_symbol
  138. );
  139. tairealconsttype = (
  140. aitrealconst_s32bit,
  141. aitrealconst_s64bit,
  142. aitrealconst_s80bit,
  143. aitrealconst_s128bit,
  144. aitrealconst_s64comp
  145. );
  146. const
  147. {$if defined(cpu64bitaddr)}
  148. aitconst_ptr = aitconst_64bit;
  149. aitconst_ptr_unaligned = aitconst_64bit_unaligned;
  150. {$elseif defined(cpu32bitaddr)}
  151. aitconst_ptr = aitconst_32bit;
  152. aitconst_ptr_unaligned = aitconst_32bit_unaligned;
  153. {$elseif defined(cpu16bitaddr)}
  154. aitconst_ptr = aitconst_16bit;
  155. aitconst_ptr_unaligned = aitconst_16bit_unaligned;
  156. {$endif}
  157. {$if defined(cpu64bitalu)}
  158. aitconst_aint = aitconst_64bit;
  159. {$elseif defined(cpu32bitalu)}
  160. aitconst_aint = aitconst_32bit;
  161. {$elseif defined(cpu16bitalu)}
  162. aitconst_aint = aitconst_16bit;
  163. {$elseif defined(cpu8bitalu)}
  164. aitconst_aint = aitconst_8bit;
  165. {$endif}
  166. taitypestr : array[taitype] of string[24] = (
  167. '<none>',
  168. 'align',
  169. 'section',
  170. 'comment',
  171. 'string',
  172. 'instruction',
  173. 'datablock',
  174. 'symbol',
  175. 'symbol_end',
  176. 'symbol_directive',
  177. 'label',
  178. 'const',
  179. 'realconst',
  180. 'typedconst',
  181. 'stab',
  182. 'force_line',
  183. 'function_name',
  184. 'symbolpair',
  185. 'cut',
  186. 'regalloc',
  187. 'tempalloc',
  188. 'marker',
  189. 'varloc',
  190. {$ifdef JVM}
  191. 'jvar',
  192. 'jcatch',
  193. {$endif JVM}
  194. {$ifdef llvm}
  195. 'llvmins',
  196. 'llvmalias',
  197. 'llvmdecl',
  198. {$endif}
  199. 'seh_directive'
  200. );
  201. type
  202. { Types of operand }
  203. toptype=(top_none,top_reg,top_ref,top_const,top_bool,top_local
  204. {$ifdef arm}
  205. { ARM only }
  206. ,top_regset
  207. ,top_modeflags
  208. ,top_specialreg
  209. {$endif arm}
  210. {$if defined(arm) or defined(aarch64)}
  211. ,top_conditioncode
  212. ,top_shifterop
  213. {$endif defined(arm) or defined(aarch64)}
  214. {$ifdef m68k}
  215. { m68k only }
  216. ,top_regset
  217. {$endif m68k}
  218. {$ifdef jvm}
  219. { jvm only}
  220. ,top_single
  221. ,top_double
  222. ,top_string
  223. ,top_wstring
  224. {$endif jvm}
  225. {$ifdef llvm}
  226. { llvm only }
  227. ,top_single
  228. ,top_double
  229. ,top_undef
  230. {$ifdef cpuextended}
  231. ,top_extended80
  232. {$endif cpuextended}
  233. ,top_tai
  234. ,top_def
  235. ,top_fpcond
  236. ,top_cond
  237. ,top_para
  238. {$endif llvm}
  239. );
  240. { kinds of operations that an instruction can perform on an operand }
  241. topertype = (operand_read,operand_write,operand_readwrite);
  242. tlocaloper = record
  243. localsym : pointer;
  244. localsymderef : tderef;
  245. localsymofs : longint;
  246. localindexreg : tregister;
  247. localscale : byte;
  248. localgetoffset,
  249. localforceref : boolean
  250. end;
  251. plocaloper = ^tlocaloper;
  252. const
  253. { ait_* types which don't result in executable code or which don't influence
  254. the way the program runs/behaves, but which may be encountered by the
  255. optimizer (= if it's sometimes added to the exprasm list). Update if you add
  256. a new ait type! }
  257. SkipInstr = [ait_comment, ait_symbol,ait_section
  258. ,ait_stab, ait_function_name, ait_force_line
  259. ,ait_regalloc, ait_tempalloc, ait_symbol_end
  260. ,ait_directive
  261. ,ait_varloc,
  262. {$ifdef JVM}
  263. ait_jvar,
  264. {$endif JVM}
  265. ait_seh_directive];
  266. { ait_* types which do not have line information (and hence which are of type
  267. tai, otherwise, they are of type tailineinfo }
  268. SkipLineInfo =[ait_label,
  269. ait_regalloc,ait_tempalloc,
  270. ait_stab,ait_function_name,
  271. ait_cutobject,ait_marker,ait_varloc,ait_align,ait_section,ait_comment,
  272. ait_const,ait_directive,
  273. ait_symbolpair,
  274. ait_realconst,
  275. ait_symbol,
  276. {$ifdef JVM}
  277. ait_jvar, ait_jcatch,
  278. {$endif JVM}
  279. {$ifdef llvm}
  280. ait_llvmdecl,
  281. {$endif llvm}
  282. ait_seh_directive
  283. ];
  284. type
  285. { cut type, required for alphanumeric ordering of the assembler filenames }
  286. TCutPlace=(cut_normal,cut_begin,cut_end);
  287. TAsmMarker = (
  288. mark_NoPropInfoStart,mark_NoPropInfoEnd,
  289. mark_AsmBlockStart,mark_AsmBlockEnd,
  290. mark_NoLineInfoStart,mark_NoLineInfoEnd,mark_BlockStart,
  291. mark_Position
  292. );
  293. TRegAllocType = (ra_alloc,ra_dealloc,ra_sync,ra_resize,ra_markused);
  294. TStabType = (stab_stabs,stab_stabn,stab_stabd,
  295. { AIX/XCOFF stab types }
  296. stab_stabx,
  297. { begin/end include file }
  298. stabx_bi,stabx_ei,
  299. { begin/end function }
  300. stabx_bf, stabx_ef,
  301. { begin/end static data block }
  302. stabx_bs, stabx_es,
  303. { line spec, function start/end label }
  304. stabx_line, stabx_function);
  305. TAsmDirective=(
  306. asd_indirect_symbol,
  307. asd_extern,asd_nasm_import, asd_toc_entry,
  308. asd_reference,asd_no_dead_strip,asd_weak_reference,asd_lazy_reference,
  309. asd_weak_definition,
  310. { for Jasmin }
  311. asd_jclass,asd_jinterface,asd_jsuper,asd_jfield,asd_jlimit,asd_jline,
  312. { .ent/.end for MIPS }
  313. asd_ent,asd_ent_end,
  314. { supported by recent clang-based assemblers for data-in-code }
  315. asd_data_region, asd_end_data_region,
  316. { ARM }
  317. asd_thumb_func,asd_code,
  318. { restricts the assembler only to those instructions, which are
  319. available on the specified CPU; this represents directives such as
  320. NASM's 'CPU 686' or MASM/TASM's '.686p'. Might not be supported by
  321. all assemblers. }
  322. asd_cpu
  323. );
  324. TAsmSehDirective=(
  325. ash_proc,ash_endproc,
  326. ash_endprologue,ash_handler,ash_handlerdata,
  327. ash_eh,ash_32,ash_no32,
  328. ash_setframe,ash_stackalloc,ash_pushreg,
  329. ash_savereg,ash_savexmm,ash_pushframe
  330. );
  331. TSymbolPairKind = (spk_set, spk_thumb_set, spk_localentry);
  332. const
  333. regallocstr : array[tregalloctype] of string[10]=('allocated','released','sync','resized','used');
  334. tempallocstr : array[boolean] of string[10]=('released','allocated');
  335. stabtypestr : array[TStabType] of string[8]=(
  336. 'stabs','stabn','stabd',
  337. 'stabx',
  338. 'bi','ei',
  339. 'bf','ef',
  340. 'bs','es',
  341. 'line','function');
  342. directivestr : array[TAsmDirective] of string[23]=(
  343. 'indirect_symbol',
  344. 'extern','nasm_import', 'tc', 'reference',
  345. 'no_dead_strip','weak','lazy_reference','weak',
  346. { for Jasmin }
  347. 'class','interface','super','field','limit','line',
  348. { .ent/.end for MIPS }
  349. 'ent','end',
  350. { supported by recent clang-based assemblers for data-in-code }
  351. 'data_region','end_data_region',
  352. { ARM }
  353. 'thumb_func',
  354. 'code',
  355. 'cpu'
  356. );
  357. sehdirectivestr : array[TAsmSehDirective] of string[16]=(
  358. '.seh_proc','.seh_endproc',
  359. '.seh_endprologue','.seh_handler','.seh_handlerdata',
  360. '.seh_eh','.seh_32','seh_no32',
  361. '.seh_setframe','.seh_stackalloc','.seh_pushreg',
  362. '.seh_savereg','.seh_savexmm','.seh_pushframe'
  363. );
  364. symbolpairkindstr: array[TSymbolPairKind] of string[11]=(
  365. '.set', '.thumb_set', '.localentry'
  366. );
  367. type
  368. tai = class;
  369. { please keep the size of this record <=12 bytes and keep it properly aligned }
  370. toper = record
  371. ot : longint;
  372. case typ : toptype of
  373. top_none : ();
  374. top_reg : (reg:tregister);
  375. top_ref : (ref:preference);
  376. top_const : (val:tcgint);
  377. top_bool : (b:boolean);
  378. { local varsym that will be inserted in pass_generate_code }
  379. top_local : (localoper:plocaloper);
  380. {$ifdef arm}
  381. top_regset : (regset:^tcpuregisterset; regtyp: tregistertype; subreg: tsubregister; usermode: boolean);
  382. top_modeflags : (modeflags : tcpumodeflags);
  383. top_specialreg : (specialreg:tregister; specialflags:tspecialregflags);
  384. {$endif arm}
  385. {$if defined(arm) or defined(aarch64)}
  386. top_shifterop : (shifterop : pshifterop);
  387. top_conditioncode : (cc : TAsmCond);
  388. {$endif defined(arm) or defined(aarch64)}
  389. {$ifdef m68k}
  390. top_regset : (dataregset,addrregset,fpuregset:^tcpuregisterset);
  391. {$endif m68k}
  392. {$ifdef jvm}
  393. top_single : (sval:single);
  394. top_double : (dval:double);
  395. top_string : (pcvallen: aint; pcval: pchar);
  396. top_wstring : (pwstrval: pcompilerwidestring);
  397. {$endif jvm}
  398. {$ifdef llvm}
  399. top_single : (sval:single);
  400. top_double : (dval:double);
  401. top_undef : ();
  402. {$ifdef cpuextended}
  403. top_extended80 : (eval:extended);
  404. {$endif cpuextended}
  405. top_tai : (ai: tai);
  406. top_def : (def: tdef);
  407. top_cond : (cond: topcmp);
  408. top_fpcond : (fpcond: tllvmfpcmp);
  409. top_para : (paras: tfplist);
  410. {$endif llvm}
  411. end;
  412. poper=^toper;
  413. { abstract assembler item }
  414. tai = class(TLinkedListItem)
  415. {$ifndef NOOPT}
  416. { pointer to record with optimizer info about this tai object }
  417. optinfo : pointer;
  418. {$endif NOOPT}
  419. typ : taitype;
  420. constructor Create;
  421. constructor ppuload(t:taitype;ppufile:tcompilerppufile);virtual;
  422. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  423. procedure buildderefimpl;virtual;
  424. procedure derefimpl;virtual;
  425. end;
  426. { abstract assembler item with line information }
  427. tailineinfo = class(tai)
  428. fileinfo : tfileposinfo;
  429. constructor Create;
  430. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  431. procedure ppuwrite(ppufile:tcompilerppufile);override;
  432. end;
  433. tai_simple = class(tai)
  434. constructor create(_typ : taitype);
  435. end;
  436. taiclass = class of tai;
  437. taiclassarray = array[taitype] of taiclass;
  438. { Generates an assembler string }
  439. tai_string = class(tailineinfo)
  440. str : pchar;
  441. { extra len so the string can contain an \0 }
  442. len : longint;
  443. constructor Create(const _str : string);
  444. constructor Create_pchar(_str : pchar;length : longint);
  445. destructor Destroy;override;
  446. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  447. procedure ppuwrite(ppufile:tcompilerppufile);override;
  448. function getcopy:tlinkedlistitem;override;
  449. end;
  450. { Generates a common label }
  451. tai_symbol = class(tai)
  452. sym : tasmsymbol;
  453. value : puint;
  454. size : longint;
  455. is_global,
  456. has_value : boolean;
  457. constructor Create(_sym:tasmsymbol;siz:longint);
  458. constructor Create_Global(_sym:tasmsymbol;siz:longint);
  459. constructor Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  460. constructor Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  461. constructor Createname_global_value(const _name : string;_symtyp:Tasmsymtype;siz:longint;val:ptruint);
  462. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  463. procedure ppuwrite(ppufile:tcompilerppufile);override;
  464. procedure derefimpl;override;
  465. end;
  466. tai_symbol_end = class(tailineinfo)
  467. sym : tasmsymbol;
  468. constructor Create(_sym:tasmsymbol);
  469. constructor Createname(const _name : string);
  470. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  471. procedure ppuwrite(ppufile:tcompilerppufile);override;
  472. procedure derefimpl;override;
  473. end;
  474. tai_directive = class(tailineinfo)
  475. name : ansistring;
  476. directive : TAsmDirective;
  477. constructor Create(_directive:TAsmDirective;const _name:ansistring);
  478. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  479. procedure ppuwrite(ppufile:tcompilerppufile);override;
  480. end;
  481. { Generates an assembler label }
  482. tai_label = class(tai)
  483. labsym : tasmlabel;
  484. {$ifdef arm}
  485. { set to true when the label has been moved by insertpcrelativedata to the correct location
  486. so one label can be used multiple times }
  487. moved : boolean;
  488. { true, if a label has been already inserted, this is important for arm thumb where no negative
  489. pc relative offsets are allowed }
  490. inserted : boolean;
  491. {$endif arm}
  492. constructor Create(_labsym : tasmlabel);
  493. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  494. procedure ppuwrite(ppufile:tcompilerppufile);override;
  495. procedure derefimpl;override;
  496. end;
  497. { Generates an assembler comment }
  498. tai_comment = class(tai)
  499. str : pchar;
  500. constructor Create(_str : pchar);
  501. destructor Destroy; override;
  502. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  503. procedure ppuwrite(ppufile:tcompilerppufile);override;
  504. function getcopy:tlinkedlistitem;override;
  505. end;
  506. { Generates a section / segment directive }
  507. tai_section = class(tai)
  508. sectype : TAsmSectiontype;
  509. secorder : TasmSectionorder;
  510. secalign : byte;
  511. name : pshortstring;
  512. sec : TObjSection; { used in binary writer }
  513. destructor Destroy;override;
  514. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  515. procedure ppuwrite(ppufile:tcompilerppufile);override;
  516. {$push}{$warnings off}
  517. private
  518. { this constructor is made private on purpose }
  519. { because sections should be created via new_section() }
  520. constructor Create(Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  521. {$pop}
  522. end;
  523. { Generates an uninitializised data block }
  524. tai_datablock = class(tailineinfo)
  525. is_global : boolean;
  526. sym : tasmsymbol;
  527. size : asizeint;
  528. constructor Create(const _name : string;_size : asizeint);
  529. constructor Create_global(const _name : string;_size : asizeint);
  530. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  531. procedure ppuwrite(ppufile:tcompilerppufile);override;
  532. procedure derefimpl;override;
  533. end;
  534. { Generates an integer const }
  535. { tai_const }
  536. tai_const = class(tai)
  537. sym,
  538. endsym : tasmsymbol;
  539. { if symbols and offset are provided the symofs is used,
  540. the value is calculated during assembling }
  541. symofs,
  542. value : int64;
  543. consttype : taiconst_type;
  544. { we use for the 128bit int64/qword for now because I can't imagine a
  545. case where we need 128 bit now (FK) }
  546. constructor Create(_typ:taiconst_type;_value : int64);
  547. constructor Create_128bit(_value : int64);
  548. constructor Create_64bit(_value : int64);
  549. constructor Create_32bit(_value : longint);
  550. constructor Create_16bit(_value : word);
  551. constructor Create_64bit_unaligned(_value : int64);
  552. constructor Create_32bit_unaligned(_value : longint);
  553. constructor Create_16bit_unaligned(_value : word);
  554. constructor Create_8bit(_value : byte);
  555. constructor Create_char(size: integer; _value: dword);
  556. constructor Create_sleb128bit(_value : int64);
  557. constructor Create_uleb128bit(_value : qword);
  558. constructor Create_aint(_value : aint);
  559. constructor Create_pint(_value : pint);
  560. constructor Create_pint_unaligned(_value : pint);
  561. constructor Create_sym(_sym:tasmsymbol);
  562. {$ifdef i8086}
  563. constructor Create_sym_near(_sym:tasmsymbol);
  564. constructor Create_sym_far(_sym:tasmsymbol);
  565. {$endif i8086}
  566. constructor Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  567. constructor Create_sym_offset(_sym:tasmsymbol;ofs:asizeint);
  568. constructor Create_type_sym_offset(_typ:taiconst_type;_sym:tasmsymbol;ofs:asizeint);
  569. constructor Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  570. constructor Create_rel_sym_offset(_typ : taiconst_type; _sym,_endsym : tasmsymbol; _ofs : int64);
  571. constructor Create_rva_sym(_sym:tasmsymbol);
  572. constructor Createname(const name:string;ofs:asizeint);
  573. constructor Createname(const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  574. constructor Create_type_name(_typ:taiconst_type;const name:string;ofs:asizeint);
  575. constructor Create_type_name(_typ:taiconst_type;const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  576. constructor Create_nil_codeptr;
  577. constructor Create_nil_dataptr;
  578. constructor Create_int_codeptr(_value: int64);
  579. constructor Create_int_dataptr(_value: int64);
  580. {$ifdef i8086}
  581. constructor Create_seg_name(const name:string);
  582. constructor Create_dgroup;
  583. constructor Create_fardataseg;
  584. {$endif i8086}
  585. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  586. procedure ppuwrite(ppufile:tcompilerppufile);override;
  587. procedure derefimpl;override;
  588. function getcopy:tlinkedlistitem;override;
  589. function size:longint;
  590. end;
  591. { floating point const }
  592. tformatoptions = (fo_none,fo_hiloswapped);
  593. tai_realconst = class(tai)
  594. realtyp: tairealconsttype;
  595. savesize: byte;
  596. value: record
  597. case tairealconsttype of
  598. aitrealconst_s32bit: (s32val: ts32real);
  599. aitrealconst_s64bit: (s64val: ts64real);
  600. aitrealconst_s80bit: (s80val: ts80real);
  601. aitrealconst_s128bit: (s128val: ts128real);
  602. aitrealconst_s64comp: (s64compval: ts64comp);
  603. end;
  604. {$ifdef ARM}
  605. formatoptions : tformatoptions;
  606. {$endif ARM}
  607. constructor create_s32real(val: ts32real);
  608. constructor create_s64real(val: ts64real);
  609. {$ifdef ARM}
  610. constructor create_s64real_hiloswapped(val : ts64real);
  611. {$endif ARM}
  612. constructor create_s80real(val: ts80real; _savesize: byte);
  613. constructor create_s128real(val: ts128real);
  614. constructor create_s64compreal(val: ts64comp);
  615. constructor ppuload(t: taitype;ppufile: tcompilerppufile); override;
  616. procedure ppuwrite(ppufile: tcompilerppufile); override;
  617. function getcopy:tlinkedlistitem;override;
  618. function datasize: word;
  619. end;
  620. { tai_stab }
  621. tai_stab = class(tai)
  622. str : pchar;
  623. stabtype : TStabType;
  624. constructor Create(_stabtype:TStabType;_str : pchar);
  625. constructor Create_str(_stabtype:TStabType;const s:string);
  626. constructor create_ansistr(_stabtype: TStabType; const s: ansistring);
  627. destructor Destroy;override;
  628. end;
  629. tai_force_line = class(tailineinfo)
  630. constructor Create;
  631. end;
  632. tai_function_name = class(tai)
  633. funcname : pshortstring;
  634. constructor create(const s:string);
  635. destructor destroy;override;
  636. end;
  637. { Insert a cut to split assembler into several smaller files }
  638. tai_cutobject = class(tai)
  639. place : tcutplace;
  640. constructor Create;
  641. constructor Create_begin;
  642. constructor Create_end;
  643. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  644. procedure ppuwrite(ppufile:tcompilerppufile);override;
  645. end;
  646. { Insert a marker for assembler and inline blocks }
  647. tai_marker = class(tai)
  648. Kind: TAsmMarker;
  649. Constructor Create(_Kind: TAsmMarker);
  650. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  651. procedure ppuwrite(ppufile:tcompilerppufile);override;
  652. end;
  653. tai_tempalloc = class(tai)
  654. allocation : boolean;
  655. {$ifdef EXTDEBUG}
  656. problem : pshortstring;
  657. {$endif EXTDEBUG}
  658. temppos,
  659. tempsize : longint;
  660. constructor alloc(pos,size:longint);
  661. constructor dealloc(pos,size:longint);
  662. {$ifdef EXTDEBUG}
  663. constructor allocinfo(pos,size:longint;const st:string);
  664. {$endif EXTDEBUG}
  665. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  666. destructor destroy;override;
  667. procedure ppuwrite(ppufile:tcompilerppufile);override;
  668. end;
  669. tai_regalloc = class(tai)
  670. reg : tregister;
  671. ratype : TRegAllocType;
  672. { tells BuildLabelTableAndFixRegAlloc that the deallocation should be kept }
  673. keep : boolean;
  674. { reg(de)alloc belongs to this instruction, this
  675. is only used for automatic inserted (de)alloc for
  676. imaginary register and required for spilling code }
  677. instr : tai;
  678. constructor alloc(r : tregister;ainstr:tai);
  679. constructor dealloc(r : tregister;ainstr:tai);
  680. constructor sync(r : tregister);
  681. constructor resize(r : tregister);
  682. constructor markused(r : tregister);
  683. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  684. procedure ppuwrite(ppufile:tcompilerppufile);override;
  685. end;
  686. tadd_reg_instruction_proc=procedure(instr:Tai;r:tregister) of object;
  687. { Class template for assembler instructions
  688. }
  689. tai_cpu_abstract = class(tailineinfo)
  690. protected
  691. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);virtual;
  692. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);virtual;
  693. procedure ppubuildderefimploper(var o:toper);virtual;abstract;
  694. procedure ppuderefoper(var o:toper);virtual;abstract;
  695. public
  696. { Condition flags for instruction }
  697. condition : TAsmCond;
  698. { Number of operands to instruction }
  699. ops : byte;
  700. { Number of allocate oper structures }
  701. opercnt : byte;
  702. { Operands of instruction }
  703. oper : array[0..max_operands-1] of poper;
  704. { Actual opcode of instruction }
  705. opcode : tasmop;
  706. {$ifdef x86}
  707. segprefix : tregister;
  708. {$endif x86}
  709. { true if instruction is a jmp }
  710. is_jmp : boolean; { is this instruction a jump? (needed for optimizer) }
  711. Constructor Create(op : tasmop);virtual;
  712. Destructor Destroy;override;
  713. function getcopy:TLinkedListItem;override;
  714. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  715. procedure ppuwrite(ppufile:tcompilerppufile);override;
  716. procedure buildderefimpl;override;
  717. procedure derefimpl;override;
  718. procedure SetCondition(const c:TAsmCond);
  719. procedure allocate_oper(opers:longint);
  720. procedure loadconst(opidx:longint;l:aint);
  721. procedure loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  722. procedure loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  723. procedure loadref(opidx:longint;const r:treference);
  724. procedure loadreg(opidx:longint;r:tregister);
  725. procedure loadoper(opidx:longint;o:toper); virtual;
  726. procedure clearop(opidx:longint); virtual;
  727. procedure freeop(opidx:longint);
  728. { register allocator }
  729. function is_same_reg_move(regtype: Tregistertype):boolean;virtual;
  730. function spilling_get_operation_type(opnr: longint): topertype;virtual;
  731. function spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;virtual;
  732. function Pass1(objdata:TObjData):longint;virtual;
  733. procedure Pass2(objdata:TObjData);virtual;
  734. procedure resetpass1; virtual;
  735. procedure resetpass2; virtual;
  736. end;
  737. tai_cpu_class = class of tai_cpu_abstract;
  738. { Buffer type used for alignment }
  739. tfillbuffer = array[0..63] of char;
  740. { alignment for operator }
  741. tai_align_abstract = class(tai)
  742. aligntype : byte; { 1 = no align, 2 = word align, 4 = dword align }
  743. fillsize : byte; { real size to fill }
  744. fillop : byte; { value to fill with - optional }
  745. use_op : boolean;
  746. constructor Create(b:byte);virtual;
  747. constructor Create_op(b: byte; _op: byte);virtual;
  748. constructor Create_zeros(b:byte);
  749. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  750. procedure ppuwrite(ppufile:tcompilerppufile);override;
  751. function calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;virtual;
  752. end;
  753. tai_align_class = class of tai_align_abstract;
  754. tai_varloc = class(tai)
  755. oldlocation,
  756. oldlocationhi,
  757. newlocation,
  758. newlocationhi : tregister;
  759. varsym : tsym;
  760. constructor create(sym : tsym;loc : tregister);
  761. constructor create64(sym : tsym;loc,lochi : tregister);
  762. {$ifdef cpu64bitalu}
  763. constructor create128(sym : tsym;loc,lochi : tregister);
  764. {$endif cpu64bitalu}
  765. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  766. procedure ppuwrite(ppufile:tcompilerppufile);override;
  767. procedure buildderefimpl;override;
  768. procedure derefimpl;override;
  769. end;
  770. TSehDirectiveDatatype=(sd_none,sd_string,sd_reg,sd_offset,sd_regoffset);
  771. TSehDirectiveData=record
  772. case typ: TSehDirectiveDatatype of
  773. sd_none: ();
  774. sd_string: (name:pshortstring;flags:byte);
  775. sd_reg,sd_offset,sd_regoffset: (reg:TRegister;offset:dword);
  776. end;
  777. tai_seh_directive = class(tai)
  778. kind: TAsmSehDirective;
  779. data: TSehDirectiveData;
  780. constructor create(_kind:TAsmSehDirective);
  781. constructor create_name(_kind:TAsmSehDirective;const _name: string);
  782. constructor create_reg(_kind:TAsmSehDirective;r:TRegister);
  783. constructor create_offset(_kind:TAsmSehDirective;ofs:dword);
  784. constructor create_reg_offset(_kind:TAsmSehDirective;r:TRegister;ofs:dword);
  785. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  786. destructor destroy;override;
  787. procedure ppuwrite(ppufile:tcompilerppufile);override;
  788. procedure generate_code(objdata:TObjData);virtual;
  789. property datatype: TSehDirectiveDatatype read data.typ;
  790. end;
  791. tai_seh_directive_class=class of tai_seh_directive;
  792. {$ifdef JVM}
  793. { JVM variable live range description }
  794. tai_jvar = class(tai)
  795. stackslot: longint;
  796. desc: pshortstring;
  797. startlab,stoplab: tasmsymbol;
  798. constructor Create(_stackslot: longint; const _desc: shortstring; _startlab, _stoplab: TAsmSymbol);
  799. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  800. procedure ppuwrite(ppufile:tcompilerppufile);override;
  801. destructor destroy;override;
  802. end;
  803. tai_jvar_class = class of tai_jvar;
  804. { JVM exception catch description }
  805. tai_jcatch = class(tai)
  806. name: pshortstring;
  807. startlab,stoplab,handlerlab: tasmsymbol;
  808. constructor Create(const _name: shortstring; _startlab, _stoplab, _handlerlab: TAsmSymbol);
  809. destructor destroy;override;
  810. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  811. procedure ppuwrite(ppufile:tcompilerppufile);override;
  812. end;
  813. tai_jcatch_class = class of tai_jcatch;
  814. {$endif JVM}
  815. tai_symbolpair = class(tai)
  816. kind: TSymbolPairKind;
  817. sym,
  818. value: pshortstring;
  819. constructor create(akind: TSymbolPairKind; const asym, avalue: string);
  820. destructor destroy;override;
  821. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  822. procedure ppuwrite(ppufile:tcompilerppufile);override;
  823. end;
  824. var
  825. { array with all class types for tais }
  826. aiclass : taiclassarray;
  827. { target specific tais, possibly overwritten in target specific aasmcpu }
  828. cai_align : tai_align_class = tai_align_abstract;
  829. cai_cpu : tai_cpu_class = tai_cpu_abstract;
  830. cai_seh_directive: tai_seh_directive_class = tai_seh_directive;
  831. { hook to notify uses of registers }
  832. add_reg_instruction_hook : tadd_reg_instruction_proc;
  833. procedure maybe_new_object_file(list:TAsmList);
  834. procedure new_section(list:TAsmList;Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  835. procedure section_symbol_start(list:TAsmList;const Aname:string;Asymtyp:Tasmsymtype;
  836. Aglobal:boolean;Asectype:TAsmSectiontype;Aalign:byte);
  837. procedure section_symbol_end(list:TAsmList;const Aname:string);
  838. function ppuloadai(ppufile:tcompilerppufile):tai;
  839. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  840. implementation
  841. uses
  842. SysUtils,
  843. verbose,
  844. globals;
  845. const
  846. pputaimarker = 254;
  847. {****************************************************************************
  848. Helpers
  849. ****************************************************************************}
  850. procedure maybe_new_object_file(list:TAsmList);
  851. begin
  852. if create_smartlink_library then
  853. list.concat(tai_cutobject.create);
  854. end;
  855. procedure new_section(list:TAsmList;Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  856. begin
  857. list.concat(tai_section.create(Asectype,Aname,Aalign,Asecorder));
  858. list.concat(cai_align.create(Aalign));
  859. end;
  860. procedure section_symbol_start(list:TAsmList;const Aname:string;Asymtyp:Tasmsymtype;
  861. Aglobal:boolean;Asectype:TAsmSectiontype;Aalign:byte);
  862. begin
  863. maybe_new_object_file(list);
  864. new_section(list,Asectype,Aname,Aalign);
  865. if Aglobal or
  866. create_smartlink then
  867. list.concat(tai_symbol.createname_global(Aname,Asymtyp,0))
  868. else
  869. list.concat(tai_symbol.createname(Aname,Asymtyp,0));
  870. end;
  871. procedure section_symbol_end(list:TAsmList;const Aname:string);
  872. begin
  873. list.concat(tai_symbol_end.createname(Aname));
  874. end;
  875. function ppuloadai(ppufile:tcompilerppufile):tai;
  876. var
  877. b : byte;
  878. t : taitype;
  879. begin
  880. { marker }
  881. b:=ppufile.getbyte;
  882. if b<>pputaimarker then
  883. internalerror(200208181);
  884. { load nodetype }
  885. t:=taitype(ppufile.getbyte);
  886. if t<>ait_none then
  887. begin
  888. if t>high(taitype) then
  889. internalerror(200208182);
  890. if not assigned(aiclass[t]) then
  891. internalerror(200208183);
  892. {writeln('taiload: ',taitypestr[t]);}
  893. { generate tai of the correct class }
  894. ppuloadai:=aiclass[t].ppuload(t,ppufile);
  895. end
  896. else
  897. ppuloadai:=nil;
  898. end;
  899. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  900. begin
  901. { marker, read by ppuloadnode }
  902. ppufile.putbyte(pputaimarker);
  903. if assigned(n) then
  904. begin
  905. { type, read by ppuloadnode }
  906. ppufile.putbyte(byte(n.typ));
  907. {writeln('taiwrite: ',taitypestr[n.typ]);}
  908. n.ppuwrite(ppufile);
  909. end
  910. else
  911. ppufile.putbyte(byte(ait_none));
  912. end;
  913. constructor tai_symbolpair.create(akind: TSymbolPairKind; const asym, avalue: string);
  914. begin
  915. inherited create;
  916. kind:=akind;
  917. typ:=ait_symbolpair;
  918. sym:=stringdup(asym);
  919. value:=stringdup(avalue);
  920. end;
  921. destructor tai_symbolpair.destroy;
  922. begin
  923. stringdispose(sym);
  924. stringdispose(value);
  925. inherited destroy;
  926. end;
  927. constructor tai_symbolpair.ppuload(t: taitype; ppufile: tcompilerppufile);
  928. begin
  929. inherited ppuload(t,ppufile);
  930. kind:=TSymbolPairKind(ppufile.getbyte);;
  931. sym:=stringdup(ppufile.getstring);
  932. value:=stringdup(ppufile.getstring);
  933. end;
  934. procedure tai_symbolpair.ppuwrite(ppufile: tcompilerppufile);
  935. begin
  936. inherited ppuwrite(ppufile);
  937. ppufile.putbyte(byte(kind));
  938. ppufile.putstring(sym^);
  939. ppufile.putstring(value^);
  940. end;
  941. constructor tai_varloc.create(sym: tsym; loc: tregister);
  942. begin
  943. inherited Create;
  944. typ:=ait_varloc;
  945. newlocation:=loc;
  946. newlocationhi:=NR_NO;
  947. varsym:=sym;
  948. oldlocationhi:=NR_NO;
  949. end;
  950. constructor tai_varloc.create64(sym: tsym; loc, lochi: tregister);
  951. begin
  952. inherited Create;
  953. typ:=ait_varloc;
  954. newlocation:=loc;
  955. newlocationhi:=lochi;
  956. varsym:=sym;
  957. end;
  958. {$ifdef cpu64bitalu}
  959. constructor tai_varloc.create128(sym: tsym; loc, lochi: tregister);
  960. begin
  961. inherited Create;
  962. typ:=ait_varloc;
  963. newlocation:=loc;
  964. newlocationhi:=lochi;
  965. varsym:=sym;
  966. end;
  967. {$endif cpu64bitalu}
  968. constructor tai_varloc.ppuload(t: taitype; ppufile: tcompilerppufile);
  969. begin
  970. inherited ppuload(t, ppufile);
  971. end;
  972. procedure tai_varloc.ppuwrite(ppufile: tcompilerppufile);
  973. begin
  974. inherited ppuwrite(ppufile);
  975. end;
  976. procedure tai_varloc.buildderefimpl;
  977. begin
  978. inherited buildderefimpl;
  979. end;
  980. procedure tai_varloc.derefimpl;
  981. begin
  982. inherited derefimpl;
  983. end;
  984. {****************************************************************************
  985. TAI
  986. ****************************************************************************}
  987. constructor tai.Create;
  988. begin
  989. {$ifndef NOOPT}
  990. optinfo:=nil;
  991. {$endif NOOPT}
  992. end;
  993. constructor tai.ppuload(t:taitype;ppufile:tcompilerppufile);
  994. begin
  995. typ:=t;
  996. {$ifndef NOOPT}
  997. optinfo:=nil;
  998. {$endif}
  999. end;
  1000. procedure tai.ppuwrite(ppufile:tcompilerppufile);
  1001. begin
  1002. end;
  1003. procedure tai.buildderefimpl;
  1004. begin
  1005. end;
  1006. procedure tai.derefimpl;
  1007. begin
  1008. end;
  1009. {****************************************************************************
  1010. TAILINEINFO
  1011. ****************************************************************************}
  1012. constructor tailineinfo.create;
  1013. begin
  1014. inherited create;
  1015. fileinfo:=current_filepos;
  1016. end;
  1017. constructor tailineinfo.ppuload(t:taitype;ppufile:tcompilerppufile);
  1018. begin
  1019. inherited ppuload(t,ppufile);
  1020. ppufile.getposinfo(fileinfo);
  1021. end;
  1022. procedure tailineinfo.ppuwrite(ppufile:tcompilerppufile);
  1023. begin
  1024. inherited ppuwrite(ppufile);
  1025. ppufile.putposinfo(fileinfo);
  1026. end;
  1027. {****************************************************************************
  1028. TAI_SIMPLE
  1029. ****************************************************************************}
  1030. constructor tai_simple.create(_typ : taitype);
  1031. begin
  1032. inherited create;
  1033. typ:=_typ;
  1034. end;
  1035. {****************************************************************************
  1036. TAI_SECTION
  1037. ****************************************************************************}
  1038. constructor tai_section.Create(Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  1039. begin
  1040. inherited Create;
  1041. typ:=ait_section;
  1042. sectype:=asectype;
  1043. secalign:=Aalign;
  1044. secorder:=Asecorder;
  1045. name:=stringdup(Aname);
  1046. sec:=nil;
  1047. end;
  1048. constructor tai_section.ppuload(t:taitype;ppufile:tcompilerppufile);
  1049. begin
  1050. inherited ppuload(t,ppufile);
  1051. sectype:=TAsmSectiontype(ppufile.getbyte);
  1052. secalign:=ppufile.getbyte;
  1053. name:=stringdup(ppufile.getstring);
  1054. sec:=nil;
  1055. end;
  1056. destructor tai_section.Destroy;
  1057. begin
  1058. stringdispose(name);
  1059. end;
  1060. procedure tai_section.ppuwrite(ppufile:tcompilerppufile);
  1061. begin
  1062. inherited ppuwrite(ppufile);
  1063. ppufile.putbyte(byte(sectype));
  1064. ppufile.putbyte(secalign);
  1065. ppufile.putstring(name^);
  1066. end;
  1067. {****************************************************************************
  1068. TAI_DATABLOCK
  1069. ****************************************************************************}
  1070. constructor tai_datablock.Create(const _name : string;_size : asizeint);
  1071. begin
  1072. inherited Create;
  1073. typ:=ait_datablock;
  1074. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,AT_DATA);
  1075. { keep things aligned }
  1076. if _size<=0 then
  1077. _size:=sizeof(aint);
  1078. size:=_size;
  1079. is_global:=false;
  1080. end;
  1081. constructor tai_datablock.Create_global(const _name : string;_size : asizeint);
  1082. begin
  1083. inherited Create;
  1084. typ:=ait_datablock;
  1085. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,AT_DATA);
  1086. { keep things aligned }
  1087. if _size<=0 then
  1088. _size:=sizeof(aint);
  1089. size:=_size;
  1090. is_global:=true;
  1091. end;
  1092. constructor tai_datablock.ppuload(t:taitype;ppufile:tcompilerppufile);
  1093. begin
  1094. inherited Create;
  1095. sym:=ppufile.getasmsymbol;
  1096. size:=ppufile.getaint;
  1097. is_global:=boolean(ppufile.getbyte);
  1098. end;
  1099. procedure tai_datablock.ppuwrite(ppufile:tcompilerppufile);
  1100. begin
  1101. inherited ppuwrite(ppufile);
  1102. ppufile.putasmsymbol(sym);
  1103. ppufile.putaint(size);
  1104. ppufile.putbyte(byte(is_global));
  1105. end;
  1106. procedure tai_datablock.derefimpl;
  1107. begin
  1108. end;
  1109. {****************************************************************************
  1110. TAI_SYMBOL
  1111. ****************************************************************************}
  1112. constructor tai_symbol.Create(_sym:tasmsymbol;siz:longint);
  1113. begin
  1114. inherited Create;
  1115. typ:=ait_symbol;
  1116. sym:=_sym;
  1117. size:=siz;
  1118. { don't redefine global/external symbols as local, as code to access
  1119. such symbols is different on some platforms }
  1120. if not(sym.bind in [AB_NONE,AB_LOCAL]) then
  1121. internalerror(2013081601);
  1122. sym.bind:=AB_LOCAL;
  1123. is_global:=false;
  1124. end;
  1125. constructor tai_symbol.Create_global(_sym:tasmsymbol;siz:longint);
  1126. begin
  1127. inherited Create;
  1128. typ:=ait_symbol;
  1129. sym:=_sym;
  1130. size:=siz;
  1131. { don't override PRIVATE_EXTERN with GLOBAL }
  1132. if not(sym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN]) then
  1133. sym.bind:=AB_GLOBAL;
  1134. is_global:=true;
  1135. end;
  1136. constructor tai_symbol.Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  1137. begin
  1138. inherited Create;
  1139. typ:=ait_symbol;
  1140. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,_symtyp);
  1141. size:=siz;
  1142. is_global:=false;
  1143. end;
  1144. constructor tai_symbol.Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  1145. begin
  1146. inherited Create;
  1147. typ:=ait_symbol;
  1148. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,_symtyp);
  1149. size:=siz;
  1150. is_global:=true;
  1151. end;
  1152. constructor tai_symbol.createname_global_value(const _name: string;_symtyp: tasmsymtype; siz: longint; val: ptruint);
  1153. begin
  1154. Createname_global(_name,_symtyp,siz);
  1155. value:=val;
  1156. has_value:=true;
  1157. end;
  1158. constructor tai_symbol.ppuload(t:taitype;ppufile:tcompilerppufile);
  1159. begin
  1160. inherited ppuload(t,ppufile);
  1161. sym:=ppufile.getasmsymbol;
  1162. size:=ppufile.getlongint;
  1163. is_global:=boolean(ppufile.getbyte);
  1164. end;
  1165. procedure tai_symbol.ppuwrite(ppufile:tcompilerppufile);
  1166. begin
  1167. inherited ppuwrite(ppufile);
  1168. ppufile.putasmsymbol(sym);
  1169. ppufile.putlongint(size);
  1170. ppufile.putbyte(byte(is_global));
  1171. end;
  1172. procedure tai_symbol.derefimpl;
  1173. begin
  1174. end;
  1175. {****************************************************************************
  1176. TAI_SYMBOL_END
  1177. ****************************************************************************}
  1178. constructor tai_symbol_end.Create(_sym:tasmsymbol);
  1179. begin
  1180. inherited Create;
  1181. typ:=ait_symbol_end;
  1182. sym:=_sym;
  1183. end;
  1184. constructor tai_symbol_end.Createname(const _name : string);
  1185. begin
  1186. inherited Create;
  1187. typ:=ait_symbol_end;
  1188. sym:=current_asmdata.GetAsmSymbol(_name);
  1189. if not assigned(sym) then
  1190. internalerror(2013080301);
  1191. end;
  1192. constructor tai_symbol_end.ppuload(t:taitype;ppufile:tcompilerppufile);
  1193. begin
  1194. inherited ppuload(t,ppufile);
  1195. sym:=ppufile.getasmsymbol;
  1196. end;
  1197. procedure tai_symbol_end.ppuwrite(ppufile:tcompilerppufile);
  1198. begin
  1199. inherited ppuwrite(ppufile);
  1200. ppufile.putasmsymbol(sym);
  1201. end;
  1202. procedure tai_symbol_end.derefimpl;
  1203. begin
  1204. end;
  1205. {****************************************************************************
  1206. TAI_SYMBOL_END
  1207. ****************************************************************************}
  1208. constructor tai_directive.Create(_directive:TAsmDirective;const _name:ansistring);
  1209. begin
  1210. inherited Create;
  1211. typ:=ait_directive;
  1212. name:=_name;
  1213. directive:=_directive;
  1214. end;
  1215. constructor tai_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  1216. begin
  1217. inherited ppuload(t,ppufile);
  1218. name:=ppufile.getansistring;
  1219. directive:=TAsmDirective(ppufile.getbyte);
  1220. end;
  1221. procedure tai_directive.ppuwrite(ppufile:tcompilerppufile);
  1222. begin
  1223. inherited ppuwrite(ppufile);
  1224. ppufile.putansistring(name);
  1225. ppufile.putbyte(byte(directive));
  1226. end;
  1227. {****************************************************************************
  1228. TAI_CONST
  1229. ****************************************************************************}
  1230. constructor tai_const.Create(_typ:taiconst_type;_value : int64);
  1231. begin
  1232. inherited Create;
  1233. typ:=ait_const;
  1234. consttype:=_typ;
  1235. value:=_value;
  1236. sym:=nil;
  1237. endsym:=nil;
  1238. end;
  1239. constructor tai_const.Create_128bit(_value : int64);
  1240. begin
  1241. inherited Create;
  1242. typ:=ait_const;
  1243. consttype:=aitconst_128bit;
  1244. value:=_value;
  1245. sym:=nil;
  1246. endsym:=nil;
  1247. end;
  1248. constructor tai_const.Create_64bit(_value : int64);
  1249. begin
  1250. inherited Create;
  1251. typ:=ait_const;
  1252. consttype:=aitconst_64bit;
  1253. value:=_value;
  1254. sym:=nil;
  1255. endsym:=nil;
  1256. end;
  1257. constructor tai_const.Create_32bit(_value : longint);
  1258. begin
  1259. inherited Create;
  1260. typ:=ait_const;
  1261. consttype:=aitconst_32bit;
  1262. value:=_value;
  1263. sym:=nil;
  1264. endsym:=nil;
  1265. end;
  1266. constructor tai_const.Create_16bit(_value : word);
  1267. begin
  1268. inherited Create;
  1269. typ:=ait_const;
  1270. consttype:=aitconst_16bit;
  1271. value:=_value;
  1272. sym:=nil;
  1273. endsym:=nil;
  1274. end;
  1275. constructor tai_const.Create_64bit_unaligned(_value : int64);
  1276. begin
  1277. inherited Create;
  1278. typ:=ait_const;
  1279. consttype:=aitconst_64bit_unaligned;
  1280. value:=_value;
  1281. sym:=nil;
  1282. endsym:=nil;
  1283. end;
  1284. constructor tai_const.Create_32bit_unaligned(_value : longint);
  1285. begin
  1286. inherited Create;
  1287. typ:=ait_const;
  1288. consttype:=aitconst_32bit_unaligned;
  1289. value:=_value;
  1290. sym:=nil;
  1291. endsym:=nil;
  1292. end;
  1293. constructor tai_const.Create_16bit_unaligned(_value : word);
  1294. begin
  1295. inherited Create;
  1296. typ:=ait_const;
  1297. consttype:=aitconst_16bit_unaligned;
  1298. value:=_value;
  1299. sym:=nil;
  1300. endsym:=nil;
  1301. end;
  1302. constructor tai_const.Create_8bit(_value : byte);
  1303. begin
  1304. inherited Create;
  1305. typ:=ait_const;
  1306. consttype:=aitconst_8bit;
  1307. value:=_value;
  1308. sym:=nil;
  1309. endsym:=nil;
  1310. end;
  1311. constructor tai_const.Create_char(size: integer; _value: dword);
  1312. begin
  1313. inherited Create;
  1314. typ:=ait_const;
  1315. case size of
  1316. 1:
  1317. begin
  1318. consttype:=aitconst_8bit;
  1319. value:=byte(_value)
  1320. end;
  1321. 2:
  1322. begin
  1323. consttype:=aitconst_16bit;
  1324. value:=word(_value)
  1325. end
  1326. else
  1327. InternalError(2010030701)
  1328. end
  1329. end;
  1330. constructor tai_const.Create_sleb128bit(_value : int64);
  1331. begin
  1332. inherited Create;
  1333. typ:=ait_const;
  1334. consttype:=aitconst_sleb128bit;
  1335. value:=_value;
  1336. sym:=nil;
  1337. endsym:=nil;
  1338. end;
  1339. constructor tai_const.Create_uleb128bit(_value : qword);
  1340. begin
  1341. inherited Create;
  1342. typ:=ait_const;
  1343. consttype:=aitconst_uleb128bit;
  1344. value:=int64(_value);
  1345. sym:=nil;
  1346. endsym:=nil;
  1347. end;
  1348. constructor tai_const.Create_aint(_value : aint);
  1349. begin
  1350. inherited Create;
  1351. typ:=ait_const;
  1352. consttype:=aitconst_aint;
  1353. value:=_value;
  1354. sym:=nil;
  1355. endsym:=nil;
  1356. end;
  1357. constructor tai_const.Create_pint(_value : pint);
  1358. begin
  1359. inherited Create;
  1360. typ:=ait_const;
  1361. consttype:=aitconst_ptr;
  1362. value:=_value;
  1363. sym:=nil;
  1364. endsym:=nil;
  1365. end;
  1366. constructor tai_const.Create_pint_unaligned(_value: pint);
  1367. begin
  1368. inherited Create;
  1369. typ:=ait_const;
  1370. consttype:=aitconst_ptr_unaligned;
  1371. value:=_value;
  1372. sym:=nil;
  1373. endsym:=nil;
  1374. end;
  1375. constructor tai_const.Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  1376. begin
  1377. inherited Create;
  1378. typ:=ait_const;
  1379. consttype:=_typ;
  1380. sym:=_sym;
  1381. endsym:=nil;
  1382. value:=0;
  1383. { update sym info }
  1384. if assigned(sym) then
  1385. sym.increfs;
  1386. end;
  1387. constructor tai_const.Create_sym(_sym:tasmsymbol);
  1388. begin
  1389. self.create_sym_offset(_sym,0);
  1390. end;
  1391. {$ifdef i8086}
  1392. constructor tai_const.Create_sym_near(_sym: tasmsymbol);
  1393. begin
  1394. self.create_sym(_sym);
  1395. consttype:=aitconst_ptr;
  1396. end;
  1397. constructor tai_const.Create_sym_far(_sym: tasmsymbol);
  1398. begin
  1399. self.create_sym(_sym);
  1400. consttype:=aitconst_farptr;
  1401. end;
  1402. {$endif i8086}
  1403. constructor tai_const.Create_sym_offset(_sym:tasmsymbol;ofs:asizeint);
  1404. begin
  1405. inherited Create;
  1406. typ:=ait_const;
  1407. {$ifdef i8086}
  1408. if assigned(_sym) and (_sym.typ=AT_DATA) then
  1409. begin
  1410. if current_settings.x86memorymodel in x86_far_data_models then
  1411. consttype:=aitconst_farptr
  1412. else
  1413. consttype:=aitconst_ptr;
  1414. end
  1415. else
  1416. begin
  1417. if current_settings.x86memorymodel in x86_far_code_models then
  1418. consttype:=aitconst_farptr
  1419. else
  1420. consttype:=aitconst_ptr;
  1421. end;
  1422. {$else i8086}
  1423. {$ifdef avr}
  1424. if assigned(_sym) and (_sym.typ=AT_FUNCTION) then
  1425. consttype:=aitconst_gs
  1426. else
  1427. {$endif avr}
  1428. consttype:=aitconst_ptr;
  1429. {$endif i8086}
  1430. { sym is allowed to be nil, this is used to write nil pointers }
  1431. sym:=_sym;
  1432. endsym:=nil;
  1433. { store the original offset in symofs so that we can recalculate the
  1434. value field in the assembler }
  1435. symofs:=ofs;
  1436. value:=ofs;
  1437. { update sym info }
  1438. if assigned(sym) then
  1439. sym.increfs;
  1440. end;
  1441. constructor tai_const.Create_type_sym_offset(_typ : taiconst_type;_sym : tasmsymbol; ofs : asizeint);
  1442. begin
  1443. inherited Create;
  1444. typ:=ait_const;
  1445. consttype:=_typ;
  1446. { sym is allowed to be nil, this is used to write nil pointers }
  1447. sym:=_sym;
  1448. endsym:=nil;
  1449. { store the original offset in symofs so that we can recalculate the
  1450. value field in the assembler }
  1451. symofs:=ofs;
  1452. value:=ofs;
  1453. { update sym info }
  1454. if assigned(sym) then
  1455. sym.increfs;
  1456. end;
  1457. constructor tai_const.Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  1458. begin
  1459. self.create_sym_offset(_sym,0);
  1460. consttype:=_typ;
  1461. endsym:=_endsym;
  1462. endsym.increfs;
  1463. end;
  1464. constructor tai_const.Create_rel_sym_offset(_typ: taiconst_type; _sym,_endsym: tasmsymbol; _ofs: int64);
  1465. begin
  1466. self.create_sym_offset(_sym,_ofs);
  1467. consttype:=_typ;
  1468. endsym:=_endsym;
  1469. endsym.increfs;
  1470. end;
  1471. constructor tai_const.Create_rva_sym(_sym:tasmsymbol);
  1472. begin
  1473. self.create_sym_offset(_sym,0);
  1474. consttype:=aitconst_rva_symbol;
  1475. end;
  1476. constructor tai_const.Createname(const name:string;ofs:asizeint);
  1477. begin
  1478. self.Createname(name,AT_NONE,ofs);
  1479. end;
  1480. constructor tai_const.Createname(const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  1481. begin
  1482. self.create_sym_offset(current_asmdata.RefAsmSymbol(name,_symtyp),ofs);
  1483. end;
  1484. constructor tai_const.Create_type_name(_typ:taiconst_type;const name:string;ofs:asizeint);
  1485. begin
  1486. self.Create_type_name(_typ,name,AT_NONE,ofs);
  1487. end;
  1488. constructor tai_const.Create_type_name(_typ:taiconst_type;const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  1489. begin
  1490. self.create_sym_offset(current_asmdata.RefAsmSymbol(name,_symtyp),ofs);
  1491. consttype:=_typ;
  1492. end;
  1493. constructor tai_const.Create_nil_codeptr;
  1494. begin
  1495. self.Create_int_codeptr(0);
  1496. end;
  1497. constructor tai_const.Create_nil_dataptr;
  1498. begin
  1499. self.Create_int_dataptr(0);
  1500. end;
  1501. constructor tai_const.Create_int_codeptr(_value: int64);
  1502. begin
  1503. inherited Create;
  1504. typ:=ait_const;
  1505. {$ifdef i8086}
  1506. if current_settings.x86memorymodel in x86_far_code_models then
  1507. consttype:=aitconst_farptr
  1508. else
  1509. {$endif i8086}
  1510. {$ifdef avr}
  1511. consttype:=aitconst_gs;
  1512. {$else avr}
  1513. consttype:=aitconst_ptr;
  1514. {$endif avr}
  1515. sym:=nil;
  1516. endsym:=nil;
  1517. symofs:=0;
  1518. value:=_value;
  1519. end;
  1520. constructor tai_const.Create_int_dataptr(_value: int64);
  1521. begin
  1522. inherited Create;
  1523. typ:=ait_const;
  1524. {$ifdef i8086}
  1525. if current_settings.x86memorymodel in x86_far_data_models then
  1526. consttype:=aitconst_farptr
  1527. else
  1528. {$endif i8086}
  1529. consttype:=aitconst_ptr;
  1530. sym:=nil;
  1531. endsym:=nil;
  1532. symofs:=0;
  1533. value:=_value;
  1534. end;
  1535. {$ifdef i8086}
  1536. constructor tai_const.Create_seg_name(const name:string);
  1537. begin
  1538. self.Createname(name,0);
  1539. self.consttype:=aitconst_seg;
  1540. end;
  1541. constructor tai_const.Create_dgroup;
  1542. begin
  1543. self.Create_16bit(0);
  1544. self.consttype:=aitconst_dgroup;
  1545. end;
  1546. constructor tai_const.Create_fardataseg;
  1547. begin
  1548. self.Create_16bit(0);
  1549. self.consttype:=aitconst_fardataseg;
  1550. end;
  1551. {$endif i8086}
  1552. constructor tai_const.ppuload(t:taitype;ppufile:tcompilerppufile);
  1553. begin
  1554. inherited ppuload(t,ppufile);
  1555. consttype:=taiconst_type(ppufile.getbyte);
  1556. sym:=ppufile.getasmsymbol;
  1557. endsym:=ppufile.getasmsymbol;
  1558. value:=ppufile.getint64;
  1559. end;
  1560. procedure tai_const.ppuwrite(ppufile:tcompilerppufile);
  1561. begin
  1562. inherited ppuwrite(ppufile);
  1563. ppufile.putbyte(byte(consttype));
  1564. ppufile.putasmsymbol(sym);
  1565. ppufile.putasmsymbol(endsym);
  1566. ppufile.putint64(value);
  1567. end;
  1568. procedure tai_const.derefimpl;
  1569. begin
  1570. end;
  1571. function tai_const.getcopy:tlinkedlistitem;
  1572. begin
  1573. getcopy:=inherited getcopy;
  1574. { we need to increase the reference number }
  1575. if assigned(sym) then
  1576. sym.increfs;
  1577. if assigned(endsym) then
  1578. endsym.increfs;
  1579. end;
  1580. function tai_const.size:longint;
  1581. begin
  1582. case consttype of
  1583. aitconst_8bit :
  1584. result:=1;
  1585. aitconst_16bit,aitconst_16bit_unaligned :
  1586. result:=2;
  1587. aitconst_32bit,aitconst_darwin_dwarf_delta32,
  1588. aitconst_32bit_unaligned:
  1589. result:=4;
  1590. aitconst_64bit,aitconst_darwin_dwarf_delta64,
  1591. aitconst_64bit_unaligned:
  1592. result:=8;
  1593. aitconst_secrel32_symbol,
  1594. aitconst_rva_symbol :
  1595. if target_info.system=system_x86_64_win64 then
  1596. result:=sizeof(longint)
  1597. else
  1598. result:=sizeof(pint);
  1599. aitconst_uleb128bit :
  1600. result:=LengthUleb128(qword(value));
  1601. aitconst_sleb128bit :
  1602. result:=LengthSleb128(value);
  1603. aitconst_half16bit,
  1604. aitconst_gs:
  1605. result:=2;
  1606. aitconst_farptr:
  1607. result:=4;
  1608. aitconst_dgroup,
  1609. aitconst_fardataseg,
  1610. aitconst_seg:
  1611. result:=2;
  1612. aitconst_got:
  1613. result:=sizeof(pint);
  1614. aitconst_gotoff_symbol:
  1615. result:=4;
  1616. else
  1617. internalerror(200603253);
  1618. end;
  1619. end;
  1620. {****************************************************************************
  1621. TAI_realconst
  1622. ****************************************************************************}
  1623. constructor tai_realconst.create_s32real(val: ts32real);
  1624. begin
  1625. inherited create;
  1626. typ:=ait_realconst;
  1627. realtyp:=aitrealconst_s32bit;
  1628. savesize:=4;
  1629. value.s32val:=val;
  1630. end;
  1631. constructor tai_realconst.create_s64real(val: ts64real);
  1632. begin
  1633. inherited create;
  1634. typ:=ait_realconst;
  1635. realtyp:=aitrealconst_s64bit;
  1636. savesize:=8;
  1637. value.s64val:=val;
  1638. end;
  1639. {$ifdef ARM}
  1640. constructor tai_realconst.create_s64real_hiloswapped(val : ts64real);
  1641. begin
  1642. inherited create;
  1643. typ:=ait_realconst;
  1644. realtyp:=aitrealconst_s64bit;
  1645. value.s64val:=val;
  1646. savesize:=8;
  1647. formatoptions:=fo_hiloswapped;
  1648. end;
  1649. {$endif ARM}
  1650. constructor tai_realconst.create_s80real(val: ts80real; _savesize: byte);
  1651. begin
  1652. inherited create;
  1653. typ:=ait_realconst;
  1654. realtyp:=aitrealconst_s80bit;
  1655. savesize:=_savesize;
  1656. value.s80val:=val;
  1657. end;
  1658. constructor tai_realconst.create_s128real(val: ts128real);
  1659. begin
  1660. inherited create;
  1661. typ:=ait_realconst;
  1662. realtyp:=aitrealconst_s128bit;
  1663. savesize:=16;
  1664. value.s128val:=val;
  1665. end;
  1666. constructor tai_realconst.create_s64compreal(val: ts64comp);
  1667. begin
  1668. inherited create;
  1669. typ:=ait_realconst;
  1670. realtyp:=aitrealconst_s64comp;
  1671. savesize:=8;
  1672. value.s64compval:=val;
  1673. end;
  1674. constructor tai_realconst.ppuload(t: taitype; ppufile: tcompilerppufile);
  1675. begin
  1676. inherited;
  1677. realtyp:=tairealconsttype(ppufile.getbyte);
  1678. {$ifdef ARM}
  1679. formatoptions:=tformatoptions(ppufile.getbyte);
  1680. {$endif ARM}
  1681. case realtyp of
  1682. aitrealconst_s32bit:
  1683. value.s32val:=ppufile.getreal;
  1684. aitrealconst_s64bit:
  1685. value.s64val:=ppufile.getreal;
  1686. aitrealconst_s80bit:
  1687. value.s80val:=ppufile.getreal;
  1688. aitrealconst_s128bit:
  1689. value.s128val:=ppufile.getreal;
  1690. aitrealconst_s64comp:
  1691. value.s64compval:=comp(ppufile.getint64);
  1692. else
  1693. internalerror(2014050602);
  1694. end;
  1695. end;
  1696. procedure tai_realconst.ppuwrite(ppufile: tcompilerppufile);
  1697. var
  1698. c: comp;
  1699. begin
  1700. inherited ppuwrite(ppufile);
  1701. ppufile.putbyte(byte(realtyp));
  1702. {$ifdef ARM}
  1703. ppufile.putbyte(byte(formatoptions));
  1704. {$endif ARM}
  1705. case realtyp of
  1706. aitrealconst_s32bit:
  1707. ppufile.putreal(value.s32val);
  1708. aitrealconst_s64bit:
  1709. ppufile.putreal(value.s64val);
  1710. aitrealconst_s80bit:
  1711. ppufile.putreal(value.s80val);
  1712. aitrealconst_s128bit:
  1713. ppufile.putreal(value.s128val);
  1714. aitrealconst_s64comp:
  1715. begin
  1716. c:=comp(value.s64compval);
  1717. ppufile.putint64(int64(c));
  1718. end
  1719. else
  1720. internalerror(2014050601);
  1721. end;
  1722. end;
  1723. function tai_realconst.getcopy: tlinkedlistitem;
  1724. begin
  1725. result:=inherited getcopy;
  1726. tai_realconst(result).value:=value;
  1727. tai_realconst(result).realtyp:=realtyp;
  1728. tai_realconst(result).savesize:=savesize;
  1729. {$ifdef ARM}
  1730. tai_realconst(result).formatoptions:=formatoptions;
  1731. {$endif ARM}
  1732. end;
  1733. function tai_realconst.datasize: word;
  1734. begin
  1735. case realtyp of
  1736. aitrealconst_s32bit:
  1737. result:=4;
  1738. aitrealconst_s64bit,
  1739. aitrealconst_s64comp:
  1740. result:=8;
  1741. aitrealconst_s80bit:
  1742. result:=10;
  1743. aitrealconst_s128bit:
  1744. result:=16;
  1745. else
  1746. internalerror(2014050603);
  1747. end;
  1748. end;
  1749. {****************************************************************************
  1750. TAI_STRING
  1751. ****************************************************************************}
  1752. constructor tai_string.Create(const _str : string);
  1753. begin
  1754. inherited Create;
  1755. typ:=ait_string;
  1756. len:=length(_str);
  1757. getmem(str,len+1);
  1758. move(_str[1],str^,len);
  1759. str[len]:=#0;
  1760. end;
  1761. constructor tai_string.Create_pchar(_str : pchar;length : longint);
  1762. begin
  1763. inherited Create;
  1764. typ:=ait_string;
  1765. str:=_str;
  1766. len:=length;
  1767. end;
  1768. destructor tai_string.destroy;
  1769. begin
  1770. if str<>nil then
  1771. freemem(str);
  1772. inherited Destroy;
  1773. end;
  1774. constructor tai_string.ppuload(t:taitype;ppufile:tcompilerppufile);
  1775. begin
  1776. inherited ppuload(t,ppufile);
  1777. len:=ppufile.getlongint;
  1778. getmem(str,len+1);
  1779. ppufile.getdata(str^,len);
  1780. str[len]:=#0
  1781. end;
  1782. procedure tai_string.ppuwrite(ppufile:tcompilerppufile);
  1783. begin
  1784. inherited ppuwrite(ppufile);
  1785. ppufile.putlongint(len);
  1786. ppufile.putdata(str^,len);
  1787. end;
  1788. function tai_string.getcopy : tlinkedlistitem;
  1789. var
  1790. p : tlinkedlistitem;
  1791. begin
  1792. p:=inherited getcopy;
  1793. getmem(tai_string(p).str,len);
  1794. move(str^,tai_string(p).str^,len);
  1795. getcopy:=p;
  1796. end;
  1797. {****************************************************************************
  1798. TAI_LABEL
  1799. ****************************************************************************}
  1800. constructor tai_label.Create(_labsym : tasmlabel);
  1801. begin
  1802. inherited Create;
  1803. typ:=ait_label;
  1804. labsym:=_labsym;
  1805. labsym.is_set:=true;
  1806. end;
  1807. constructor tai_label.ppuload(t:taitype;ppufile:tcompilerppufile);
  1808. begin
  1809. inherited ppuload(t,ppufile);
  1810. labsym:=tasmlabel(ppufile.getasmsymbol);
  1811. ppufile.getbyte; { was is_global flag, now unused }
  1812. end;
  1813. procedure tai_label.ppuwrite(ppufile:tcompilerppufile);
  1814. begin
  1815. inherited ppuwrite(ppufile);
  1816. ppufile.putasmsymbol(labsym);
  1817. ppufile.putbyte(0); { was is_global flag, now unused }
  1818. end;
  1819. procedure tai_label.derefimpl;
  1820. begin
  1821. labsym.is_set:=true;
  1822. end;
  1823. {****************************************************************************
  1824. tai_comment comment to be inserted in the assembler file
  1825. ****************************************************************************}
  1826. constructor tai_comment.Create(_str : pchar);
  1827. begin
  1828. inherited Create;
  1829. typ:=ait_comment;
  1830. str:=_str;
  1831. end;
  1832. destructor tai_comment.destroy;
  1833. begin
  1834. freemem(str);
  1835. inherited Destroy;
  1836. end;
  1837. constructor tai_comment.ppuload(t:taitype;ppufile:tcompilerppufile);
  1838. var
  1839. len : longint;
  1840. begin
  1841. inherited ppuload(t,ppufile);
  1842. len:=ppufile.getlongint;
  1843. getmem(str,len+1);
  1844. ppufile.getdata(str^,len);
  1845. str[len]:=#0;
  1846. end;
  1847. procedure tai_comment.ppuwrite(ppufile:tcompilerppufile);
  1848. var
  1849. len : longint;
  1850. begin
  1851. inherited ppuwrite(ppufile);
  1852. len:=strlen(str);
  1853. ppufile.putlongint(len);
  1854. ppufile.putdata(str^,len);
  1855. end;
  1856. function tai_comment.getcopy : tlinkedlistitem;
  1857. var
  1858. p : tlinkedlistitem;
  1859. begin
  1860. p:=inherited getcopy;
  1861. getmem(tai_comment(p).str,strlen(str)+1);
  1862. move(str^,tai_comment(p).str^,strlen(str)+1);
  1863. getcopy:=p;
  1864. end;
  1865. {****************************************************************************
  1866. TAI_STABS
  1867. ****************************************************************************}
  1868. constructor tai_stab.create(_stabtype:TStabType;_str : pchar);
  1869. begin
  1870. inherited create;
  1871. typ:=ait_stab;
  1872. str:=_str;
  1873. stabtype:=_stabtype;
  1874. end;
  1875. constructor tai_stab.create_str(_stabtype:TStabType;const s:string);
  1876. begin
  1877. self.create(_stabtype,strpnew(s));
  1878. end;
  1879. constructor tai_stab.create_ansistr(_stabtype:TStabType;const s:ansistring);
  1880. begin
  1881. inherited create;
  1882. typ:=ait_stab;
  1883. stabtype:=_stabtype;
  1884. getmem(str,length(s)+1);
  1885. if length(s)>0 then
  1886. move(s[1],str^,length(s)+1)
  1887. else
  1888. str^:=#0;
  1889. end;
  1890. destructor tai_stab.destroy;
  1891. begin
  1892. freemem(str);
  1893. inherited destroy;
  1894. end;
  1895. {****************************************************************************
  1896. TAI_FORCE_LINE
  1897. ****************************************************************************}
  1898. constructor tai_force_line.create;
  1899. begin
  1900. inherited create;
  1901. typ:=ait_force_line;
  1902. end;
  1903. {****************************************************************************
  1904. TAI_FUNCTION_NAME
  1905. ****************************************************************************}
  1906. constructor tai_function_name.create(const s:string);
  1907. begin
  1908. inherited create;
  1909. typ:=ait_function_name;
  1910. funcname:=stringdup(s);
  1911. end;
  1912. destructor tai_function_name.destroy;
  1913. begin
  1914. stringdispose(funcname);
  1915. inherited destroy;
  1916. end;
  1917. {****************************************************************************
  1918. TAI_CUTOBJECT
  1919. ****************************************************************************}
  1920. constructor tai_cutobject.Create;
  1921. begin
  1922. inherited Create;
  1923. typ:=ait_cutobject;
  1924. place:=cut_normal;
  1925. end;
  1926. constructor tai_cutobject.Create_begin;
  1927. begin
  1928. inherited Create;
  1929. typ:=ait_cutobject;
  1930. place:=cut_begin;
  1931. end;
  1932. constructor tai_cutobject.Create_end;
  1933. begin
  1934. inherited Create;
  1935. typ:=ait_cutobject;
  1936. place:=cut_end;
  1937. end;
  1938. constructor tai_cutobject.ppuload(t:taitype;ppufile:tcompilerppufile);
  1939. begin
  1940. inherited ppuload(t,ppufile);
  1941. place:=TCutPlace(ppufile.getbyte);
  1942. end;
  1943. procedure tai_cutobject.ppuwrite(ppufile:tcompilerppufile);
  1944. begin
  1945. inherited ppuwrite(ppufile);
  1946. ppufile.putbyte(byte(place));
  1947. end;
  1948. {****************************************************************************
  1949. Tai_Marker
  1950. ****************************************************************************}
  1951. constructor Tai_Marker.Create(_Kind: TAsmMarker);
  1952. begin
  1953. Inherited Create;
  1954. typ := ait_marker;
  1955. Kind := _Kind;
  1956. end;
  1957. constructor Tai_Marker.ppuload(t:taitype;ppufile:tcompilerppufile);
  1958. begin
  1959. inherited ppuload(t,ppufile);
  1960. kind:=TAsmMarker(ppufile.getbyte);
  1961. end;
  1962. procedure Tai_Marker.ppuwrite(ppufile:tcompilerppufile);
  1963. begin
  1964. inherited ppuwrite(ppufile);
  1965. ppufile.putbyte(byte(kind));
  1966. end;
  1967. {*****************************************************************************
  1968. tai_tempalloc
  1969. *****************************************************************************}
  1970. constructor tai_tempalloc.alloc(pos,size:longint);
  1971. begin
  1972. inherited Create;
  1973. typ:=ait_tempalloc;
  1974. allocation:=true;
  1975. temppos:=pos;
  1976. tempsize:=size;
  1977. {$ifdef EXTDEBUG}
  1978. problem:=nil;
  1979. {$endif EXTDEBUG}
  1980. end;
  1981. destructor tai_tempalloc.destroy;
  1982. begin
  1983. {$ifdef EXTDEBUG}
  1984. stringdispose(problem);
  1985. {$endif EXTDEBUG}
  1986. inherited destroy;
  1987. end;
  1988. constructor tai_tempalloc.dealloc(pos,size:longint);
  1989. begin
  1990. inherited Create;
  1991. typ:=ait_tempalloc;
  1992. allocation:=false;
  1993. temppos:=pos;
  1994. tempsize:=size;
  1995. {$ifdef EXTDEBUG}
  1996. problem:=nil;
  1997. {$endif EXTDEBUG}
  1998. end;
  1999. {$ifdef EXTDEBUG}
  2000. constructor tai_tempalloc.allocinfo(pos,size:longint;const st:string);
  2001. begin
  2002. inherited Create;
  2003. typ:=ait_tempalloc;
  2004. allocation:=false;
  2005. temppos:=pos;
  2006. tempsize:=size;
  2007. problem:=stringdup(st);
  2008. end;
  2009. {$endif EXTDEBUG}
  2010. constructor tai_tempalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  2011. begin
  2012. inherited ppuload(t,ppufile);
  2013. temppos:=ppufile.getlongint;
  2014. tempsize:=ppufile.getlongint;
  2015. allocation:=boolean(ppufile.getbyte);
  2016. {$ifdef EXTDEBUG}
  2017. problem:=nil;
  2018. {$endif EXTDEBUG}
  2019. end;
  2020. procedure tai_tempalloc.ppuwrite(ppufile:tcompilerppufile);
  2021. begin
  2022. inherited ppuwrite(ppufile);
  2023. ppufile.putlongint(temppos);
  2024. ppufile.putlongint(tempsize);
  2025. ppufile.putbyte(byte(allocation));
  2026. end;
  2027. {*****************************************************************************
  2028. tai_regalloc
  2029. *****************************************************************************}
  2030. constructor tai_regalloc.alloc(r : tregister;ainstr:tai);
  2031. begin
  2032. inherited create;
  2033. typ:=ait_regalloc;
  2034. ratype:=ra_alloc;
  2035. reg:=r;
  2036. { ainstr must be an instruction }
  2037. if assigned(ainstr) and
  2038. (ainstr.typ<>ait_instruction) then
  2039. internalerror(200411011);
  2040. instr:=ainstr;
  2041. end;
  2042. constructor tai_regalloc.dealloc(r : tregister;ainstr:tai);
  2043. begin
  2044. inherited create;
  2045. typ:=ait_regalloc;
  2046. ratype:=ra_dealloc;
  2047. reg:=r;
  2048. { ainstr must be an instruction }
  2049. if assigned(ainstr) and
  2050. (ainstr.typ<>ait_instruction) then
  2051. internalerror(200411012);
  2052. instr:=ainstr;
  2053. end;
  2054. constructor tai_regalloc.sync(r : tregister);
  2055. begin
  2056. inherited create;
  2057. typ:=ait_regalloc;
  2058. ratype:=ra_sync;
  2059. reg:=r;
  2060. end;
  2061. constructor tai_regalloc.resize(r : tregister);
  2062. begin
  2063. inherited create;
  2064. typ:=ait_regalloc;
  2065. ratype:=ra_resize;
  2066. reg:=r;
  2067. end;
  2068. constructor tai_regalloc.markused(r : tregister);
  2069. begin
  2070. inherited create;
  2071. typ:=ait_regalloc;
  2072. ratype:=ra_markused;
  2073. reg:=r;
  2074. end;
  2075. constructor tai_regalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  2076. begin
  2077. inherited ppuload(t,ppufile);
  2078. ppufile.getdata(reg,sizeof(Tregister));
  2079. ratype:=tregalloctype(ppufile.getbyte);
  2080. keep:=boolean(ppufile.getbyte);
  2081. end;
  2082. procedure tai_regalloc.ppuwrite(ppufile:tcompilerppufile);
  2083. begin
  2084. inherited ppuwrite(ppufile);
  2085. ppufile.putdata(reg,sizeof(Tregister));
  2086. ppufile.putbyte(byte(ratype));
  2087. ppufile.putbyte(byte(keep));
  2088. end;
  2089. {*****************************************************************************
  2090. TaiInstruction
  2091. *****************************************************************************}
  2092. constructor tai_cpu_abstract.Create(op : tasmop);
  2093. begin
  2094. inherited create;
  2095. typ:=ait_instruction;
  2096. is_jmp:=false;
  2097. opcode:=op;
  2098. ops:=0;
  2099. end;
  2100. destructor tai_cpu_abstract.Destroy;
  2101. var
  2102. i : integer;
  2103. begin
  2104. for i:=0 to opercnt-1 do
  2105. freeop(i);
  2106. inherited destroy;
  2107. end;
  2108. { ---------------------------------------------------------------------
  2109. Loading of operands.
  2110. ---------------------------------------------------------------------}
  2111. procedure tai_cpu_abstract.allocate_oper(opers:longint);
  2112. begin
  2113. while (opers>opercnt) do
  2114. begin
  2115. new(oper[opercnt]);
  2116. fillchar(oper[opercnt]^,sizeof(toper),0);
  2117. inc(opercnt);
  2118. end;
  2119. end;
  2120. procedure tai_cpu_abstract.loadconst(opidx:longint;l:aint);
  2121. begin
  2122. allocate_oper(opidx+1);
  2123. with oper[opidx]^ do
  2124. begin
  2125. if typ<>top_const then
  2126. clearop(opidx);
  2127. val:=l;
  2128. typ:=top_const;
  2129. end;
  2130. end;
  2131. procedure tai_cpu_abstract.loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  2132. var
  2133. r : treference;
  2134. begin
  2135. reference_reset_symbol(r,s,sofs,1);
  2136. r.refaddr:=addr_full;
  2137. loadref(opidx,r);
  2138. end;
  2139. procedure tai_cpu_abstract.loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  2140. begin
  2141. if not assigned(s) then
  2142. internalerror(200204251);
  2143. allocate_oper(opidx+1);
  2144. with oper[opidx]^ do
  2145. begin
  2146. if typ<>top_local then
  2147. begin
  2148. clearop(opidx);
  2149. new(localoper);
  2150. end;
  2151. with oper[opidx]^.localoper^ do
  2152. begin
  2153. localsym:=s;
  2154. localsymofs:=sofs;
  2155. localindexreg:=indexreg;
  2156. localscale:=scale;
  2157. localgetoffset:=getoffset;
  2158. localforceref:=forceref;
  2159. end;
  2160. typ:=top_local;
  2161. end;
  2162. end;
  2163. procedure tai_cpu_abstract.loadref(opidx:longint;const r:treference);
  2164. begin
  2165. allocate_oper(opidx+1);
  2166. with oper[opidx]^ do
  2167. begin
  2168. if typ<>top_ref then
  2169. begin
  2170. clearop(opidx);
  2171. new(ref);
  2172. end;
  2173. ref^:=r;
  2174. {$ifdef x86}
  2175. { We allow this exception for x86, since overloading this would be
  2176. too much of a a speed penalty}
  2177. if (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  2178. segprefix:=ref^.segment;
  2179. {$endif}
  2180. {$ifndef llvm}
  2181. if (cs_create_pic in current_settings.moduleswitches) and
  2182. assigned(r.symbol) and
  2183. not assigned(r.relsymbol) and
  2184. (r.refaddr=addr_no)
  2185. {$ifdef ARM}
  2186. and not(r.base=NR_R15)
  2187. {$endif ARM}
  2188. {$ifdef aarch64}
  2189. and not(r.refaddr in [addr_full,addr_gotpageoffset,addr_gotpage])
  2190. {$endif aarch64}
  2191. then
  2192. internalerror(200502052);
  2193. {$endif not llvm}
  2194. typ:=top_ref;
  2195. if assigned(add_reg_instruction_hook) then
  2196. begin
  2197. add_reg_instruction_hook(self,ref^.base);
  2198. add_reg_instruction_hook(self,ref^.index);
  2199. end;
  2200. { mark symbol as used }
  2201. if assigned(ref^.symbol) then
  2202. ref^.symbol.increfs;
  2203. if assigned(ref^.relsymbol) then
  2204. ref^.relsymbol.increfs;
  2205. end;
  2206. end;
  2207. procedure tai_cpu_abstract.loadreg(opidx:longint;r:tregister);
  2208. begin
  2209. allocate_oper(opidx+1);
  2210. with oper[opidx]^ do
  2211. begin
  2212. if typ<>top_reg then
  2213. clearop(opidx);
  2214. reg:=r;
  2215. typ:=top_reg;
  2216. end;
  2217. if assigned(add_reg_instruction_hook) then
  2218. add_reg_instruction_hook(self,r);
  2219. {$ifdef ARM}
  2220. { R15 is the PC on the ARM thus moves to R15 are jumps.
  2221. Due to speed considerations we don't use a virtual overridden method here.
  2222. Because the pc/r15 isn't handled by the reg. allocator this should never cause
  2223. problems with iregs getting r15.
  2224. }
  2225. is_jmp:=(opcode=A_MOV) and (opidx=0) and (r=NR_R15);
  2226. {$endif ARM}
  2227. end;
  2228. procedure tai_cpu_abstract.loadoper(opidx:longint;o:toper);
  2229. begin
  2230. allocate_oper(opidx+1);
  2231. clearop(opidx);
  2232. oper[opidx]^:=o;
  2233. { copy also the reference }
  2234. with oper[opidx]^ do
  2235. begin
  2236. case typ of
  2237. top_reg:
  2238. begin
  2239. if assigned(add_reg_instruction_hook) then
  2240. add_reg_instruction_hook(self,reg);
  2241. end;
  2242. top_ref:
  2243. begin
  2244. new(ref);
  2245. ref^:=o.ref^;
  2246. {$ifdef x86}
  2247. if (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  2248. segprefix:=ref^.segment;
  2249. {$endif x86}
  2250. if assigned(add_reg_instruction_hook) then
  2251. begin
  2252. add_reg_instruction_hook(self,ref^.base);
  2253. add_reg_instruction_hook(self,ref^.index);
  2254. end;
  2255. end;
  2256. {$ifdef ARM}
  2257. top_shifterop:
  2258. begin
  2259. new(shifterop);
  2260. shifterop^:=o.shifterop^;
  2261. if assigned(add_reg_instruction_hook) then
  2262. add_reg_instruction_hook(self,shifterop^.rs);
  2263. end;
  2264. {$endif ARM}
  2265. end;
  2266. end;
  2267. end;
  2268. procedure tai_cpu_abstract.clearop(opidx:longint);
  2269. begin
  2270. with oper[opidx]^ do
  2271. begin
  2272. case typ of
  2273. top_ref:
  2274. dispose(ref);
  2275. top_local:
  2276. dispose(localoper);
  2277. {$ifdef ARM}
  2278. top_shifterop:
  2279. dispose(shifterop);
  2280. top_regset:
  2281. dispose(regset);
  2282. {$endif ARM}
  2283. {$ifdef m68k}
  2284. top_regset:
  2285. begin
  2286. dispose(dataregset);
  2287. dispose(addrregset);
  2288. dispose(fpuregset);
  2289. end;
  2290. {$endif m68k}
  2291. {$ifdef jvm}
  2292. top_string:
  2293. freemem(pcval);
  2294. top_wstring:
  2295. donewidestring(pwstrval);
  2296. {$endif jvm}
  2297. end;
  2298. typ:=top_none;
  2299. end;
  2300. end;
  2301. procedure tai_cpu_abstract.freeop(opidx:longint);
  2302. begin
  2303. clearop(opidx);
  2304. dispose(oper[opidx]);
  2305. end;
  2306. { ---------------------------------------------------------------------
  2307. Miscellaneous methods.
  2308. ---------------------------------------------------------------------}
  2309. procedure tai_cpu_abstract.SetCondition(const c:TAsmCond);
  2310. begin
  2311. condition:=c;
  2312. end;
  2313. Function tai_cpu_abstract.getcopy:TLinkedListItem;
  2314. var
  2315. i : longint;
  2316. p : tai_cpu_abstract;
  2317. begin
  2318. p:=tai_cpu_abstract(inherited getcopy);
  2319. { make a copy of the references }
  2320. p.opercnt:=0;
  2321. p.allocate_oper(ops);
  2322. for i:=0 to ops-1 do
  2323. begin
  2324. p.oper[i]^:=oper[i]^;
  2325. case oper[i]^.typ of
  2326. top_local :
  2327. begin
  2328. new(p.oper[i]^.localoper);
  2329. p.oper[i]^.localoper^:=oper[i]^.localoper^;
  2330. end;
  2331. top_ref :
  2332. begin
  2333. new(p.oper[i]^.ref);
  2334. p.oper[i]^.ref^:=oper[i]^.ref^;
  2335. end;
  2336. {$ifdef ARM}
  2337. top_shifterop:
  2338. begin
  2339. new(p.oper[i]^.shifterop);
  2340. p.oper[i]^.shifterop^:=oper[i]^.shifterop^;
  2341. end;
  2342. {$endif ARM}
  2343. end;
  2344. end;
  2345. getcopy:=p;
  2346. end;
  2347. function tai_cpu_abstract.is_same_reg_move(regtype: Tregistertype):boolean;
  2348. begin
  2349. { When the generic RA is used this needs to be overridden, we don't use
  2350. virtual;abstract; to prevent a lot of warnings of unimplemented abstract methods
  2351. when tai_cpu is created (PFV) }
  2352. internalerror(2004040901);
  2353. result:=false;
  2354. end;
  2355. function tai_cpu_abstract.spilling_get_operation_type(opnr: longint): topertype;
  2356. begin
  2357. internalerror(2004040902);
  2358. result:=operand_readwrite;
  2359. end;
  2360. function tai_cpu_abstract.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  2361. begin
  2362. result := operand_read;
  2363. end;
  2364. constructor tai_cpu_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  2365. var
  2366. i : integer;
  2367. begin
  2368. inherited ppuload(t,ppufile);
  2369. { hopefully, we don't get problems with big/litte endian here when cross compiling :/ }
  2370. ppufile.getdata(condition,sizeof(tasmcond));
  2371. allocate_oper(ppufile.getbyte);
  2372. for i:=0 to ops-1 do
  2373. ppuloadoper(ppufile,oper[i]^);
  2374. opcode:=tasmop(ppufile.getword);
  2375. {$ifdef x86}
  2376. ppufile.getdata(segprefix,sizeof(Tregister));
  2377. {$endif x86}
  2378. is_jmp:=boolean(ppufile.getbyte);
  2379. end;
  2380. procedure tai_cpu_abstract.ppuwrite(ppufile:tcompilerppufile);
  2381. var
  2382. i : integer;
  2383. begin
  2384. inherited ppuwrite(ppufile);
  2385. ppufile.putdata(condition,sizeof(tasmcond));
  2386. ppufile.putbyte(ops);
  2387. for i:=0 to ops-1 do
  2388. ppuwriteoper(ppufile,oper[i]^);
  2389. ppufile.putword(word(opcode));
  2390. {$ifdef x86}
  2391. ppufile.putdata(segprefix,sizeof(Tregister));
  2392. {$endif x86}
  2393. ppufile.putbyte(byte(is_jmp));
  2394. end;
  2395. procedure tai_cpu_abstract.buildderefimpl;
  2396. var
  2397. i : integer;
  2398. begin
  2399. for i:=0 to ops-1 do
  2400. ppubuildderefimploper(oper[i]^);
  2401. end;
  2402. procedure tai_cpu_abstract.derefimpl;
  2403. var
  2404. i : integer;
  2405. begin
  2406. for i:=0 to ops-1 do
  2407. ppuderefoper(oper[i]^);
  2408. end;
  2409. procedure tai_cpu_abstract.resetpass1;
  2410. begin
  2411. end;
  2412. procedure tai_cpu_abstract.resetpass2;
  2413. begin
  2414. end;
  2415. function tai_cpu_abstract.Pass1(objdata:TObjData):longint;
  2416. begin
  2417. result:=0;
  2418. end;
  2419. procedure tai_cpu_abstract.Pass2(objdata:TObjData);
  2420. begin
  2421. end;
  2422. procedure tai_cpu_abstract.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  2423. begin
  2424. o.typ:=toptype(ppufile.getbyte);
  2425. o.ot:=ppufile.getlongint;
  2426. case o.typ of
  2427. top_reg :
  2428. ppufile.getdata(o.reg,sizeof(Tregister));
  2429. top_ref :
  2430. begin
  2431. new(o.ref);
  2432. {$ifdef x86}
  2433. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  2434. {$endif x86}
  2435. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  2436. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  2437. ppufile.getdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  2438. o.ref^.scalefactor:=ppufile.getbyte;
  2439. o.ref^.offset:=ppufile.getaint;
  2440. o.ref^.symbol:=ppufile.getasmsymbol;
  2441. o.ref^.relsymbol:=ppufile.getasmsymbol;
  2442. end;
  2443. top_const :
  2444. o.val:=ppufile.getaint;
  2445. top_local :
  2446. begin
  2447. new(o.localoper);
  2448. with o.localoper^ do
  2449. begin
  2450. ppufile.getderef(localsymderef);
  2451. localsymofs:=ppufile.getaint;
  2452. localindexreg:=tregister(ppufile.getlongint);
  2453. localscale:=ppufile.getbyte;
  2454. localgetoffset:=(ppufile.getbyte<>0);
  2455. end;
  2456. end;
  2457. else
  2458. internalerror(2007010210);
  2459. end;
  2460. end;
  2461. procedure tai_cpu_abstract.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  2462. begin
  2463. ppufile.putbyte(byte(o.typ));
  2464. ppufile.putlongint(o.ot);
  2465. case o.typ of
  2466. top_reg :
  2467. ppufile.putdata(o.reg,sizeof(Tregister));
  2468. top_ref :
  2469. begin
  2470. {$ifdef x86}
  2471. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  2472. {$endif x86}
  2473. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  2474. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  2475. ppufile.putdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  2476. ppufile.putbyte(o.ref^.scalefactor);
  2477. ppufile.putaint(o.ref^.offset);
  2478. ppufile.putasmsymbol(o.ref^.symbol);
  2479. ppufile.putasmsymbol(o.ref^.relsymbol);
  2480. end;
  2481. top_const :
  2482. ppufile.putaint(o.val);
  2483. top_local :
  2484. begin
  2485. with o.localoper^ do
  2486. begin
  2487. ppufile.putderef(localsymderef);
  2488. ppufile.putaint(localsymofs);
  2489. ppufile.putlongint(longint(localindexreg));
  2490. ppufile.putbyte(localscale);
  2491. ppufile.putbyte(byte(localgetoffset));
  2492. end;
  2493. end;
  2494. else
  2495. internalerror(2007010211);
  2496. end;
  2497. end;
  2498. {****************************************************************************
  2499. tai_align_abstract
  2500. ****************************************************************************}
  2501. constructor tai_align_abstract.Create(b: byte);
  2502. begin
  2503. inherited Create;
  2504. typ:=ait_align;
  2505. {$ifdef EXTDEBUG}
  2506. if upper(classname)='TAI_ALIGN_ABSTRACT' then
  2507. internalerror(200709191);
  2508. {$endif EXTDEBUG}
  2509. if b in [1,2,4,8,16,32] then
  2510. aligntype := b
  2511. else
  2512. aligntype := 1;
  2513. fillsize:=0;
  2514. fillop:=0;
  2515. use_op:=false;
  2516. end;
  2517. constructor tai_align_abstract.Create_op(b: byte; _op: byte);
  2518. begin
  2519. inherited Create;
  2520. typ:=ait_align;
  2521. if b in [1,2,4,8,16,32] then
  2522. aligntype := b
  2523. else
  2524. aligntype := 1;
  2525. fillsize:=0;
  2526. fillop:=_op;
  2527. use_op:=true;
  2528. end;
  2529. constructor tai_align_abstract.Create_zeros(b: byte);
  2530. begin
  2531. inherited Create;
  2532. typ:=ait_align;
  2533. if b in [1,2,4,8,16,32] then
  2534. aligntype := b
  2535. else
  2536. aligntype := 1;
  2537. use_op:=true;
  2538. fillsize:=0;
  2539. fillop:=0;
  2540. end;
  2541. function tai_align_abstract.calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;
  2542. begin
  2543. if fillsize>sizeof(buf) then
  2544. internalerror(200404293);
  2545. fillchar(buf,high(buf),fillop);
  2546. calculatefillbuf:=pchar(@buf);
  2547. end;
  2548. constructor tai_align_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  2549. begin
  2550. inherited ppuload(t,ppufile);
  2551. aligntype:=ppufile.getbyte;
  2552. fillsize:=0;
  2553. fillop:=ppufile.getbyte;
  2554. use_op:=boolean(ppufile.getbyte);
  2555. end;
  2556. procedure tai_align_abstract.ppuwrite(ppufile:tcompilerppufile);
  2557. begin
  2558. inherited ppuwrite(ppufile);
  2559. ppufile.putbyte(aligntype);
  2560. ppufile.putbyte(fillop);
  2561. ppufile.putbyte(byte(use_op));
  2562. end;
  2563. {****************************************************************************
  2564. tai_seh_directive
  2565. ****************************************************************************}
  2566. const
  2567. datatypemap: array[TAsmSehDirective] of TSehDirectiveDatatype=(
  2568. sd_string, { proc }
  2569. sd_none, { endproc }
  2570. sd_none, { endprologue }
  2571. sd_string, { handler }
  2572. sd_none, { handlerdata }
  2573. sd_none,sd_none,sd_none, { eh, 32, no32 }
  2574. sd_regoffset, { setframe }
  2575. sd_offset, { stackalloc }
  2576. sd_reg, { pushreg }
  2577. sd_regoffset, { savereg }
  2578. sd_regoffset, { savexmm }
  2579. sd_none { pushframe }
  2580. );
  2581. constructor tai_seh_directive.create(_kind:TAsmSehDirective);
  2582. begin
  2583. inherited Create;
  2584. typ:=ait_seh_directive;
  2585. kind:=_kind;
  2586. data.typ:=datatypemap[_kind];
  2587. end;
  2588. constructor tai_seh_directive.create_name(_kind:TAsmSehDirective;const _name:string);
  2589. begin
  2590. create(_kind);
  2591. data.name:=stringdup(_name);
  2592. end;
  2593. constructor tai_seh_directive.create_reg(_kind:TAsmSehDirective;r:TRegister);
  2594. begin
  2595. create(_kind);
  2596. data.reg:=r;
  2597. end;
  2598. constructor tai_seh_directive.create_offset(_kind:TAsmSehDirective;ofs:dword);
  2599. begin
  2600. create(_kind);
  2601. data.offset:=ofs;
  2602. end;
  2603. constructor tai_seh_directive.create_reg_offset(_kind:TAsmSehDirective;
  2604. r:TRegister;ofs:dword);
  2605. begin
  2606. create(_kind);
  2607. data.offset:=ofs;
  2608. data.reg:=r;
  2609. end;
  2610. constructor tai_seh_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  2611. begin
  2612. inherited ppuload(t, ppufile);
  2613. kind:=TAsmSehDirective(ppufile.getbyte);
  2614. data.typ:=datatypemap[kind];
  2615. case data.typ of
  2616. sd_none: ;
  2617. sd_string:
  2618. begin
  2619. data.name:=stringdup(ppufile.getstring);
  2620. data.flags:=ppufile.getbyte;
  2621. end;
  2622. sd_reg,sd_offset,sd_regoffset:
  2623. begin
  2624. ppufile.getdata(data.reg,sizeof(TRegister));
  2625. data.offset:=ppufile.getdword;
  2626. end;
  2627. else
  2628. InternalError(2011091201);
  2629. end;
  2630. end;
  2631. destructor tai_seh_directive.destroy;
  2632. begin
  2633. if data.typ=sd_string then
  2634. stringdispose(data.name);
  2635. inherited destroy;
  2636. end;
  2637. procedure tai_seh_directive.ppuwrite(ppufile:tcompilerppufile);
  2638. begin
  2639. inherited ppuwrite(ppufile);
  2640. ppufile.putbyte(ord(kind));
  2641. case data.typ of
  2642. sd_none: ;
  2643. sd_string:
  2644. begin
  2645. ppufile.putstring(data.name^);
  2646. ppufile.putbyte(data.flags);
  2647. end;
  2648. sd_reg,sd_offset,sd_regoffset:
  2649. begin
  2650. ppufile.putdata(data.reg,sizeof(TRegister));
  2651. ppufile.putdword(data.offset);
  2652. end;
  2653. else
  2654. InternalError(2011091202);
  2655. end;
  2656. end;
  2657. procedure tai_seh_directive.generate_code(objdata:TObjData);
  2658. begin
  2659. end;
  2660. {$ifdef JVM}
  2661. {****************************************************************************
  2662. tai_jvar
  2663. ****************************************************************************}
  2664. constructor tai_jvar.Create(_stackslot: longint; const _desc: shortstring; _startlab, _stoplab: TAsmSymbol);
  2665. begin
  2666. Inherited create;
  2667. typ:=ait_jvar;
  2668. stackslot:=_stackslot;
  2669. desc:=stringdup(_desc);
  2670. startlab:=_startlab;
  2671. stoplab:=_stoplab;
  2672. end;
  2673. constructor tai_jvar.ppuload(t: taitype; ppufile: tcompilerppufile);
  2674. begin
  2675. inherited ppuload(t, ppufile);
  2676. stackslot:=ppufile.getlongint;
  2677. desc:=stringdup(ppufile.getstring);
  2678. startlab:=ppufile.getasmsymbol;
  2679. stoplab:=ppufile.getasmsymbol;
  2680. end;
  2681. procedure tai_jvar.ppuwrite(ppufile: tcompilerppufile);
  2682. begin
  2683. inherited ppuwrite(ppufile);
  2684. ppufile.putlongint(stackslot);
  2685. ppufile.putstring(desc^);
  2686. ppufile.putasmsymbol(startlab);
  2687. ppufile.putasmsymbol(stoplab);
  2688. end;
  2689. destructor tai_jvar.destroy;
  2690. begin
  2691. stringdispose(desc);
  2692. inherited destroy;
  2693. end;
  2694. {****************************************************************************
  2695. tai_jcatch
  2696. ****************************************************************************}
  2697. constructor tai_jcatch.Create(const _name: shortstring; _startlab, _stoplab, _handlerlab: TAsmSymbol);
  2698. begin
  2699. Inherited create;
  2700. typ:=ait_jcatch;
  2701. name:=stringdup(_name);
  2702. startlab:=_startlab;
  2703. startlab.increfs;
  2704. stoplab:=_stoplab;
  2705. stoplab.increfs;
  2706. handlerlab:=_handlerlab;
  2707. handlerlab.increfs;
  2708. end;
  2709. destructor tai_jcatch.destroy;
  2710. begin
  2711. stringdispose(name);
  2712. inherited destroy;
  2713. end;
  2714. constructor tai_jcatch.ppuload(t: taitype; ppufile: tcompilerppufile);
  2715. begin
  2716. inherited ppuload(t, ppufile);
  2717. name:=stringdup(ppufile.getstring);
  2718. startlab:=ppufile.getasmsymbol;
  2719. startlab.increfs;
  2720. stoplab:=ppufile.getasmsymbol;
  2721. stoplab.increfs;
  2722. handlerlab:=ppufile.getasmsymbol;
  2723. handlerlab.increfs;
  2724. end;
  2725. procedure tai_jcatch.ppuwrite(ppufile: tcompilerppufile);
  2726. begin
  2727. inherited ppuwrite(ppufile);
  2728. ppufile.putstring(name^);
  2729. ppufile.putasmsymbol(startlab);
  2730. ppufile.putasmsymbol(stoplab);
  2731. ppufile.putasmsymbol(handlerlab);
  2732. end;
  2733. {$endif JVM}
  2734. begin
  2735. {$push}{$warnings off}
  2736. { taitype should fit into a 4 byte set for speed reasons }
  2737. if ord(high(taitype))>31 then
  2738. internalerror(201108181);
  2739. {$pop}
  2740. end.