aasmtai.pas 95 KB

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