aasmtai.pas 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231
  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. {$ifdef alpha}
  63. { the follow is for the DEC Alpha }
  64. ait_frame,
  65. {$endif alpha}
  66. {$ifdef ia64}
  67. ait_bundle,
  68. ait_stop,
  69. {$endif ia64}
  70. {$ifdef m68k}
  71. ait_labeled_instruction,
  72. {$endif m68k}
  73. {$ifdef arm}
  74. ait_thumb_func,
  75. ait_thumb_set,
  76. {$endif arm}
  77. ait_set,
  78. ait_weak,
  79. { used to split into tiny assembler files }
  80. ait_cutobject,
  81. ait_regalloc,
  82. ait_tempalloc,
  83. { used to mark assembler blocks and inlined functions }
  84. ait_marker,
  85. { used to describe a new location of a variable }
  86. ait_varloc,
  87. {$ifdef JVM}
  88. { JVM only }
  89. ait_jvar, { debug information for a local variable }
  90. ait_jcatch, { exception catch clause }
  91. {$endif JVM}
  92. {$ifdef llvm}
  93. ait_llvmins, { llvm instruction }
  94. ait_llvmalias, { alias for a symbol }
  95. ait_llvmdecl, { llvm symbol declaration (global/external variable, external procdef) }
  96. {$endif}
  97. { SEH directives used in ARM,MIPS and x86_64 COFF targets }
  98. ait_seh_directive
  99. );
  100. taiconst_type = (
  101. aitconst_128bit,
  102. aitconst_64bit,
  103. aitconst_32bit,
  104. aitconst_16bit,
  105. aitconst_8bit,
  106. aitconst_sleb128bit,
  107. aitconst_uleb128bit,
  108. { win32 only }
  109. aitconst_rva_symbol,
  110. aitconst_secrel32_symbol,
  111. { darwin only }
  112. { From gcc/config/darwin.c (darwin_asm_output_dwarf_delta):
  113. ***
  114. Output a difference of two labels that will be an assembly time
  115. constant if the two labels are local. (.long lab1-lab2 will be
  116. very different if lab1 is at the boundary between two sections; it
  117. will be relocated according to the second section, not the first,
  118. so one ends up with a difference between labels in different
  119. sections, which is bad in the dwarf2 eh context for instance.)
  120. ***
  121. We cannot use this everywhere, because older versions of the
  122. darwin assembler don't support the construct used for these
  123. relsyms (nor do they support dwarf, for that matter)
  124. }
  125. aitconst_darwin_dwarf_delta64,
  126. aitconst_darwin_dwarf_delta32,
  127. { ARM Thumb-2 only }
  128. aitconst_half16bit, { used for table jumps. The actual value is the 16bit value shifted left once }
  129. { for use by dwarf debugger information }
  130. aitconst_16bit_unaligned,
  131. aitconst_32bit_unaligned,
  132. aitconst_64bit_unaligned,
  133. { i8086 far pointer; emits: 'DW symbol, SEG symbol' }
  134. aitconst_farptr,
  135. { offset of symbol's GOT slot in GOT }
  136. aitconst_got,
  137. { offset of symbol itself from GOT }
  138. aitconst_gotoff_symbol
  139. );
  140. tairealconsttype = (
  141. aitrealconst_s32bit,
  142. aitrealconst_s64bit,
  143. aitrealconst_s80bit,
  144. aitrealconst_s128bit,
  145. aitrealconst_s64comp
  146. );
  147. const
  148. {$if defined(cpu64bitaddr)}
  149. aitconst_ptr = aitconst_64bit;
  150. aitconst_ptr_unaligned = aitconst_64bit_unaligned;
  151. {$elseif defined(cpu32bitaddr)}
  152. aitconst_ptr = aitconst_32bit;
  153. aitconst_ptr_unaligned = aitconst_32bit_unaligned;
  154. {$elseif defined(cpu16bitaddr)}
  155. aitconst_ptr = aitconst_16bit;
  156. aitconst_ptr_unaligned = aitconst_16bit_unaligned;
  157. {$endif}
  158. {$if defined(cpu64bitalu)}
  159. aitconst_aint = aitconst_64bit;
  160. {$elseif defined(cpu32bitalu)}
  161. aitconst_aint = aitconst_32bit;
  162. {$elseif defined(cpu16bitalu)}
  163. aitconst_aint = aitconst_16bit;
  164. {$elseif defined(cpu8bitalu)}
  165. aitconst_aint = aitconst_8bit;
  166. {$endif}
  167. taitypestr : array[taitype] of string[24] = (
  168. '<none>',
  169. 'align',
  170. 'section',
  171. 'comment',
  172. 'string',
  173. 'instruction',
  174. 'datablock',
  175. 'symbol',
  176. 'symbol_end',
  177. 'symbol_directive',
  178. 'label',
  179. 'const',
  180. 'realconst',
  181. 'typedconst',
  182. 'stab',
  183. 'force_line',
  184. 'function_name',
  185. {$ifdef alpha}
  186. { the follow is for the DEC Alpha }
  187. 'frame',
  188. {$endif alpha}
  189. {$ifdef ia64}
  190. 'bundle',
  191. 'stop',
  192. {$endif ia64}
  193. {$ifdef m68k}
  194. 'labeled_instr',
  195. {$endif m68k}
  196. {$ifdef arm}
  197. 'thumb_func',
  198. 'thumb_set',
  199. {$endif arm}
  200. 'set',
  201. 'weak',
  202. 'cut',
  203. 'regalloc',
  204. 'tempalloc',
  205. 'marker',
  206. 'varloc',
  207. {$ifdef JVM}
  208. 'jvar',
  209. 'jcatch',
  210. {$endif JVM}
  211. {$ifdef llvm}
  212. 'llvmins',
  213. 'llvmalias',
  214. 'llvmdecl',
  215. {$endif}
  216. 'seh_directive'
  217. );
  218. type
  219. { Types of operand }
  220. toptype=(top_none,top_reg,top_ref,top_const,top_bool,top_local
  221. {$ifdef arm}
  222. { ARM only }
  223. ,top_regset
  224. ,top_modeflags
  225. ,top_specialreg
  226. {$endif arm}
  227. {$if defined(arm) or defined(aarch64)}
  228. ,top_conditioncode
  229. ,top_shifterop
  230. {$endif defined(arm) or defined(aarch64)}
  231. {$ifdef m68k}
  232. { m68k only }
  233. ,top_regset
  234. {$endif m68k}
  235. {$ifdef jvm}
  236. { jvm only}
  237. ,top_single
  238. ,top_double
  239. ,top_string
  240. ,top_wstring
  241. {$endif jvm}
  242. {$ifdef llvm}
  243. { llvm only }
  244. ,top_single
  245. ,top_double
  246. {$ifdef cpuextended}
  247. ,top_extended80
  248. {$endif cpuextended}
  249. ,top_tai
  250. ,top_def
  251. ,top_fpcond
  252. ,top_cond
  253. ,top_para
  254. {$endif llvm}
  255. );
  256. { kinds of operations that an instruction can perform on an operand }
  257. topertype = (operand_read,operand_write,operand_readwrite);
  258. tlocaloper = record
  259. localsym : pointer;
  260. localsymderef : tderef;
  261. localsymofs : longint;
  262. localindexreg : tregister;
  263. localscale : byte;
  264. localgetoffset,
  265. localforceref : boolean
  266. end;
  267. plocaloper = ^tlocaloper;
  268. const
  269. { ait_* types which don't result in executable code or which don't influence
  270. the way the program runs/behaves, but which may be encountered by the
  271. optimizer (= if it's sometimes added to the exprasm list). Update if you add
  272. a new ait type! }
  273. SkipInstr = [ait_comment, ait_symbol,ait_section
  274. ,ait_stab, ait_function_name, ait_force_line
  275. ,ait_regalloc, ait_tempalloc, ait_symbol_end
  276. ,ait_directive
  277. ,ait_varloc,
  278. {$ifdef JVM}
  279. ait_jvar, ait_jcatch,
  280. {$endif JVM}
  281. ait_seh_directive];
  282. { ait_* types which do not have line information (and hence which are of type
  283. tai, otherwise, they are of type tailineinfo }
  284. SkipLineInfo =[ait_label,
  285. ait_regalloc,ait_tempalloc,
  286. ait_stab,ait_function_name,
  287. ait_cutobject,ait_marker,ait_varloc,ait_align,ait_section,ait_comment,
  288. ait_const,ait_typedconst,ait_directive,
  289. {$ifdef arm}
  290. ait_thumb_func,
  291. ait_thumb_set,
  292. {$endif arm}
  293. ait_set,ait_weak,
  294. ait_realconst,
  295. ait_symbol,
  296. {$ifdef JVM}
  297. ait_jvar, ait_jcatch,
  298. {$endif JVM}
  299. {$ifdef llvm}
  300. ait_llvmdecl,
  301. {$endif llvm}
  302. ait_seh_directive
  303. ];
  304. type
  305. { cut type, required for alphanumeric ordering of the assembler filenames }
  306. TCutPlace=(cut_normal,cut_begin,cut_end);
  307. TAsmMarker = (
  308. mark_NoPropInfoStart,mark_NoPropInfoEnd,
  309. mark_AsmBlockStart,mark_AsmBlockEnd,
  310. mark_NoLineInfoStart,mark_NoLineInfoEnd,mark_BlockStart,
  311. mark_Position
  312. );
  313. TRegAllocType = (ra_alloc,ra_dealloc,ra_sync,ra_resize,ra_markused);
  314. TStabType = (stab_stabs,stab_stabn,stab_stabd,
  315. { AIX/XCOFF stab types }
  316. stab_stabx,
  317. { begin/end include file }
  318. stabx_bi,stabx_ei,
  319. { begin/end function }
  320. stabx_bf, stabx_ef,
  321. { begin/end static data block }
  322. stabx_bs, stabx_es,
  323. { line spec, function start/end label }
  324. stabx_line, stabx_function);
  325. TAsmDirective=(
  326. asd_indirect_symbol,
  327. asd_extern,asd_nasm_import, asd_toc_entry,
  328. asd_reference,asd_no_dead_strip,asd_weak_reference,asd_lazy_reference,
  329. asd_weak_definition,
  330. { for Jasmin }
  331. asd_jclass,asd_jinterface,asd_jsuper,asd_jfield,asd_jlimit,asd_jline,
  332. { .ent/.end for MIPS and Alpha }
  333. asd_ent,asd_ent_end,
  334. { supported by recent clang-based assemblers for data-in-code }
  335. asd_data_region, asd_end_data_region
  336. );
  337. TAsmSehDirective=(
  338. ash_proc,ash_endproc,
  339. ash_endprologue,ash_handler,ash_handlerdata,
  340. ash_eh,ash_32,ash_no32,
  341. ash_setframe,ash_stackalloc,ash_pushreg,
  342. ash_savereg,ash_savexmm,ash_pushframe
  343. );
  344. const
  345. regallocstr : array[tregalloctype] of string[10]=('allocated','released','sync','resized','used');
  346. tempallocstr : array[boolean] of string[10]=('released','allocated');
  347. stabtypestr : array[TStabType] of string[8]=(
  348. 'stabs','stabn','stabd',
  349. 'stabx',
  350. 'bi','ei',
  351. 'bf','ef',
  352. 'bs','es',
  353. 'line','function');
  354. directivestr : array[TAsmDirective] of string[23]=(
  355. 'indirect_symbol',
  356. 'extern','nasm_import', 'tc', 'reference',
  357. 'no_dead_strip','weak_reference','lazy_reference','weak_definition',
  358. { for Jasmin }
  359. 'class','interface','super','field','limit','line',
  360. { .ent/.end for MIPS and Alpha }
  361. 'ent','end',
  362. { supported by recent clang-based assemblers for data-in-code }
  363. 'data_region','end_data_region'
  364. );
  365. sehdirectivestr : array[TAsmSehDirective] of string[16]=(
  366. '.seh_proc','.seh_endproc',
  367. '.seh_endprologue','.seh_handler','.seh_handlerdata',
  368. '.seh_eh','.seh_32','seh_no32',
  369. '.seh_setframe','.seh_stackalloc','.seh_pushreg',
  370. '.seh_savereg','.seh_savexmm','.seh_pushframe'
  371. );
  372. type
  373. tai = class;
  374. { please keep the size of this record <=12 bytes and keep it properly aligned }
  375. toper = record
  376. ot : longint;
  377. case typ : toptype of
  378. top_none : ();
  379. top_reg : (reg:tregister);
  380. top_ref : (ref:preference);
  381. top_const : (val:tcgint);
  382. top_bool : (b:boolean);
  383. { local varsym that will be inserted in pass_generate_code }
  384. top_local : (localoper:plocaloper);
  385. {$ifdef arm}
  386. top_regset : (regset:^tcpuregisterset; regtyp: tregistertype; subreg: tsubregister; usermode: boolean);
  387. top_modeflags : (modeflags : tcpumodeflags);
  388. top_specialreg : (specialreg:tregister; specialflags:tspecialregflags);
  389. {$endif arm}
  390. {$if defined(arm) or defined(aarch64)}
  391. top_shifterop : (shifterop : pshifterop);
  392. top_conditioncode : (cc : TAsmCond);
  393. {$endif defined(arm) or defined(aarch64)}
  394. {$ifdef m68k}
  395. top_regset : (dataregset,addrregset,fpuregset:^tcpuregisterset);
  396. {$endif m68k}
  397. {$ifdef jvm}
  398. top_single : (sval:single);
  399. top_double : (dval:double);
  400. top_string : (pcvallen: aint; pcval: pchar);
  401. top_wstring : (pwstrval: pcompilerwidestring);
  402. {$endif jvm}
  403. {$ifdef llvm}
  404. top_single : (sval:single);
  405. top_double : (dval:double);
  406. {$ifdef cpuextended}
  407. top_extended80 : (eval:extended);
  408. {$endif cpuextended}
  409. top_tai : (ai: tai);
  410. top_def : (def: tdef);
  411. top_cond : (cond: topcmp);
  412. top_fpcond : (fpcond: tllvmfpcmp);
  413. top_para : (paras: tfplist);
  414. {$endif llvm}
  415. end;
  416. poper=^toper;
  417. { abstract assembler item }
  418. tai = class(TLinkedListItem)
  419. {$ifndef NOOPT}
  420. { pointer to record with optimizer info about this tai object }
  421. optinfo : pointer;
  422. {$endif NOOPT}
  423. typ : taitype;
  424. constructor Create;
  425. constructor ppuload(t:taitype;ppufile:tcompilerppufile);virtual;
  426. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  427. procedure buildderefimpl;virtual;
  428. procedure derefimpl;virtual;
  429. end;
  430. { abstract assembler item with line information }
  431. tailineinfo = class(tai)
  432. fileinfo : tfileposinfo;
  433. constructor Create;
  434. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  435. procedure ppuwrite(ppufile:tcompilerppufile);override;
  436. end;
  437. tai_simple = class(tai)
  438. constructor create(_typ : taitype);
  439. end;
  440. taiclass = class of tai;
  441. taiclassarray = array[taitype] of taiclass;
  442. { Generates an assembler string }
  443. tai_string = class(tailineinfo)
  444. str : pchar;
  445. { extra len so the string can contain an \0 }
  446. len : longint;
  447. constructor Create(const _str : string);
  448. constructor Create_pchar(_str : pchar;length : longint);
  449. destructor Destroy;override;
  450. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  451. procedure ppuwrite(ppufile:tcompilerppufile);override;
  452. function getcopy:tlinkedlistitem;override;
  453. end;
  454. { Generates a common label }
  455. tai_symbol = class(tai)
  456. sym : tasmsymbol;
  457. value : puint;
  458. size : longint;
  459. is_global,
  460. has_value : boolean;
  461. constructor Create(_sym:tasmsymbol;siz:longint);
  462. constructor Create_Global(_sym:tasmsymbol;siz:longint);
  463. constructor Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  464. constructor Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  465. constructor Createname_global_value(const _name : string;_symtyp:Tasmsymtype;siz:longint;val:ptruint);
  466. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  467. procedure ppuwrite(ppufile:tcompilerppufile);override;
  468. procedure derefimpl;override;
  469. end;
  470. tai_symbol_end = class(tailineinfo)
  471. sym : tasmsymbol;
  472. constructor Create(_sym:tasmsymbol);
  473. constructor Createname(const _name : string);
  474. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  475. procedure ppuwrite(ppufile:tcompilerppufile);override;
  476. procedure derefimpl;override;
  477. end;
  478. tai_directive = class(tailineinfo)
  479. name : ansistring;
  480. directive : TAsmDirective;
  481. constructor Create(_directive:TAsmDirective;const _name:ansistring);
  482. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  483. procedure ppuwrite(ppufile:tcompilerppufile);override;
  484. end;
  485. { Generates an assembler label }
  486. tai_label = class(tai)
  487. labsym : tasmlabel;
  488. {$ifdef arm}
  489. { set to true when the label has been moved by insertpcrelativedata to the correct location
  490. so one label can be used multiple times }
  491. moved : boolean;
  492. { true, if a label has been already inserted, this is important for arm thumb where no negative
  493. pc relative offsets are allowed }
  494. inserted : boolean;
  495. {$endif arm}
  496. constructor Create(_labsym : tasmlabel);
  497. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  498. procedure ppuwrite(ppufile:tcompilerppufile);override;
  499. procedure derefimpl;override;
  500. end;
  501. { Generates an assembler comment }
  502. tai_comment = class(tai)
  503. str : pchar;
  504. constructor Create(_str : pchar);
  505. destructor Destroy; override;
  506. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  507. procedure ppuwrite(ppufile:tcompilerppufile);override;
  508. function getcopy:tlinkedlistitem;override;
  509. end;
  510. { Generates a section / segment directive }
  511. tai_section = class(tai)
  512. sectype : TAsmSectiontype;
  513. secorder : TasmSectionorder;
  514. secalign : byte;
  515. name : pshortstring;
  516. sec : TObjSection; { used in binary writer }
  517. destructor Destroy;override;
  518. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  519. procedure ppuwrite(ppufile:tcompilerppufile);override;
  520. {$push}{$warnings off}
  521. private
  522. { this constructor is made private on purpose }
  523. { because sections should be created via new_section() }
  524. constructor Create(Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  525. {$pop}
  526. end;
  527. { Generates an uninitializised data block }
  528. tai_datablock = class(tailineinfo)
  529. is_global : boolean;
  530. sym : tasmsymbol;
  531. size : asizeint;
  532. constructor Create(const _name : string;_size : aint);
  533. constructor Create_global(const _name : string;_size : asizeint);
  534. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  535. procedure ppuwrite(ppufile:tcompilerppufile);override;
  536. procedure derefimpl;override;
  537. end;
  538. { Generates an integer const }
  539. { tai_const }
  540. tai_const = class(tai)
  541. sym,
  542. endsym : tasmsymbol;
  543. { if symbols and offset are provided the symofs is used,
  544. the value is calculated during assembling }
  545. symofs,
  546. value : int64;
  547. consttype : taiconst_type;
  548. { we use for the 128bit int64/qword for now because I can't imagine a
  549. case where we need 128 bit now (FK) }
  550. constructor Create(_typ:taiconst_type;_value : int64);
  551. constructor Create_128bit(_value : int64);
  552. constructor Create_64bit(_value : int64);
  553. constructor Create_32bit(_value : longint);
  554. constructor Create_16bit(_value : word);
  555. constructor Create_64bit_unaligned(_value : int64);
  556. constructor Create_32bit_unaligned(_value : longint);
  557. constructor Create_16bit_unaligned(_value : word);
  558. constructor Create_8bit(_value : byte);
  559. constructor Create_char(size: integer; _value: dword);
  560. constructor Create_sleb128bit(_value : int64);
  561. constructor Create_uleb128bit(_value : qword);
  562. constructor Create_aint(_value : aint);
  563. constructor Create_pint(_value : pint);
  564. constructor Create_pint_unaligned(_value : pint);
  565. constructor Create_sym(_sym:tasmsymbol);
  566. {$ifdef i8086}
  567. constructor Create_sym_near(_sym:tasmsymbol);
  568. constructor Create_sym_far(_sym:tasmsymbol);
  569. {$endif i8086}
  570. constructor Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  571. constructor Create_sym_offset(_sym:tasmsymbol;ofs:aint);
  572. constructor Create_type_sym_offset(_typ:taiconst_type;_sym:tasmsymbol;ofs:aint);
  573. constructor Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  574. constructor Create_rel_sym_offset(_typ : taiconst_type; _sym,_endsym : tasmsymbol; _ofs : int64);
  575. constructor Create_rva_sym(_sym:tasmsymbol);
  576. constructor Createname(const name:string;ofs:aint);
  577. constructor Createname(const name:string;_symtyp:Tasmsymtype;ofs:aint);
  578. constructor Create_type_name(_typ:taiconst_type;const name:string;ofs:aint);
  579. constructor Create_type_name(_typ:taiconst_type;const name:string;_symtyp:Tasmsymtype;ofs:aint);
  580. constructor Create_nil_codeptr;
  581. constructor Create_nil_dataptr;
  582. constructor Create_int_codeptr(_value: int64);
  583. constructor Create_int_dataptr(_value: int64);
  584. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  585. procedure ppuwrite(ppufile:tcompilerppufile);override;
  586. procedure derefimpl;override;
  587. function getcopy:tlinkedlistitem;override;
  588. function size:longint;
  589. end;
  590. { floating point const }
  591. tformatoptions = (fo_none,fo_hiloswapped);
  592. tai_realconst = class(tai)
  593. realtyp: tairealconsttype;
  594. savesize: byte;
  595. value: record
  596. case tairealconsttype of
  597. aitrealconst_s32bit: (s32val: ts32real);
  598. aitrealconst_s64bit: (s64val: ts64real);
  599. aitrealconst_s80bit: (s80val: ts80real);
  600. aitrealconst_s128bit: (s128val: ts128real);
  601. aitrealconst_s64comp: (s64compval: ts64comp);
  602. end;
  603. {$ifdef ARM}
  604. formatoptions : tformatoptions;
  605. {$endif ARM}
  606. constructor create_s32real(val: ts32real);
  607. constructor create_s64real(val: ts64real);
  608. {$ifdef ARM}
  609. constructor create_s64real_hiloswapped(val : ts64real);
  610. {$endif ARM}
  611. constructor create_s80real(val: ts80real; _savesize: byte);
  612. constructor create_s128real(val: ts128real);
  613. constructor create_s64compreal(val: ts64comp);
  614. constructor ppuload(t: taitype;ppufile: tcompilerppufile); override;
  615. procedure ppuwrite(ppufile: tcompilerppufile); override;
  616. function getcopy:tlinkedlistitem;override;
  617. function datasize: word;
  618. end;
  619. { tai_stab }
  620. tai_stab = class(tai)
  621. str : pchar;
  622. stabtype : TStabType;
  623. constructor Create(_stabtype:TStabType;_str : pchar);
  624. constructor Create_str(_stabtype:TStabType;const s:string);
  625. constructor create_ansistr(_stabtype: TStabType; const s: ansistring);
  626. destructor Destroy;override;
  627. end;
  628. tai_force_line = class(tailineinfo)
  629. constructor Create;
  630. end;
  631. tai_function_name = class(tai)
  632. funcname : pshortstring;
  633. constructor create(const s:string);
  634. destructor destroy;override;
  635. end;
  636. { Insert a cut to split assembler into several smaller files }
  637. tai_cutobject = class(tai)
  638. place : tcutplace;
  639. constructor Create;
  640. constructor Create_begin;
  641. constructor Create_end;
  642. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  643. procedure ppuwrite(ppufile:tcompilerppufile);override;
  644. end;
  645. { Insert a marker for assembler and inline blocks }
  646. tai_marker = class(tai)
  647. Kind: TAsmMarker;
  648. Constructor Create(_Kind: TAsmMarker);
  649. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  650. procedure ppuwrite(ppufile:tcompilerppufile);override;
  651. end;
  652. tai_tempalloc = class(tai)
  653. allocation : boolean;
  654. {$ifdef EXTDEBUG}
  655. problem : pshortstring;
  656. {$endif EXTDEBUG}
  657. temppos,
  658. tempsize : longint;
  659. constructor alloc(pos,size:longint);
  660. constructor dealloc(pos,size:longint);
  661. {$ifdef EXTDEBUG}
  662. constructor allocinfo(pos,size:longint;const st:string);
  663. {$endif EXTDEBUG}
  664. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  665. destructor destroy;override;
  666. procedure ppuwrite(ppufile:tcompilerppufile);override;
  667. end;
  668. tai_regalloc = class(tai)
  669. reg : tregister;
  670. ratype : TRegAllocType;
  671. { tells BuildLabelTableAndFixRegAlloc that the deallocation should be kept }
  672. keep : boolean;
  673. { reg(de)alloc belongs to this instruction, this
  674. is only used for automatic inserted (de)alloc for
  675. imaginary register and required for spilling code }
  676. instr : tai;
  677. constructor alloc(r : tregister;ainstr:tai);
  678. constructor dealloc(r : tregister;ainstr:tai);
  679. constructor sync(r : tregister);
  680. constructor resize(r : tregister);
  681. constructor markused(r : tregister);
  682. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  683. procedure ppuwrite(ppufile:tcompilerppufile);override;
  684. end;
  685. tadd_reg_instruction_proc=procedure(instr:Tai;r:tregister) of object;
  686. { Class template for assembler instructions
  687. }
  688. tai_cpu_abstract = class(tailineinfo)
  689. protected
  690. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);virtual;
  691. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);virtual;
  692. procedure ppubuildderefimploper(var o:toper);virtual;abstract;
  693. procedure ppuderefoper(var o:toper);virtual;abstract;
  694. public
  695. { Condition flags for instruction }
  696. condition : TAsmCond;
  697. { Number of operands to instruction }
  698. ops : byte;
  699. { Number of allocate oper structures }
  700. opercnt : byte;
  701. { Operands of instruction }
  702. oper : array[0..max_operands-1] of poper;
  703. { Actual opcode of instruction }
  704. opcode : tasmop;
  705. {$ifdef x86}
  706. segprefix : tregister;
  707. {$endif x86}
  708. { true if instruction is a jmp }
  709. is_jmp : boolean; { is this instruction a jump? (needed for optimizer) }
  710. Constructor Create(op : tasmop);virtual;
  711. Destructor Destroy;override;
  712. function getcopy:TLinkedListItem;override;
  713. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  714. procedure ppuwrite(ppufile:tcompilerppufile);override;
  715. procedure buildderefimpl;override;
  716. procedure derefimpl;override;
  717. procedure SetCondition(const c:TAsmCond);
  718. procedure allocate_oper(opers:longint);
  719. procedure loadconst(opidx:longint;l:aint);
  720. procedure loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  721. procedure loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  722. procedure loadref(opidx:longint;const r:treference);
  723. procedure loadreg(opidx:longint;r:tregister);
  724. procedure loadoper(opidx:longint;o:toper);
  725. procedure clearop(opidx:longint);
  726. procedure freeop(opidx:longint);
  727. { register allocator }
  728. function is_same_reg_move(regtype: Tregistertype):boolean;virtual;
  729. function spilling_get_operation_type(opnr: longint): topertype;virtual;
  730. function spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;virtual;
  731. function Pass1(objdata:TObjData):longint;virtual;
  732. procedure Pass2(objdata:TObjData);virtual;
  733. procedure resetpass1; virtual;
  734. procedure resetpass2; virtual;
  735. end;
  736. tai_cpu_class = class of tai_cpu_abstract;
  737. { Buffer type used for alignment }
  738. tfillbuffer = array[0..63] of char;
  739. { alignment for operator }
  740. tai_align_abstract = class(tai)
  741. aligntype : byte; { 1 = no align, 2 = word align, 4 = dword align }
  742. fillsize : byte; { real size to fill }
  743. fillop : byte; { value to fill with - optional }
  744. use_op : boolean;
  745. constructor Create(b:byte);virtual;
  746. constructor Create_op(b: byte; _op: byte);virtual;
  747. constructor Create_zeros(b:byte);
  748. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  749. procedure ppuwrite(ppufile:tcompilerppufile);override;
  750. function calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;virtual;
  751. end;
  752. tai_align_class = class of tai_align_abstract;
  753. tai_varloc = class(tai)
  754. oldlocation,
  755. oldlocationhi,
  756. newlocation,
  757. newlocationhi : tregister;
  758. varsym : tsym;
  759. constructor create(sym : tsym;loc : tregister);
  760. constructor create64(sym : tsym;loc,lochi : tregister);
  761. {$ifdef cpu64bitalu}
  762. constructor create128(sym : tsym;loc,lochi : tregister);
  763. {$endif cpu64bitalu}
  764. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  765. procedure ppuwrite(ppufile:tcompilerppufile);override;
  766. procedure buildderefimpl;override;
  767. procedure derefimpl;override;
  768. end;
  769. TSehDirectiveDatatype=(sd_none,sd_string,sd_reg,sd_offset,sd_regoffset);
  770. TSehDirectiveData=record
  771. case typ: TSehDirectiveDatatype of
  772. sd_none: ();
  773. sd_string: (name:pshortstring;flags:byte);
  774. sd_reg,sd_offset,sd_regoffset: (reg:TRegister;offset:dword);
  775. end;
  776. tai_seh_directive = class(tai)
  777. kind: TAsmSehDirective;
  778. data: TSehDirectiveData;
  779. constructor create(_kind:TAsmSehDirective);
  780. constructor create_name(_kind:TAsmSehDirective;const _name: string);
  781. constructor create_reg(_kind:TAsmSehDirective;r:TRegister);
  782. constructor create_offset(_kind:TAsmSehDirective;ofs:dword);
  783. constructor create_reg_offset(_kind:TAsmSehDirective;r:TRegister;ofs:dword);
  784. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  785. destructor destroy;override;
  786. procedure ppuwrite(ppufile:tcompilerppufile);override;
  787. procedure generate_code(objdata:TObjData);virtual;
  788. property datatype: TSehDirectiveDatatype read data.typ;
  789. end;
  790. tai_seh_directive_class=class of tai_seh_directive;
  791. {$ifdef JVM}
  792. { JVM variable live range description }
  793. tai_jvar = class(tai)
  794. stackslot: longint;
  795. desc: pshortstring;
  796. startlab,stoplab: tasmsymbol;
  797. constructor Create(_stackslot: longint; const _desc: shortstring; _startlab, _stoplab: TAsmSymbol);
  798. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  799. procedure ppuwrite(ppufile:tcompilerppufile);override;
  800. destructor destroy;override;
  801. end;
  802. tai_jvar_class = class of tai_jvar;
  803. { JVM exception catch description }
  804. tai_jcatch = class(tai)
  805. name: pshortstring;
  806. startlab,stoplab,handlerlab: tasmsymbol;
  807. constructor Create(const _name: shortstring; _startlab, _stoplab, _handlerlab: TAsmSymbol);
  808. destructor destroy;override;
  809. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  810. procedure ppuwrite(ppufile:tcompilerppufile);override;
  811. end;
  812. tai_jcatch_class = class of tai_jcatch;
  813. {$endif JVM}
  814. tai_set = class(tai)
  815. sym,
  816. value: pshortstring;
  817. constructor create(const asym, avalue: string);
  818. destructor destroy;override;
  819. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  820. procedure ppuwrite(ppufile:tcompilerppufile);override;
  821. end;
  822. {$ifdef arm}
  823. tai_thumb_set = class(tai_set)
  824. constructor create(const asym, avalue: string);
  825. end;
  826. {$endif arm}
  827. tai_weak = class(tai)
  828. sym: pshortstring;
  829. constructor create(const asym: string);
  830. destructor destroy;override;
  831. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  832. procedure ppuwrite(ppufile:tcompilerppufile);override;
  833. end;
  834. var
  835. { array with all class types for tais }
  836. aiclass : taiclassarray;
  837. { target specific tais, possibly overwritten in target specific aasmcpu }
  838. cai_align : tai_align_class = tai_align_abstract;
  839. cai_cpu : tai_cpu_class = tai_cpu_abstract;
  840. cai_seh_directive: tai_seh_directive_class = tai_seh_directive;
  841. { hook to notify uses of registers }
  842. add_reg_instruction_hook : tadd_reg_instruction_proc;
  843. procedure maybe_new_object_file(list:TAsmList);
  844. procedure new_section(list:TAsmList;Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  845. procedure section_symbol_start(list:TAsmList;const Aname:string;Asymtyp:Tasmsymtype;
  846. Aglobal:boolean;Asectype:TAsmSectiontype;Aalign:byte);
  847. procedure section_symbol_end(list:TAsmList;const Aname:string);
  848. function ppuloadai(ppufile:tcompilerppufile):tai;
  849. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  850. implementation
  851. uses
  852. SysUtils,
  853. verbose,
  854. globals;
  855. const
  856. pputaimarker = 254;
  857. {****************************************************************************
  858. Helpers
  859. ****************************************************************************}
  860. procedure maybe_new_object_file(list:TAsmList);
  861. begin
  862. if create_smartlink_library then
  863. list.concat(tai_cutobject.create);
  864. end;
  865. procedure new_section(list:TAsmList;Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  866. begin
  867. list.concat(tai_section.create(Asectype,Aname,Aalign,Asecorder));
  868. list.concat(cai_align.create(Aalign));
  869. end;
  870. procedure section_symbol_start(list:TAsmList;const Aname:string;Asymtyp:Tasmsymtype;
  871. Aglobal:boolean;Asectype:TAsmSectiontype;Aalign:byte);
  872. begin
  873. maybe_new_object_file(list);
  874. new_section(list,Asectype,Aname,Aalign);
  875. if Aglobal or
  876. create_smartlink then
  877. list.concat(tai_symbol.createname_global(Aname,Asymtyp,0))
  878. else
  879. list.concat(tai_symbol.createname(Aname,Asymtyp,0));
  880. end;
  881. procedure section_symbol_end(list:TAsmList;const Aname:string);
  882. begin
  883. list.concat(tai_symbol_end.createname(Aname));
  884. end;
  885. function ppuloadai(ppufile:tcompilerppufile):tai;
  886. var
  887. b : byte;
  888. t : taitype;
  889. begin
  890. { marker }
  891. b:=ppufile.getbyte;
  892. if b<>pputaimarker then
  893. internalerror(200208181);
  894. { load nodetype }
  895. t:=taitype(ppufile.getbyte);
  896. if t<>ait_none then
  897. begin
  898. if t>high(taitype) then
  899. internalerror(200208182);
  900. if not assigned(aiclass[t]) then
  901. internalerror(200208183);
  902. {writeln('taiload: ',taitypestr[t]);}
  903. { generate tai of the correct class }
  904. ppuloadai:=aiclass[t].ppuload(t,ppufile);
  905. end
  906. else
  907. ppuloadai:=nil;
  908. end;
  909. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  910. begin
  911. { marker, read by ppuloadnode }
  912. ppufile.putbyte(pputaimarker);
  913. if assigned(n) then
  914. begin
  915. { type, read by ppuloadnode }
  916. ppufile.putbyte(byte(n.typ));
  917. {writeln('taiwrite: ',taitypestr[n.typ]);}
  918. n.ppuwrite(ppufile);
  919. end
  920. else
  921. ppufile.putbyte(byte(ait_none));
  922. end;
  923. constructor tai_weak.create(const asym: string);
  924. begin
  925. inherited create;
  926. typ:=ait_weak;
  927. sym:=stringdup(asym);
  928. end;
  929. destructor tai_weak.destroy;
  930. begin
  931. stringdispose(sym);
  932. inherited destroy;
  933. end;
  934. constructor tai_weak.ppuload(t: taitype; ppufile: tcompilerppufile);
  935. begin
  936. inherited ppuload(t,ppufile);
  937. sym:=stringdup(ppufile.getstring);
  938. end;
  939. procedure tai_weak.ppuwrite(ppufile: tcompilerppufile);
  940. begin
  941. inherited ppuwrite(ppufile);
  942. ppufile.putstring(sym^);
  943. end;
  944. {$ifdef arm}
  945. constructor tai_thumb_set.create(const asym, avalue: string);
  946. begin
  947. inherited create(asym, avalue);
  948. typ:=ait_thumb_set;
  949. end;
  950. {$endif arm}
  951. constructor tai_set.create(const asym, avalue: string);
  952. begin
  953. inherited create;
  954. typ:=ait_set;
  955. sym:=stringdup(asym);
  956. value:=stringdup(avalue);
  957. end;
  958. destructor tai_set.destroy;
  959. begin
  960. stringdispose(sym);
  961. stringdispose(value);
  962. inherited destroy;
  963. end;
  964. constructor tai_set.ppuload(t: taitype; ppufile: tcompilerppufile);
  965. begin
  966. inherited ppuload(t,ppufile);
  967. sym:=stringdup(ppufile.getstring);
  968. value:=stringdup(ppufile.getstring);
  969. end;
  970. procedure tai_set.ppuwrite(ppufile: tcompilerppufile);
  971. begin
  972. inherited ppuwrite(ppufile);
  973. ppufile.putstring(sym^);
  974. ppufile.putstring(value^);
  975. end;
  976. constructor tai_varloc.create(sym: tsym; loc: tregister);
  977. begin
  978. inherited Create;
  979. typ:=ait_varloc;
  980. newlocation:=loc;
  981. newlocationhi:=NR_NO;
  982. varsym:=sym;
  983. oldlocationhi:=NR_NO;
  984. end;
  985. constructor tai_varloc.create64(sym: tsym; loc, lochi: tregister);
  986. begin
  987. inherited Create;
  988. typ:=ait_varloc;
  989. newlocation:=loc;
  990. newlocationhi:=lochi;
  991. varsym:=sym;
  992. end;
  993. {$ifdef cpu64bitalu}
  994. constructor tai_varloc.create128(sym: tsym; loc, lochi: tregister);
  995. begin
  996. inherited Create;
  997. typ:=ait_varloc;
  998. newlocation:=loc;
  999. newlocationhi:=lochi;
  1000. varsym:=sym;
  1001. end;
  1002. {$endif cpu64bitalu}
  1003. constructor tai_varloc.ppuload(t: taitype; ppufile: tcompilerppufile);
  1004. begin
  1005. inherited ppuload(t, ppufile);
  1006. end;
  1007. procedure tai_varloc.ppuwrite(ppufile: tcompilerppufile);
  1008. begin
  1009. inherited ppuwrite(ppufile);
  1010. end;
  1011. procedure tai_varloc.buildderefimpl;
  1012. begin
  1013. inherited buildderefimpl;
  1014. end;
  1015. procedure tai_varloc.derefimpl;
  1016. begin
  1017. inherited derefimpl;
  1018. end;
  1019. {****************************************************************************
  1020. TAI
  1021. ****************************************************************************}
  1022. constructor tai.Create;
  1023. begin
  1024. {$ifndef NOOPT}
  1025. optinfo:=nil;
  1026. {$endif NOOPT}
  1027. end;
  1028. constructor tai.ppuload(t:taitype;ppufile:tcompilerppufile);
  1029. begin
  1030. typ:=t;
  1031. {$ifndef NOOPT}
  1032. optinfo:=nil;
  1033. {$endif}
  1034. end;
  1035. procedure tai.ppuwrite(ppufile:tcompilerppufile);
  1036. begin
  1037. end;
  1038. procedure tai.buildderefimpl;
  1039. begin
  1040. end;
  1041. procedure tai.derefimpl;
  1042. begin
  1043. end;
  1044. {****************************************************************************
  1045. TAILINEINFO
  1046. ****************************************************************************}
  1047. constructor tailineinfo.create;
  1048. begin
  1049. inherited create;
  1050. fileinfo:=current_filepos;
  1051. end;
  1052. constructor tailineinfo.ppuload(t:taitype;ppufile:tcompilerppufile);
  1053. begin
  1054. inherited ppuload(t,ppufile);
  1055. ppufile.getposinfo(fileinfo);
  1056. end;
  1057. procedure tailineinfo.ppuwrite(ppufile:tcompilerppufile);
  1058. begin
  1059. inherited ppuwrite(ppufile);
  1060. ppufile.putposinfo(fileinfo);
  1061. end;
  1062. {****************************************************************************
  1063. TAI_SIMPLE
  1064. ****************************************************************************}
  1065. constructor tai_simple.create(_typ : taitype);
  1066. begin
  1067. inherited create;
  1068. typ:=_typ;
  1069. end;
  1070. {****************************************************************************
  1071. TAI_SECTION
  1072. ****************************************************************************}
  1073. constructor tai_section.Create(Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  1074. begin
  1075. inherited Create;
  1076. typ:=ait_section;
  1077. sectype:=asectype;
  1078. secalign:=Aalign;
  1079. secorder:=Asecorder;
  1080. name:=stringdup(Aname);
  1081. sec:=nil;
  1082. end;
  1083. constructor tai_section.ppuload(t:taitype;ppufile:tcompilerppufile);
  1084. begin
  1085. inherited ppuload(t,ppufile);
  1086. sectype:=TAsmSectiontype(ppufile.getbyte);
  1087. secalign:=ppufile.getbyte;
  1088. name:=stringdup(ppufile.getstring);
  1089. sec:=nil;
  1090. end;
  1091. destructor tai_section.Destroy;
  1092. begin
  1093. stringdispose(name);
  1094. end;
  1095. procedure tai_section.ppuwrite(ppufile:tcompilerppufile);
  1096. begin
  1097. inherited ppuwrite(ppufile);
  1098. ppufile.putbyte(byte(sectype));
  1099. ppufile.putbyte(secalign);
  1100. ppufile.putstring(name^);
  1101. end;
  1102. {****************************************************************************
  1103. TAI_DATABLOCK
  1104. ****************************************************************************}
  1105. constructor tai_datablock.Create(const _name : string;_size : aint);
  1106. begin
  1107. inherited Create;
  1108. typ:=ait_datablock;
  1109. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,AT_DATA);
  1110. { keep things aligned }
  1111. if _size<=0 then
  1112. _size:=sizeof(aint);
  1113. size:=_size;
  1114. is_global:=false;
  1115. end;
  1116. constructor tai_datablock.Create_global(const _name : string;_size : asizeint);
  1117. begin
  1118. inherited Create;
  1119. typ:=ait_datablock;
  1120. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,AT_DATA);
  1121. { keep things aligned }
  1122. if _size<=0 then
  1123. _size:=sizeof(aint);
  1124. size:=_size;
  1125. is_global:=true;
  1126. end;
  1127. constructor tai_datablock.ppuload(t:taitype;ppufile:tcompilerppufile);
  1128. begin
  1129. inherited Create;
  1130. sym:=ppufile.getasmsymbol;
  1131. size:=ppufile.getaint;
  1132. is_global:=boolean(ppufile.getbyte);
  1133. end;
  1134. procedure tai_datablock.ppuwrite(ppufile:tcompilerppufile);
  1135. begin
  1136. inherited ppuwrite(ppufile);
  1137. ppufile.putasmsymbol(sym);
  1138. ppufile.putaint(size);
  1139. ppufile.putbyte(byte(is_global));
  1140. end;
  1141. procedure tai_datablock.derefimpl;
  1142. begin
  1143. end;
  1144. {****************************************************************************
  1145. TAI_SYMBOL
  1146. ****************************************************************************}
  1147. constructor tai_symbol.Create(_sym:tasmsymbol;siz:longint);
  1148. begin
  1149. inherited Create;
  1150. typ:=ait_symbol;
  1151. sym:=_sym;
  1152. size:=siz;
  1153. { don't redefine global/external symbols as local, as code to access
  1154. such symbols is different on some platforms }
  1155. if not(sym.bind in [AB_NONE,AB_LOCAL]) then
  1156. internalerror(2013081601);
  1157. sym.bind:=AB_LOCAL;
  1158. is_global:=false;
  1159. end;
  1160. constructor tai_symbol.Create_global(_sym:tasmsymbol;siz:longint);
  1161. begin
  1162. inherited Create;
  1163. typ:=ait_symbol;
  1164. sym:=_sym;
  1165. size:=siz;
  1166. { don't override PRIVATE_EXTERN with GLOBAL }
  1167. if not(sym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN]) then
  1168. sym.bind:=AB_GLOBAL;
  1169. is_global:=true;
  1170. end;
  1171. constructor tai_symbol.Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  1172. begin
  1173. inherited Create;
  1174. typ:=ait_symbol;
  1175. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,_symtyp);
  1176. size:=siz;
  1177. is_global:=false;
  1178. end;
  1179. constructor tai_symbol.Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  1180. begin
  1181. inherited Create;
  1182. typ:=ait_symbol;
  1183. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,_symtyp);
  1184. size:=siz;
  1185. is_global:=true;
  1186. end;
  1187. constructor tai_symbol.createname_global_value(const _name: string;_symtyp: tasmsymtype; siz: longint; val: ptruint);
  1188. begin
  1189. Createname_global(_name,_symtyp,siz);
  1190. value:=val;
  1191. has_value:=true;
  1192. end;
  1193. constructor tai_symbol.ppuload(t:taitype;ppufile:tcompilerppufile);
  1194. begin
  1195. inherited ppuload(t,ppufile);
  1196. sym:=ppufile.getasmsymbol;
  1197. size:=ppufile.getlongint;
  1198. is_global:=boolean(ppufile.getbyte);
  1199. end;
  1200. procedure tai_symbol.ppuwrite(ppufile:tcompilerppufile);
  1201. begin
  1202. inherited ppuwrite(ppufile);
  1203. ppufile.putasmsymbol(sym);
  1204. ppufile.putlongint(size);
  1205. ppufile.putbyte(byte(is_global));
  1206. end;
  1207. procedure tai_symbol.derefimpl;
  1208. begin
  1209. end;
  1210. {****************************************************************************
  1211. TAI_SYMBOL_END
  1212. ****************************************************************************}
  1213. constructor tai_symbol_end.Create(_sym:tasmsymbol);
  1214. begin
  1215. inherited Create;
  1216. typ:=ait_symbol_end;
  1217. sym:=_sym;
  1218. end;
  1219. constructor tai_symbol_end.Createname(const _name : string);
  1220. begin
  1221. inherited Create;
  1222. typ:=ait_symbol_end;
  1223. sym:=current_asmdata.GetAsmSymbol(_name);
  1224. if not assigned(sym) then
  1225. internalerror(2013080301);
  1226. end;
  1227. constructor tai_symbol_end.ppuload(t:taitype;ppufile:tcompilerppufile);
  1228. begin
  1229. inherited ppuload(t,ppufile);
  1230. sym:=ppufile.getasmsymbol;
  1231. end;
  1232. procedure tai_symbol_end.ppuwrite(ppufile:tcompilerppufile);
  1233. begin
  1234. inherited ppuwrite(ppufile);
  1235. ppufile.putasmsymbol(sym);
  1236. end;
  1237. procedure tai_symbol_end.derefimpl;
  1238. begin
  1239. end;
  1240. {****************************************************************************
  1241. TAI_SYMBOL_END
  1242. ****************************************************************************}
  1243. constructor tai_directive.Create(_directive:TAsmDirective;const _name:ansistring);
  1244. begin
  1245. inherited Create;
  1246. typ:=ait_directive;
  1247. name:=_name;
  1248. directive:=_directive;
  1249. end;
  1250. constructor tai_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  1251. begin
  1252. inherited ppuload(t,ppufile);
  1253. name:=ppufile.getansistring;
  1254. directive:=TAsmDirective(ppufile.getbyte);
  1255. end;
  1256. procedure tai_directive.ppuwrite(ppufile:tcompilerppufile);
  1257. begin
  1258. inherited ppuwrite(ppufile);
  1259. ppufile.putansistring(name);
  1260. ppufile.putbyte(byte(directive));
  1261. end;
  1262. {****************************************************************************
  1263. TAI_CONST
  1264. ****************************************************************************}
  1265. constructor tai_const.Create(_typ:taiconst_type;_value : int64);
  1266. begin
  1267. inherited Create;
  1268. typ:=ait_const;
  1269. consttype:=_typ;
  1270. value:=_value;
  1271. sym:=nil;
  1272. endsym:=nil;
  1273. end;
  1274. constructor tai_const.Create_128bit(_value : int64);
  1275. begin
  1276. inherited Create;
  1277. typ:=ait_const;
  1278. consttype:=aitconst_128bit;
  1279. value:=_value;
  1280. sym:=nil;
  1281. endsym:=nil;
  1282. end;
  1283. constructor tai_const.Create_64bit(_value : int64);
  1284. begin
  1285. inherited Create;
  1286. typ:=ait_const;
  1287. consttype:=aitconst_64bit;
  1288. value:=_value;
  1289. sym:=nil;
  1290. endsym:=nil;
  1291. end;
  1292. constructor tai_const.Create_32bit(_value : longint);
  1293. begin
  1294. inherited Create;
  1295. typ:=ait_const;
  1296. consttype:=aitconst_32bit;
  1297. value:=_value;
  1298. sym:=nil;
  1299. endsym:=nil;
  1300. end;
  1301. constructor tai_const.Create_16bit(_value : word);
  1302. begin
  1303. inherited Create;
  1304. typ:=ait_const;
  1305. consttype:=aitconst_16bit;
  1306. value:=_value;
  1307. sym:=nil;
  1308. endsym:=nil;
  1309. end;
  1310. constructor tai_const.Create_64bit_unaligned(_value : int64);
  1311. begin
  1312. inherited Create;
  1313. typ:=ait_const;
  1314. consttype:=aitconst_64bit_unaligned;
  1315. value:=_value;
  1316. sym:=nil;
  1317. endsym:=nil;
  1318. end;
  1319. constructor tai_const.Create_32bit_unaligned(_value : longint);
  1320. begin
  1321. inherited Create;
  1322. typ:=ait_const;
  1323. consttype:=aitconst_32bit_unaligned;
  1324. value:=_value;
  1325. sym:=nil;
  1326. endsym:=nil;
  1327. end;
  1328. constructor tai_const.Create_16bit_unaligned(_value : word);
  1329. begin
  1330. inherited Create;
  1331. typ:=ait_const;
  1332. consttype:=aitconst_16bit_unaligned;
  1333. value:=_value;
  1334. sym:=nil;
  1335. endsym:=nil;
  1336. end;
  1337. constructor tai_const.Create_8bit(_value : byte);
  1338. begin
  1339. inherited Create;
  1340. typ:=ait_const;
  1341. consttype:=aitconst_8bit;
  1342. value:=_value;
  1343. sym:=nil;
  1344. endsym:=nil;
  1345. end;
  1346. constructor tai_const.Create_char(size: integer; _value: dword);
  1347. begin
  1348. inherited Create;
  1349. typ:=ait_const;
  1350. case size of
  1351. 1:
  1352. begin
  1353. consttype:=aitconst_8bit;
  1354. value:=byte(_value)
  1355. end;
  1356. 2:
  1357. begin
  1358. consttype:=aitconst_16bit;
  1359. value:=word(_value)
  1360. end
  1361. else
  1362. InternalError(2010030701)
  1363. end
  1364. end;
  1365. constructor tai_const.Create_sleb128bit(_value : int64);
  1366. begin
  1367. inherited Create;
  1368. typ:=ait_const;
  1369. consttype:=aitconst_sleb128bit;
  1370. value:=_value;
  1371. sym:=nil;
  1372. endsym:=nil;
  1373. end;
  1374. constructor tai_const.Create_uleb128bit(_value : qword);
  1375. begin
  1376. inherited Create;
  1377. typ:=ait_const;
  1378. consttype:=aitconst_uleb128bit;
  1379. value:=int64(_value);
  1380. sym:=nil;
  1381. endsym:=nil;
  1382. end;
  1383. constructor tai_const.Create_aint(_value : aint);
  1384. begin
  1385. inherited Create;
  1386. typ:=ait_const;
  1387. consttype:=aitconst_aint;
  1388. value:=_value;
  1389. sym:=nil;
  1390. endsym:=nil;
  1391. end;
  1392. constructor tai_const.Create_pint(_value : pint);
  1393. begin
  1394. inherited Create;
  1395. typ:=ait_const;
  1396. consttype:=aitconst_ptr;
  1397. value:=_value;
  1398. sym:=nil;
  1399. endsym:=nil;
  1400. end;
  1401. constructor tai_const.Create_pint_unaligned(_value: pint);
  1402. begin
  1403. inherited Create;
  1404. typ:=ait_const;
  1405. consttype:=aitconst_ptr_unaligned;
  1406. value:=_value;
  1407. sym:=nil;
  1408. endsym:=nil;
  1409. end;
  1410. constructor tai_const.Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  1411. begin
  1412. inherited Create;
  1413. typ:=ait_const;
  1414. consttype:=_typ;
  1415. sym:=_sym;
  1416. endsym:=nil;
  1417. value:=0;
  1418. { update sym info }
  1419. if assigned(sym) then
  1420. sym.increfs;
  1421. end;
  1422. constructor tai_const.Create_sym(_sym:tasmsymbol);
  1423. begin
  1424. self.create_sym_offset(_sym,0);
  1425. end;
  1426. {$ifdef i8086}
  1427. constructor tai_const.Create_sym_near(_sym: tasmsymbol);
  1428. begin
  1429. self.create_sym(_sym);
  1430. consttype:=aitconst_ptr;
  1431. end;
  1432. constructor tai_const.Create_sym_far(_sym: tasmsymbol);
  1433. begin
  1434. self.create_sym(_sym);
  1435. consttype:=aitconst_farptr;
  1436. end;
  1437. {$endif i8086}
  1438. constructor tai_const.Create_sym_offset(_sym:tasmsymbol;ofs:aint);
  1439. begin
  1440. inherited Create;
  1441. typ:=ait_const;
  1442. {$ifdef i8086}
  1443. if assigned(_sym) and (_sym.typ=AT_DATA) then
  1444. begin
  1445. if current_settings.x86memorymodel in x86_far_data_models then
  1446. consttype:=aitconst_farptr
  1447. else
  1448. consttype:=aitconst_ptr;
  1449. end
  1450. else
  1451. begin
  1452. if current_settings.x86memorymodel in x86_far_code_models then
  1453. consttype:=aitconst_farptr
  1454. else
  1455. consttype:=aitconst_ptr;
  1456. end;
  1457. {$else i8086}
  1458. consttype:=aitconst_ptr;
  1459. {$endif i8086}
  1460. { sym is allowed to be nil, this is used to write nil pointers }
  1461. sym:=_sym;
  1462. endsym:=nil;
  1463. { store the original offset in symofs so that we can recalculate the
  1464. value field in the assembler }
  1465. symofs:=ofs;
  1466. value:=ofs;
  1467. { update sym info }
  1468. if assigned(sym) then
  1469. sym.increfs;
  1470. end;
  1471. constructor tai_const.Create_type_sym_offset(_typ : taiconst_type;_sym : tasmsymbol; ofs : aint);
  1472. begin
  1473. inherited Create;
  1474. typ:=ait_const;
  1475. consttype:=_typ;
  1476. { sym is allowed to be nil, this is used to write nil pointers }
  1477. sym:=_sym;
  1478. endsym:=nil;
  1479. { store the original offset in symofs so that we can recalculate the
  1480. value field in the assembler }
  1481. symofs:=ofs;
  1482. value:=ofs;
  1483. { update sym info }
  1484. if assigned(sym) then
  1485. sym.increfs;
  1486. end;
  1487. constructor tai_const.Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  1488. begin
  1489. self.create_sym_offset(_sym,0);
  1490. consttype:=_typ;
  1491. endsym:=_endsym;
  1492. endsym.increfs;
  1493. end;
  1494. constructor tai_const.Create_rel_sym_offset(_typ: taiconst_type; _sym,_endsym: tasmsymbol; _ofs: int64);
  1495. begin
  1496. self.create_sym_offset(_sym,_ofs);
  1497. consttype:=_typ;
  1498. endsym:=_endsym;
  1499. endsym.increfs;
  1500. end;
  1501. constructor tai_const.Create_rva_sym(_sym:tasmsymbol);
  1502. begin
  1503. self.create_sym_offset(_sym,0);
  1504. consttype:=aitconst_rva_symbol;
  1505. end;
  1506. constructor tai_const.Createname(const name:string;ofs:aint);
  1507. begin
  1508. self.Createname(name,AT_NONE,ofs);
  1509. end;
  1510. constructor tai_const.Createname(const name:string;_symtyp:Tasmsymtype;ofs:aint);
  1511. begin
  1512. self.create_sym_offset(current_asmdata.RefAsmSymbol(name,_symtyp),ofs);
  1513. end;
  1514. constructor tai_const.Create_type_name(_typ:taiconst_type;const name:string;ofs:aint);
  1515. begin
  1516. self.Create_type_name(_typ,name,AT_NONE,ofs);
  1517. end;
  1518. constructor tai_const.Create_type_name(_typ:taiconst_type;const name:string;_symtyp:Tasmsymtype;ofs:aint);
  1519. begin
  1520. self.create_sym_offset(current_asmdata.RefAsmSymbol(name,_symtyp),ofs);
  1521. consttype:=_typ;
  1522. end;
  1523. constructor tai_const.Create_nil_codeptr;
  1524. begin
  1525. self.Create_int_codeptr(0);
  1526. end;
  1527. constructor tai_const.Create_nil_dataptr;
  1528. begin
  1529. self.Create_int_dataptr(0);
  1530. end;
  1531. constructor tai_const.Create_int_codeptr(_value: int64);
  1532. begin
  1533. inherited Create;
  1534. typ:=ait_const;
  1535. {$ifdef i8086}
  1536. if current_settings.x86memorymodel in x86_far_code_models then
  1537. consttype:=aitconst_farptr
  1538. else
  1539. {$endif i8086}
  1540. consttype:=aitconst_ptr;
  1541. sym:=nil;
  1542. endsym:=nil;
  1543. symofs:=0;
  1544. value:=_value;
  1545. end;
  1546. constructor tai_const.Create_int_dataptr(_value: int64);
  1547. begin
  1548. inherited Create;
  1549. typ:=ait_const;
  1550. {$ifdef i8086}
  1551. if current_settings.x86memorymodel in x86_far_data_models then
  1552. consttype:=aitconst_farptr
  1553. else
  1554. {$endif i8086}
  1555. consttype:=aitconst_ptr;
  1556. sym:=nil;
  1557. endsym:=nil;
  1558. symofs:=0;
  1559. value:=_value;
  1560. end;
  1561. constructor tai_const.ppuload(t:taitype;ppufile:tcompilerppufile);
  1562. begin
  1563. inherited ppuload(t,ppufile);
  1564. consttype:=taiconst_type(ppufile.getbyte);
  1565. sym:=ppufile.getasmsymbol;
  1566. endsym:=ppufile.getasmsymbol;
  1567. value:=ppufile.getint64;
  1568. end;
  1569. procedure tai_const.ppuwrite(ppufile:tcompilerppufile);
  1570. begin
  1571. inherited ppuwrite(ppufile);
  1572. ppufile.putbyte(byte(consttype));
  1573. ppufile.putasmsymbol(sym);
  1574. ppufile.putasmsymbol(endsym);
  1575. ppufile.putint64(value);
  1576. end;
  1577. procedure tai_const.derefimpl;
  1578. begin
  1579. end;
  1580. function tai_const.getcopy:tlinkedlistitem;
  1581. begin
  1582. getcopy:=inherited getcopy;
  1583. { we need to increase the reference number }
  1584. if assigned(sym) then
  1585. sym.increfs;
  1586. if assigned(endsym) then
  1587. endsym.increfs;
  1588. end;
  1589. function tai_const.size:longint;
  1590. begin
  1591. case consttype of
  1592. aitconst_8bit :
  1593. result:=1;
  1594. aitconst_16bit,aitconst_16bit_unaligned :
  1595. result:=2;
  1596. aitconst_32bit,aitconst_darwin_dwarf_delta32,
  1597. aitconst_32bit_unaligned:
  1598. result:=4;
  1599. aitconst_64bit,aitconst_darwin_dwarf_delta64,
  1600. aitconst_64bit_unaligned:
  1601. result:=8;
  1602. aitconst_secrel32_symbol,
  1603. aitconst_rva_symbol :
  1604. if target_info.system=system_x86_64_win64 then
  1605. result:=sizeof(longint)
  1606. else
  1607. result:=sizeof(pint);
  1608. aitconst_uleb128bit :
  1609. result:=LengthUleb128(qword(value));
  1610. aitconst_sleb128bit :
  1611. result:=LengthSleb128(value);
  1612. aitconst_half16bit:
  1613. result:=2;
  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. {$ifdef llvm}
  2298. top_para:
  2299. paras.free;
  2300. top_tai:
  2301. ai.free;
  2302. {$endif llvm}
  2303. end;
  2304. typ:=top_none;
  2305. end;
  2306. end;
  2307. procedure tai_cpu_abstract.freeop(opidx:longint);
  2308. begin
  2309. clearop(opidx);
  2310. dispose(oper[opidx]);
  2311. end;
  2312. { ---------------------------------------------------------------------
  2313. Miscellaneous methods.
  2314. ---------------------------------------------------------------------}
  2315. procedure tai_cpu_abstract.SetCondition(const c:TAsmCond);
  2316. begin
  2317. condition:=c;
  2318. end;
  2319. Function tai_cpu_abstract.getcopy:TLinkedListItem;
  2320. var
  2321. i : longint;
  2322. p : tai_cpu_abstract;
  2323. begin
  2324. p:=tai_cpu_abstract(inherited getcopy);
  2325. { make a copy of the references }
  2326. p.opercnt:=0;
  2327. p.allocate_oper(ops);
  2328. for i:=0 to ops-1 do
  2329. begin
  2330. p.oper[i]^:=oper[i]^;
  2331. case oper[i]^.typ of
  2332. top_local :
  2333. begin
  2334. new(p.oper[i]^.localoper);
  2335. p.oper[i]^.localoper^:=oper[i]^.localoper^;
  2336. end;
  2337. top_ref :
  2338. begin
  2339. new(p.oper[i]^.ref);
  2340. p.oper[i]^.ref^:=oper[i]^.ref^;
  2341. end;
  2342. {$ifdef ARM}
  2343. top_shifterop:
  2344. begin
  2345. new(p.oper[i]^.shifterop);
  2346. p.oper[i]^.shifterop^:=oper[i]^.shifterop^;
  2347. end;
  2348. {$endif ARM}
  2349. end;
  2350. end;
  2351. getcopy:=p;
  2352. end;
  2353. function tai_cpu_abstract.is_same_reg_move(regtype: Tregistertype):boolean;
  2354. begin
  2355. { When the generic RA is used this needs to be overridden, we don't use
  2356. virtual;abstract; to prevent a lot of warnings of unimplemented abstract methods
  2357. when tai_cpu is created (PFV) }
  2358. internalerror(2004040901);
  2359. result:=false;
  2360. end;
  2361. function tai_cpu_abstract.spilling_get_operation_type(opnr: longint): topertype;
  2362. begin
  2363. internalerror(2004040902);
  2364. result:=operand_readwrite;
  2365. end;
  2366. function tai_cpu_abstract.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  2367. begin
  2368. result := operand_read;
  2369. end;
  2370. constructor tai_cpu_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  2371. var
  2372. i : integer;
  2373. begin
  2374. inherited ppuload(t,ppufile);
  2375. { hopefully, we don't get problems with big/litte endian here when cross compiling :/ }
  2376. ppufile.getdata(condition,sizeof(tasmcond));
  2377. allocate_oper(ppufile.getbyte);
  2378. for i:=0 to ops-1 do
  2379. ppuloadoper(ppufile,oper[i]^);
  2380. opcode:=tasmop(ppufile.getword);
  2381. {$ifdef x86}
  2382. ppufile.getdata(segprefix,sizeof(Tregister));
  2383. {$endif x86}
  2384. is_jmp:=boolean(ppufile.getbyte);
  2385. end;
  2386. procedure tai_cpu_abstract.ppuwrite(ppufile:tcompilerppufile);
  2387. var
  2388. i : integer;
  2389. begin
  2390. inherited ppuwrite(ppufile);
  2391. ppufile.putdata(condition,sizeof(tasmcond));
  2392. ppufile.putbyte(ops);
  2393. for i:=0 to ops-1 do
  2394. ppuwriteoper(ppufile,oper[i]^);
  2395. ppufile.putword(word(opcode));
  2396. {$ifdef x86}
  2397. ppufile.putdata(segprefix,sizeof(Tregister));
  2398. {$endif x86}
  2399. ppufile.putbyte(byte(is_jmp));
  2400. end;
  2401. procedure tai_cpu_abstract.buildderefimpl;
  2402. var
  2403. i : integer;
  2404. begin
  2405. for i:=0 to ops-1 do
  2406. ppubuildderefimploper(oper[i]^);
  2407. end;
  2408. procedure tai_cpu_abstract.derefimpl;
  2409. var
  2410. i : integer;
  2411. begin
  2412. for i:=0 to ops-1 do
  2413. ppuderefoper(oper[i]^);
  2414. end;
  2415. procedure tai_cpu_abstract.resetpass1;
  2416. begin
  2417. end;
  2418. procedure tai_cpu_abstract.resetpass2;
  2419. begin
  2420. end;
  2421. function tai_cpu_abstract.Pass1(objdata:TObjData):longint;
  2422. begin
  2423. result:=0;
  2424. end;
  2425. procedure tai_cpu_abstract.Pass2(objdata:TObjData);
  2426. begin
  2427. end;
  2428. procedure tai_cpu_abstract.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  2429. begin
  2430. o.typ:=toptype(ppufile.getbyte);
  2431. o.ot:=ppufile.getlongint;
  2432. case o.typ of
  2433. top_reg :
  2434. ppufile.getdata(o.reg,sizeof(Tregister));
  2435. top_ref :
  2436. begin
  2437. new(o.ref);
  2438. {$ifdef x86}
  2439. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  2440. {$endif x86}
  2441. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  2442. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  2443. ppufile.getdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  2444. o.ref^.scalefactor:=ppufile.getbyte;
  2445. o.ref^.offset:=ppufile.getaint;
  2446. o.ref^.symbol:=ppufile.getasmsymbol;
  2447. o.ref^.relsymbol:=ppufile.getasmsymbol;
  2448. end;
  2449. top_const :
  2450. o.val:=ppufile.getaint;
  2451. top_local :
  2452. begin
  2453. new(o.localoper);
  2454. with o.localoper^ do
  2455. begin
  2456. ppufile.getderef(localsymderef);
  2457. localsymofs:=ppufile.getaint;
  2458. localindexreg:=tregister(ppufile.getlongint);
  2459. localscale:=ppufile.getbyte;
  2460. localgetoffset:=(ppufile.getbyte<>0);
  2461. end;
  2462. end;
  2463. else
  2464. internalerror(2007010210);
  2465. end;
  2466. end;
  2467. procedure tai_cpu_abstract.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  2468. begin
  2469. ppufile.putbyte(byte(o.typ));
  2470. ppufile.putlongint(o.ot);
  2471. case o.typ of
  2472. top_reg :
  2473. ppufile.putdata(o.reg,sizeof(Tregister));
  2474. top_ref :
  2475. begin
  2476. {$ifdef x86}
  2477. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  2478. {$endif x86}
  2479. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  2480. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  2481. ppufile.putdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  2482. ppufile.putbyte(o.ref^.scalefactor);
  2483. ppufile.putaint(o.ref^.offset);
  2484. ppufile.putasmsymbol(o.ref^.symbol);
  2485. ppufile.putasmsymbol(o.ref^.relsymbol);
  2486. end;
  2487. top_const :
  2488. ppufile.putaint(o.val);
  2489. top_local :
  2490. begin
  2491. with o.localoper^ do
  2492. begin
  2493. ppufile.putderef(localsymderef);
  2494. ppufile.putaint(localsymofs);
  2495. ppufile.putlongint(longint(localindexreg));
  2496. ppufile.putbyte(localscale);
  2497. ppufile.putbyte(byte(localgetoffset));
  2498. end;
  2499. end;
  2500. else
  2501. internalerror(2007010211);
  2502. end;
  2503. end;
  2504. {****************************************************************************
  2505. tai_align_abstract
  2506. ****************************************************************************}
  2507. constructor tai_align_abstract.Create(b: byte);
  2508. begin
  2509. inherited Create;
  2510. typ:=ait_align;
  2511. {$ifdef EXTDEBUG}
  2512. if upper(classname)='TAI_ALIGN_ABSTRACT' then
  2513. internalerror(200709191);
  2514. {$endif EXTDEBUG}
  2515. if b in [1,2,4,8,16,32] then
  2516. aligntype := b
  2517. else
  2518. aligntype := 1;
  2519. fillsize:=0;
  2520. fillop:=0;
  2521. use_op:=false;
  2522. end;
  2523. constructor tai_align_abstract.Create_op(b: byte; _op: byte);
  2524. begin
  2525. inherited Create;
  2526. typ:=ait_align;
  2527. if b in [1,2,4,8,16,32] then
  2528. aligntype := b
  2529. else
  2530. aligntype := 1;
  2531. fillsize:=0;
  2532. fillop:=_op;
  2533. use_op:=true;
  2534. end;
  2535. constructor tai_align_abstract.Create_zeros(b: byte);
  2536. begin
  2537. inherited Create;
  2538. typ:=ait_align;
  2539. if b in [1,2,4,8,16,32] then
  2540. aligntype := b
  2541. else
  2542. aligntype := 1;
  2543. use_op:=true;
  2544. fillsize:=0;
  2545. fillop:=0;
  2546. end;
  2547. function tai_align_abstract.calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;
  2548. begin
  2549. if fillsize>sizeof(buf) then
  2550. internalerror(200404293);
  2551. fillchar(buf,high(buf),fillop);
  2552. calculatefillbuf:=pchar(@buf);
  2553. end;
  2554. constructor tai_align_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  2555. begin
  2556. inherited ppuload(t,ppufile);
  2557. aligntype:=ppufile.getbyte;
  2558. fillsize:=0;
  2559. fillop:=ppufile.getbyte;
  2560. use_op:=boolean(ppufile.getbyte);
  2561. end;
  2562. procedure tai_align_abstract.ppuwrite(ppufile:tcompilerppufile);
  2563. begin
  2564. inherited ppuwrite(ppufile);
  2565. ppufile.putbyte(aligntype);
  2566. ppufile.putbyte(fillop);
  2567. ppufile.putbyte(byte(use_op));
  2568. end;
  2569. {****************************************************************************
  2570. tai_seh_directive
  2571. ****************************************************************************}
  2572. const
  2573. datatypemap: array[TAsmSehDirective] of TSehDirectiveDatatype=(
  2574. sd_string, { proc }
  2575. sd_none, { endproc }
  2576. sd_none, { endprologue }
  2577. sd_string, { handler }
  2578. sd_none, { handlerdata }
  2579. sd_none,sd_none,sd_none, { eh, 32, no32 }
  2580. sd_regoffset, { setframe }
  2581. sd_offset, { stackalloc }
  2582. sd_reg, { pushreg }
  2583. sd_regoffset, { savereg }
  2584. sd_regoffset, { savexmm }
  2585. sd_none { pushframe }
  2586. );
  2587. constructor tai_seh_directive.create(_kind:TAsmSehDirective);
  2588. begin
  2589. inherited Create;
  2590. typ:=ait_seh_directive;
  2591. kind:=_kind;
  2592. data.typ:=datatypemap[_kind];
  2593. end;
  2594. constructor tai_seh_directive.create_name(_kind:TAsmSehDirective;const _name:string);
  2595. begin
  2596. create(_kind);
  2597. data.name:=stringdup(_name);
  2598. end;
  2599. constructor tai_seh_directive.create_reg(_kind:TAsmSehDirective;r:TRegister);
  2600. begin
  2601. create(_kind);
  2602. data.reg:=r;
  2603. end;
  2604. constructor tai_seh_directive.create_offset(_kind:TAsmSehDirective;ofs:dword);
  2605. begin
  2606. create(_kind);
  2607. data.offset:=ofs;
  2608. end;
  2609. constructor tai_seh_directive.create_reg_offset(_kind:TAsmSehDirective;
  2610. r:TRegister;ofs:dword);
  2611. begin
  2612. create(_kind);
  2613. data.offset:=ofs;
  2614. data.reg:=r;
  2615. end;
  2616. constructor tai_seh_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  2617. begin
  2618. inherited ppuload(t, ppufile);
  2619. kind:=TAsmSehDirective(ppufile.getbyte);
  2620. data.typ:=datatypemap[kind];
  2621. case data.typ of
  2622. sd_none: ;
  2623. sd_string:
  2624. begin
  2625. data.name:=stringdup(ppufile.getstring);
  2626. data.flags:=ppufile.getbyte;
  2627. end;
  2628. sd_reg,sd_offset,sd_regoffset:
  2629. begin
  2630. ppufile.getdata(data.reg,sizeof(TRegister));
  2631. data.offset:=ppufile.getdword;
  2632. end;
  2633. else
  2634. InternalError(2011091201);
  2635. end;
  2636. end;
  2637. destructor tai_seh_directive.destroy;
  2638. begin
  2639. if data.typ=sd_string then
  2640. stringdispose(data.name);
  2641. inherited destroy;
  2642. end;
  2643. procedure tai_seh_directive.ppuwrite(ppufile:tcompilerppufile);
  2644. begin
  2645. inherited ppuwrite(ppufile);
  2646. ppufile.putbyte(ord(kind));
  2647. case data.typ of
  2648. sd_none: ;
  2649. sd_string:
  2650. begin
  2651. ppufile.putstring(data.name^);
  2652. ppufile.putbyte(data.flags);
  2653. end;
  2654. sd_reg,sd_offset,sd_regoffset:
  2655. begin
  2656. ppufile.putdata(data.reg,sizeof(TRegister));
  2657. ppufile.putdword(data.offset);
  2658. end;
  2659. else
  2660. InternalError(2011091202);
  2661. end;
  2662. end;
  2663. procedure tai_seh_directive.generate_code(objdata:TObjData);
  2664. begin
  2665. end;
  2666. {$ifdef JVM}
  2667. {****************************************************************************
  2668. tai_jvar
  2669. ****************************************************************************}
  2670. constructor tai_jvar.Create(_stackslot: longint; const _desc: shortstring; _startlab, _stoplab: TAsmSymbol);
  2671. begin
  2672. Inherited create;
  2673. typ:=ait_jvar;
  2674. stackslot:=_stackslot;
  2675. desc:=stringdup(_desc);
  2676. startlab:=_startlab;
  2677. stoplab:=_stoplab;
  2678. end;
  2679. constructor tai_jvar.ppuload(t: taitype; ppufile: tcompilerppufile);
  2680. begin
  2681. inherited ppuload(t, ppufile);
  2682. stackslot:=ppufile.getlongint;
  2683. desc:=stringdup(ppufile.getstring);
  2684. startlab:=ppufile.getasmsymbol;
  2685. stoplab:=ppufile.getasmsymbol;
  2686. end;
  2687. procedure tai_jvar.ppuwrite(ppufile: tcompilerppufile);
  2688. begin
  2689. inherited ppuwrite(ppufile);
  2690. ppufile.putlongint(stackslot);
  2691. ppufile.putstring(desc^);
  2692. ppufile.putasmsymbol(startlab);
  2693. ppufile.putasmsymbol(stoplab);
  2694. end;
  2695. destructor tai_jvar.destroy;
  2696. begin
  2697. stringdispose(desc);
  2698. inherited destroy;
  2699. end;
  2700. {****************************************************************************
  2701. tai_jcatch
  2702. ****************************************************************************}
  2703. constructor tai_jcatch.Create(const _name: shortstring; _startlab, _stoplab, _handlerlab: TAsmSymbol);
  2704. begin
  2705. Inherited create;
  2706. typ:=ait_jcatch;
  2707. name:=stringdup(_name);
  2708. startlab:=_startlab;
  2709. startlab.increfs;
  2710. stoplab:=_stoplab;
  2711. stoplab.increfs;
  2712. handlerlab:=_handlerlab;
  2713. handlerlab.increfs;
  2714. end;
  2715. destructor tai_jcatch.destroy;
  2716. begin
  2717. stringdispose(name);
  2718. inherited destroy;
  2719. end;
  2720. constructor tai_jcatch.ppuload(t: taitype; ppufile: tcompilerppufile);
  2721. begin
  2722. inherited ppuload(t, ppufile);
  2723. name:=stringdup(ppufile.getstring);
  2724. startlab:=ppufile.getasmsymbol;
  2725. startlab.increfs;
  2726. stoplab:=ppufile.getasmsymbol;
  2727. stoplab.increfs;
  2728. handlerlab:=ppufile.getasmsymbol;
  2729. handlerlab.increfs;
  2730. end;
  2731. procedure tai_jcatch.ppuwrite(ppufile: tcompilerppufile);
  2732. begin
  2733. inherited ppuwrite(ppufile);
  2734. ppufile.putstring(name^);
  2735. ppufile.putasmsymbol(startlab);
  2736. ppufile.putasmsymbol(stoplab);
  2737. ppufile.putasmsymbol(handlerlab);
  2738. end;
  2739. {$endif JVM}
  2740. begin
  2741. {$push}{$warnings off}
  2742. { taitype should fit into a 4 byte set for speed reasons }
  2743. if ord(high(taitype))>31 then
  2744. internalerror(201108181);
  2745. {$pop}
  2746. end.