aasmtai.pas 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273
  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:tcgint);
  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. inc(list.section_count);
  870. list.concat(cai_align.create(Aalign));
  871. end;
  872. function ppuloadai(ppufile:tcompilerppufile):tai;
  873. var
  874. b : byte;
  875. t : taitype;
  876. begin
  877. { marker }
  878. b:=ppufile.getbyte;
  879. if b<>pputaimarker then
  880. internalerror(200208181);
  881. { load nodetype }
  882. t:=taitype(ppufile.getbyte);
  883. if t<>ait_none then
  884. begin
  885. if t>high(taitype) then
  886. internalerror(200208182);
  887. if not assigned(aiclass[t]) then
  888. internalerror(200208183);
  889. {writeln('taiload: ',taitypestr[t]);}
  890. { generate tai of the correct class }
  891. ppuloadai:=aiclass[t].ppuload(t,ppufile);
  892. end
  893. else
  894. ppuloadai:=nil;
  895. end;
  896. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  897. begin
  898. { marker, read by ppuloadnode }
  899. ppufile.putbyte(pputaimarker);
  900. if assigned(n) then
  901. begin
  902. { type, read by ppuloadnode }
  903. ppufile.putbyte(byte(n.typ));
  904. {writeln('taiwrite: ',taitypestr[n.typ]);}
  905. n.ppuwrite(ppufile);
  906. end
  907. else
  908. ppufile.putbyte(byte(ait_none));
  909. end;
  910. constructor tai_symbolpair.create(akind: TSymbolPairKind; const asym, avalue: string);
  911. begin
  912. inherited create;
  913. kind:=akind;
  914. typ:=ait_symbolpair;
  915. sym:=stringdup(asym);
  916. value:=stringdup(avalue);
  917. end;
  918. destructor tai_symbolpair.destroy;
  919. begin
  920. stringdispose(sym);
  921. stringdispose(value);
  922. inherited destroy;
  923. end;
  924. constructor tai_symbolpair.ppuload(t: taitype; ppufile: tcompilerppufile);
  925. begin
  926. inherited ppuload(t,ppufile);
  927. kind:=TSymbolPairKind(ppufile.getbyte);;
  928. sym:=ppufile.getpshortstring;
  929. value:=ppufile.getpshortstring;
  930. end;
  931. procedure tai_symbolpair.ppuwrite(ppufile: tcompilerppufile);
  932. begin
  933. inherited ppuwrite(ppufile);
  934. ppufile.putbyte(byte(kind));
  935. ppufile.putstring(sym^);
  936. ppufile.putstring(value^);
  937. end;
  938. constructor tai_varloc.create(sym: tsym; loc: tregister);
  939. begin
  940. inherited Create;
  941. typ:=ait_varloc;
  942. newlocation:=loc;
  943. newlocationhi:=NR_NO;
  944. varsym:=sym;
  945. end;
  946. constructor tai_varloc.create64(sym: tsym; loc, lochi: tregister);
  947. begin
  948. inherited Create;
  949. typ:=ait_varloc;
  950. newlocation:=loc;
  951. newlocationhi:=lochi;
  952. varsym:=sym;
  953. end;
  954. {$ifdef cpu64bitalu}
  955. constructor tai_varloc.create128(sym: tsym; loc, lochi: tregister);
  956. begin
  957. inherited Create;
  958. typ:=ait_varloc;
  959. newlocation:=loc;
  960. newlocationhi:=lochi;
  961. varsym:=sym;
  962. end;
  963. {$endif cpu64bitalu}
  964. constructor tai_varloc.ppuload(t: taitype; ppufile: tcompilerppufile);
  965. begin
  966. inherited ppuload(t, ppufile);
  967. end;
  968. procedure tai_varloc.ppuwrite(ppufile: tcompilerppufile);
  969. begin
  970. inherited ppuwrite(ppufile);
  971. end;
  972. procedure tai_varloc.buildderefimpl;
  973. begin
  974. inherited buildderefimpl;
  975. end;
  976. procedure tai_varloc.derefimpl;
  977. begin
  978. inherited derefimpl;
  979. end;
  980. {****************************************************************************
  981. TAI
  982. ****************************************************************************}
  983. constructor tai.Create;
  984. begin
  985. {$ifndef NOOPT}
  986. optinfo:=nil;
  987. {$endif NOOPT}
  988. end;
  989. constructor tai.ppuload(t:taitype;ppufile:tcompilerppufile);
  990. begin
  991. typ:=t;
  992. {$ifndef NOOPT}
  993. optinfo:=nil;
  994. {$endif}
  995. end;
  996. procedure tai.ppuwrite(ppufile:tcompilerppufile);
  997. begin
  998. end;
  999. procedure tai.buildderefimpl;
  1000. begin
  1001. end;
  1002. procedure tai.derefimpl;
  1003. begin
  1004. end;
  1005. {****************************************************************************
  1006. TAILINEINFO
  1007. ****************************************************************************}
  1008. constructor tailineinfo.create;
  1009. begin
  1010. inherited create;
  1011. fileinfo:=current_filepos;
  1012. end;
  1013. constructor tailineinfo.ppuload(t:taitype;ppufile:tcompilerppufile);
  1014. begin
  1015. inherited ppuload(t,ppufile);
  1016. ppufile.getposinfo(fileinfo);
  1017. end;
  1018. procedure tailineinfo.ppuwrite(ppufile:tcompilerppufile);
  1019. begin
  1020. inherited ppuwrite(ppufile);
  1021. ppufile.putposinfo(fileinfo);
  1022. end;
  1023. {****************************************************************************
  1024. TAI_SIMPLE
  1025. ****************************************************************************}
  1026. constructor tai_simple.create(_typ : taitype);
  1027. begin
  1028. inherited create;
  1029. typ:=_typ;
  1030. end;
  1031. {****************************************************************************
  1032. TAI_SECTION
  1033. ****************************************************************************}
  1034. constructor tai_section.Create(Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  1035. begin
  1036. inherited Create;
  1037. typ:=ait_section;
  1038. sectype:=asectype;
  1039. secalign:=Aalign;
  1040. secorder:=Asecorder;
  1041. name:=stringdup(Aname);
  1042. sec:=nil;
  1043. end;
  1044. constructor tai_section.ppuload(t:taitype;ppufile:tcompilerppufile);
  1045. begin
  1046. inherited ppuload(t,ppufile);
  1047. sectype:=TAsmSectiontype(ppufile.getbyte);
  1048. secalign:=ppufile.getbyte;
  1049. name:=ppufile.getpshortstring;
  1050. sec:=nil;
  1051. end;
  1052. destructor tai_section.Destroy;
  1053. begin
  1054. stringdispose(name);
  1055. end;
  1056. procedure tai_section.ppuwrite(ppufile:tcompilerppufile);
  1057. begin
  1058. inherited ppuwrite(ppufile);
  1059. ppufile.putbyte(byte(sectype));
  1060. ppufile.putbyte(secalign);
  1061. ppufile.putstring(name^);
  1062. end;
  1063. {****************************************************************************
  1064. TAI_DATABLOCK
  1065. ****************************************************************************}
  1066. constructor tai_datablock.Create(const _name : string;_size : asizeint; def: tdef);
  1067. begin
  1068. inherited Create;
  1069. typ:=ait_datablock;
  1070. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,AT_DATA,def);
  1071. { keep things aligned }
  1072. if _size<=0 then
  1073. _size:=sizeof(aint);
  1074. size:=_size;
  1075. is_global:=false;
  1076. end;
  1077. constructor tai_datablock.Create_global(const _name : string;_size : asizeint; def: tdef);
  1078. begin
  1079. inherited Create;
  1080. typ:=ait_datablock;
  1081. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,AT_DATA,def);
  1082. { keep things aligned }
  1083. if _size<=0 then
  1084. _size:=sizeof(aint);
  1085. size:=_size;
  1086. is_global:=true;
  1087. end;
  1088. constructor tai_datablock.ppuload(t:taitype;ppufile:tcompilerppufile);
  1089. begin
  1090. inherited Create;
  1091. sym:=ppufile.getasmsymbol;
  1092. size:=ppufile.getaint;
  1093. is_global:=boolean(ppufile.getbyte);
  1094. end;
  1095. procedure tai_datablock.ppuwrite(ppufile:tcompilerppufile);
  1096. begin
  1097. inherited ppuwrite(ppufile);
  1098. ppufile.putasmsymbol(sym);
  1099. ppufile.putaint(size);
  1100. ppufile.putbyte(byte(is_global));
  1101. end;
  1102. procedure tai_datablock.derefimpl;
  1103. begin
  1104. end;
  1105. {****************************************************************************
  1106. TAI_SYMBOL
  1107. ****************************************************************************}
  1108. constructor tai_symbol.Create(_sym:tasmsymbol;siz:longint);
  1109. begin
  1110. inherited Create;
  1111. typ:=ait_symbol;
  1112. sym:=_sym;
  1113. size:=siz;
  1114. { don't redefine global/external symbols as local, as code to access
  1115. such symbols is different on some platforms }
  1116. if not(sym.bind in [AB_NONE,AB_LOCAL]) then
  1117. internalerror(2013081601);
  1118. sym.bind:=AB_LOCAL;
  1119. is_global:=false;
  1120. end;
  1121. constructor tai_symbol.Create_global(_sym:tasmsymbol;siz:longint);
  1122. begin
  1123. inherited Create;
  1124. typ:=ait_symbol;
  1125. sym:=_sym;
  1126. size:=siz;
  1127. { don't override PRIVATE_EXTERN with GLOBAL }
  1128. if not(sym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN]) then
  1129. sym.bind:=AB_GLOBAL;
  1130. is_global:=true;
  1131. end;
  1132. constructor tai_symbol.Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint;def:tdef);
  1133. begin
  1134. inherited Create;
  1135. typ:=ait_symbol;
  1136. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,_symtyp,def);
  1137. size:=siz;
  1138. is_global:=false;
  1139. end;
  1140. constructor tai_symbol.Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint;def:tdef);
  1141. begin
  1142. inherited Create;
  1143. typ:=ait_symbol;
  1144. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,_symtyp,def);
  1145. size:=siz;
  1146. is_global:=true;
  1147. end;
  1148. constructor tai_symbol.createname_global_value(const _name: string;_symtyp: tasmsymtype; siz: longint; val: ptruint;def:tdef);
  1149. begin
  1150. Createname_global(_name,_symtyp,siz,def);
  1151. value:=val;
  1152. has_value:=true;
  1153. end;
  1154. constructor tai_symbol.ppuload(t:taitype;ppufile:tcompilerppufile);
  1155. begin
  1156. inherited ppuload(t,ppufile);
  1157. sym:=ppufile.getasmsymbol;
  1158. size:=ppufile.getlongint;
  1159. is_global:=boolean(ppufile.getbyte);
  1160. end;
  1161. procedure tai_symbol.ppuwrite(ppufile:tcompilerppufile);
  1162. begin
  1163. inherited ppuwrite(ppufile);
  1164. ppufile.putasmsymbol(sym);
  1165. ppufile.putlongint(size);
  1166. ppufile.putbyte(byte(is_global));
  1167. end;
  1168. procedure tai_symbol.derefimpl;
  1169. begin
  1170. end;
  1171. {****************************************************************************
  1172. TAI_SYMBOL_END
  1173. ****************************************************************************}
  1174. constructor tai_symbol_end.Create(_sym:tasmsymbol);
  1175. begin
  1176. inherited Create;
  1177. typ:=ait_symbol_end;
  1178. sym:=_sym;
  1179. end;
  1180. constructor tai_symbol_end.Createname(const _name : string);
  1181. begin
  1182. inherited Create;
  1183. typ:=ait_symbol_end;
  1184. sym:=current_asmdata.GetAsmSymbol(_name);
  1185. if not assigned(sym) then
  1186. internalerror(2013080301);
  1187. end;
  1188. constructor tai_symbol_end.ppuload(t:taitype;ppufile:tcompilerppufile);
  1189. begin
  1190. inherited ppuload(t,ppufile);
  1191. sym:=ppufile.getasmsymbol;
  1192. end;
  1193. procedure tai_symbol_end.ppuwrite(ppufile:tcompilerppufile);
  1194. begin
  1195. inherited ppuwrite(ppufile);
  1196. ppufile.putasmsymbol(sym);
  1197. end;
  1198. procedure tai_symbol_end.derefimpl;
  1199. begin
  1200. end;
  1201. {****************************************************************************
  1202. TAI_SYMBOL_END
  1203. ****************************************************************************}
  1204. constructor tai_directive.Create(_directive:TAsmDirective;const _name:ansistring);
  1205. begin
  1206. inherited Create;
  1207. typ:=ait_directive;
  1208. name:=_name;
  1209. directive:=_directive;
  1210. end;
  1211. constructor tai_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  1212. begin
  1213. inherited ppuload(t,ppufile);
  1214. name:=ppufile.getansistring;
  1215. directive:=TAsmDirective(ppufile.getbyte);
  1216. end;
  1217. procedure tai_directive.ppuwrite(ppufile:tcompilerppufile);
  1218. begin
  1219. inherited ppuwrite(ppufile);
  1220. ppufile.putansistring(name);
  1221. ppufile.putbyte(byte(directive));
  1222. end;
  1223. {****************************************************************************
  1224. TAI_CONST
  1225. ****************************************************************************}
  1226. constructor tai_const.Create(_typ:taiconst_type;_value : int64);
  1227. begin
  1228. inherited Create;
  1229. typ:=ait_const;
  1230. consttype:=_typ;
  1231. value:=_value;
  1232. sym:=nil;
  1233. endsym:=nil;
  1234. end;
  1235. constructor tai_const.Create_128bit(_value : int64);
  1236. begin
  1237. inherited Create;
  1238. typ:=ait_const;
  1239. consttype:=aitconst_128bit;
  1240. value:=_value;
  1241. sym:=nil;
  1242. endsym:=nil;
  1243. end;
  1244. constructor tai_const.Create_64bit(_value : int64);
  1245. begin
  1246. inherited Create;
  1247. typ:=ait_const;
  1248. consttype:=aitconst_64bit;
  1249. value:=_value;
  1250. sym:=nil;
  1251. endsym:=nil;
  1252. end;
  1253. constructor tai_const.Create_32bit(_value : longint);
  1254. begin
  1255. inherited Create;
  1256. typ:=ait_const;
  1257. consttype:=aitconst_32bit;
  1258. value:=_value;
  1259. sym:=nil;
  1260. endsym:=nil;
  1261. end;
  1262. constructor tai_const.Create_16bit(_value : word);
  1263. begin
  1264. inherited Create;
  1265. typ:=ait_const;
  1266. consttype:=aitconst_16bit;
  1267. value:=_value;
  1268. sym:=nil;
  1269. endsym:=nil;
  1270. end;
  1271. constructor tai_const.Create_64bit_unaligned(_value : int64);
  1272. begin
  1273. inherited Create;
  1274. typ:=ait_const;
  1275. consttype:=aitconst_64bit_unaligned;
  1276. value:=_value;
  1277. sym:=nil;
  1278. endsym:=nil;
  1279. end;
  1280. constructor tai_const.Create_32bit_unaligned(_value : longint);
  1281. begin
  1282. inherited Create;
  1283. typ:=ait_const;
  1284. consttype:=aitconst_32bit_unaligned;
  1285. value:=_value;
  1286. sym:=nil;
  1287. endsym:=nil;
  1288. end;
  1289. constructor tai_const.Create_16bit_unaligned(_value : word);
  1290. begin
  1291. inherited Create;
  1292. typ:=ait_const;
  1293. consttype:=aitconst_16bit_unaligned;
  1294. value:=_value;
  1295. sym:=nil;
  1296. endsym:=nil;
  1297. end;
  1298. constructor tai_const.Create_8bit(_value : byte);
  1299. begin
  1300. inherited Create;
  1301. typ:=ait_const;
  1302. consttype:=aitconst_8bit;
  1303. value:=_value;
  1304. sym:=nil;
  1305. endsym:=nil;
  1306. end;
  1307. constructor tai_const.Create_char(size: integer; _value: dword);
  1308. begin
  1309. inherited Create;
  1310. typ:=ait_const;
  1311. case size of
  1312. 1:
  1313. begin
  1314. consttype:=aitconst_8bit;
  1315. value:=byte(_value)
  1316. end;
  1317. 2:
  1318. begin
  1319. consttype:=aitconst_16bit;
  1320. value:=word(_value)
  1321. end
  1322. else
  1323. InternalError(2010030701)
  1324. end
  1325. end;
  1326. constructor tai_const.Create_sleb128bit(_value : int64);
  1327. begin
  1328. inherited Create;
  1329. typ:=ait_const;
  1330. consttype:=aitconst_sleb128bit;
  1331. value:=_value;
  1332. sym:=nil;
  1333. endsym:=nil;
  1334. end;
  1335. constructor tai_const.Create_uleb128bit(_value : qword);
  1336. begin
  1337. inherited Create;
  1338. typ:=ait_const;
  1339. consttype:=aitconst_uleb128bit;
  1340. value:=int64(_value);
  1341. sym:=nil;
  1342. endsym:=nil;
  1343. end;
  1344. constructor tai_const.Create_aint(_value : aint);
  1345. begin
  1346. inherited Create;
  1347. typ:=ait_const;
  1348. consttype:=aitconst_aint;
  1349. value:=_value;
  1350. sym:=nil;
  1351. endsym:=nil;
  1352. end;
  1353. constructor tai_const.Create_sizeint(_value : asizeint);
  1354. begin
  1355. inherited Create;
  1356. typ:=ait_const;
  1357. consttype:=aitconst_sizeint;
  1358. value:=_value;
  1359. sym:=nil;
  1360. endsym:=nil;
  1361. end;
  1362. constructor tai_const.Create_sizeint_unaligned(_value : asizeint);
  1363. begin
  1364. inherited Create;
  1365. typ:=ait_const;
  1366. consttype:=aitconst_sizeint_unaligned;
  1367. value:=_value;
  1368. sym:=nil;
  1369. endsym:=nil;
  1370. end;
  1371. constructor tai_const.Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  1372. begin
  1373. inherited Create;
  1374. typ:=ait_const;
  1375. consttype:=_typ;
  1376. sym:=_sym;
  1377. endsym:=nil;
  1378. value:=0;
  1379. { update sym info }
  1380. if assigned(sym) then
  1381. sym.increfs;
  1382. end;
  1383. constructor tai_const.Create_sym(_sym:tasmsymbol);
  1384. begin
  1385. self.create_sym_offset(_sym,0);
  1386. end;
  1387. {$ifdef i8086}
  1388. constructor tai_const.Create_sym_near(_sym: tasmsymbol);
  1389. begin
  1390. self.create_sym(_sym);
  1391. consttype:=aitconst_ptr;
  1392. end;
  1393. constructor tai_const.Create_sym_far(_sym: tasmsymbol);
  1394. begin
  1395. self.create_sym(_sym);
  1396. consttype:=aitconst_farptr;
  1397. end;
  1398. {$endif i8086}
  1399. constructor tai_const.Create_sym_offset(_sym:tasmsymbol;ofs:asizeint);
  1400. begin
  1401. inherited Create;
  1402. typ:=ait_const;
  1403. {$ifdef i8086}
  1404. if assigned(_sym) and (_sym.typ=AT_DATA) then
  1405. begin
  1406. if current_settings.x86memorymodel in x86_far_data_models then
  1407. consttype:=aitconst_farptr
  1408. else
  1409. consttype:=aitconst_ptr;
  1410. end
  1411. else
  1412. begin
  1413. if current_settings.x86memorymodel in x86_far_code_models then
  1414. consttype:=aitconst_farptr
  1415. else
  1416. consttype:=aitconst_ptr;
  1417. end;
  1418. {$else i8086}
  1419. {$ifdef avr}
  1420. if assigned(_sym) and (_sym.typ=AT_FUNCTION) then
  1421. consttype:=aitconst_gs
  1422. else
  1423. {$endif avr}
  1424. consttype:=aitconst_ptr;
  1425. {$endif i8086}
  1426. { sym is allowed to be nil, this is used to write nil pointers }
  1427. sym:=_sym;
  1428. endsym:=nil;
  1429. { store the original offset in symofs so that we can recalculate the
  1430. value field in the assembler }
  1431. symofs:=ofs;
  1432. value:=ofs;
  1433. { update sym info }
  1434. if assigned(sym) then
  1435. sym.increfs;
  1436. end;
  1437. constructor tai_const.Create_type_sym_offset(_typ : taiconst_type;_sym : tasmsymbol; ofs : asizeint);
  1438. begin
  1439. inherited Create;
  1440. typ:=ait_const;
  1441. consttype:=_typ;
  1442. { sym is allowed to be nil, this is used to write nil pointers }
  1443. sym:=_sym;
  1444. endsym:=nil;
  1445. { store the original offset in symofs so that we can recalculate the
  1446. value field in the assembler }
  1447. symofs:=ofs;
  1448. value:=ofs;
  1449. { update sym info }
  1450. if assigned(sym) then
  1451. sym.increfs;
  1452. end;
  1453. constructor tai_const.Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  1454. begin
  1455. self.create_sym_offset(_sym,0);
  1456. consttype:=_typ;
  1457. endsym:=_endsym;
  1458. endsym.increfs;
  1459. end;
  1460. constructor tai_const.Create_rel_sym_offset(_typ: taiconst_type; _sym,_endsym: tasmsymbol; _ofs: int64);
  1461. begin
  1462. self.create_sym_offset(_sym,_ofs);
  1463. consttype:=_typ;
  1464. endsym:=_endsym;
  1465. endsym.increfs;
  1466. end;
  1467. constructor tai_const.Create_rva_sym(_sym:tasmsymbol);
  1468. begin
  1469. self.create_sym_offset(_sym,0);
  1470. consttype:=aitconst_rva_symbol;
  1471. end;
  1472. constructor tai_const.Createname(const name:string;ofs:asizeint);
  1473. begin
  1474. self.Createname(name,AT_NONE,ofs);
  1475. end;
  1476. constructor tai_const.Createname(const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  1477. begin
  1478. self.create_sym_offset(current_asmdata.RefAsmSymbol(name,_symtyp),ofs);
  1479. end;
  1480. constructor tai_const.Create_type_name(_typ:taiconst_type;const name:string;ofs:asizeint);
  1481. begin
  1482. self.Create_type_name(_typ,name,AT_NONE,ofs);
  1483. end;
  1484. constructor tai_const.Create_type_name(_typ:taiconst_type;const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  1485. begin
  1486. self.create_sym_offset(current_asmdata.RefAsmSymbol(name,_symtyp),ofs);
  1487. consttype:=_typ;
  1488. end;
  1489. constructor tai_const.Create_nil_codeptr;
  1490. begin
  1491. self.Create_int_codeptr(0);
  1492. end;
  1493. constructor tai_const.Create_nil_codeptr_unaligned;
  1494. begin
  1495. self.Create_int_codeptr_unaligned(0);
  1496. end;
  1497. constructor tai_const.Create_nil_dataptr;
  1498. begin
  1499. self.Create_int_dataptr(0);
  1500. end;
  1501. constructor tai_const.Create_nil_dataptr_unaligned;
  1502. begin
  1503. self.Create_int_dataptr_unaligned(0);
  1504. end;
  1505. constructor tai_const.Create_int_codeptr(_value: int64);
  1506. begin
  1507. inherited Create;
  1508. typ:=ait_const;
  1509. {$ifdef i8086}
  1510. if current_settings.x86memorymodel in x86_far_code_models then
  1511. consttype:=aitconst_farptr
  1512. else
  1513. {$endif i8086}
  1514. {$ifdef avr}
  1515. consttype:=aitconst_gs;
  1516. {$else avr}
  1517. consttype:=aitconst_ptr;
  1518. {$endif avr}
  1519. sym:=nil;
  1520. endsym:=nil;
  1521. symofs:=0;
  1522. value:=_value;
  1523. end;
  1524. constructor tai_const.Create_int_codeptr_unaligned(_value: int64);
  1525. begin
  1526. inherited Create;
  1527. typ:=ait_const;
  1528. {$ifdef i8086}
  1529. if current_settings.x86memorymodel in x86_far_code_models then
  1530. consttype:=aitconst_farptr
  1531. else
  1532. {$endif i8086}
  1533. {$ifdef avr}
  1534. consttype:=aitconst_gs;
  1535. {$else avr}
  1536. consttype:=aitconst_ptr_unaligned;
  1537. {$endif avr}
  1538. sym:=nil;
  1539. endsym:=nil;
  1540. symofs:=0;
  1541. value:=_value;
  1542. end;
  1543. constructor tai_const.Create_int_dataptr(_value: int64);
  1544. begin
  1545. inherited Create;
  1546. typ:=ait_const;
  1547. {$ifdef i8086}
  1548. if current_settings.x86memorymodel in x86_far_data_models then
  1549. consttype:=aitconst_farptr
  1550. else
  1551. {$endif i8086}
  1552. consttype:=aitconst_ptr;
  1553. sym:=nil;
  1554. endsym:=nil;
  1555. symofs:=0;
  1556. value:=_value;
  1557. end;
  1558. constructor tai_const.Create_int_dataptr_unaligned(_value: int64);
  1559. begin
  1560. inherited Create;
  1561. typ:=ait_const;
  1562. {$ifdef i8086}
  1563. if current_settings.x86memorymodel in x86_far_data_models then
  1564. consttype:=aitconst_farptr
  1565. else
  1566. {$endif i8086}
  1567. consttype:=aitconst_ptr_unaligned;
  1568. sym:=nil;
  1569. endsym:=nil;
  1570. symofs:=0;
  1571. value:=_value;
  1572. end;
  1573. {$ifdef i8086}
  1574. constructor tai_const.Create_seg_name(const name:string);
  1575. begin
  1576. self.Createname(name,0);
  1577. self.consttype:=aitconst_seg;
  1578. end;
  1579. constructor tai_const.Create_dgroup;
  1580. begin
  1581. self.Create_16bit(0);
  1582. self.consttype:=aitconst_dgroup;
  1583. end;
  1584. constructor tai_const.Create_fardataseg;
  1585. begin
  1586. self.Create_16bit(0);
  1587. self.consttype:=aitconst_fardataseg;
  1588. end;
  1589. {$endif i8086}
  1590. constructor tai_const.ppuload(t:taitype;ppufile:tcompilerppufile);
  1591. begin
  1592. inherited ppuload(t,ppufile);
  1593. consttype:=taiconst_type(ppufile.getbyte);
  1594. sym:=ppufile.getasmsymbol;
  1595. endsym:=ppufile.getasmsymbol;
  1596. value:=ppufile.getint64;
  1597. end;
  1598. procedure tai_const.ppuwrite(ppufile:tcompilerppufile);
  1599. begin
  1600. inherited ppuwrite(ppufile);
  1601. ppufile.putbyte(byte(consttype));
  1602. ppufile.putasmsymbol(sym);
  1603. ppufile.putasmsymbol(endsym);
  1604. ppufile.putint64(value);
  1605. end;
  1606. procedure tai_const.derefimpl;
  1607. begin
  1608. end;
  1609. function tai_const.getcopy:tlinkedlistitem;
  1610. begin
  1611. getcopy:=inherited getcopy;
  1612. { we need to increase the reference number }
  1613. if assigned(sym) then
  1614. sym.increfs;
  1615. if assigned(endsym) then
  1616. endsym.increfs;
  1617. end;
  1618. function tai_const.size:longint;
  1619. begin
  1620. case consttype of
  1621. aitconst_8bit :
  1622. result:=1;
  1623. aitconst_16bit,aitconst_16bit_unaligned :
  1624. result:=2;
  1625. aitconst_32bit,aitconst_darwin_dwarf_delta32,
  1626. aitconst_32bit_unaligned:
  1627. result:=4;
  1628. aitconst_64bit,aitconst_darwin_dwarf_delta64,
  1629. aitconst_64bit_unaligned:
  1630. result:=8;
  1631. aitconst_secrel32_symbol,
  1632. aitconst_rva_symbol :
  1633. if target_info.system=system_x86_64_win64 then
  1634. result:=sizeof(longint)
  1635. else
  1636. result:=sizeof(pint);
  1637. aitconst_uleb128bit :
  1638. result:=LengthUleb128(qword(value));
  1639. aitconst_sleb128bit :
  1640. result:=LengthSleb128(value);
  1641. aitconst_half16bit,
  1642. aitconst_gs:
  1643. result:=2;
  1644. aitconst_farptr:
  1645. result:=4;
  1646. aitconst_dgroup,
  1647. aitconst_fardataseg,
  1648. aitconst_seg:
  1649. result:=2;
  1650. aitconst_got:
  1651. result:=sizeof(pint);
  1652. aitconst_gotoff_symbol:
  1653. result:=4;
  1654. else
  1655. internalerror(200603253);
  1656. end;
  1657. end;
  1658. {****************************************************************************
  1659. TAI_realconst
  1660. ****************************************************************************}
  1661. constructor tai_realconst.create_s32real(val: ts32real);
  1662. begin
  1663. inherited create;
  1664. typ:=ait_realconst;
  1665. realtyp:=aitrealconst_s32bit;
  1666. savesize:=4;
  1667. value.s32val:=val;
  1668. end;
  1669. constructor tai_realconst.create_s64real(val: ts64real);
  1670. begin
  1671. inherited create;
  1672. typ:=ait_realconst;
  1673. realtyp:=aitrealconst_s64bit;
  1674. savesize:=8;
  1675. value.s64val:=val;
  1676. end;
  1677. {$ifdef ARM}
  1678. constructor tai_realconst.create_s64real_hiloswapped(val : ts64real);
  1679. begin
  1680. inherited create;
  1681. typ:=ait_realconst;
  1682. realtyp:=aitrealconst_s64bit;
  1683. value.s64val:=val;
  1684. savesize:=8;
  1685. formatoptions:=fo_hiloswapped;
  1686. end;
  1687. {$endif ARM}
  1688. constructor tai_realconst.create_s80real(val: ts80real; _savesize: byte);
  1689. begin
  1690. inherited create;
  1691. typ:=ait_realconst;
  1692. realtyp:=aitrealconst_s80bit;
  1693. savesize:=_savesize;
  1694. value.s80val:=val;
  1695. end;
  1696. constructor tai_realconst.create_s128real(val: ts128real);
  1697. begin
  1698. inherited create;
  1699. typ:=ait_realconst;
  1700. realtyp:=aitrealconst_s128bit;
  1701. savesize:=16;
  1702. value.s128val:=val;
  1703. end;
  1704. constructor tai_realconst.create_s64compreal(val: ts64comp);
  1705. begin
  1706. inherited create;
  1707. typ:=ait_realconst;
  1708. realtyp:=aitrealconst_s64comp;
  1709. savesize:=8;
  1710. value.s64compval:=val;
  1711. end;
  1712. constructor tai_realconst.ppuload(t: taitype; ppufile: tcompilerppufile);
  1713. begin
  1714. inherited;
  1715. realtyp:=tairealconsttype(ppufile.getbyte);
  1716. {$ifdef ARM}
  1717. formatoptions:=tformatoptions(ppufile.getbyte);
  1718. {$endif ARM}
  1719. case realtyp of
  1720. aitrealconst_s32bit:
  1721. value.s32val:=ppufile.getreal;
  1722. aitrealconst_s64bit:
  1723. value.s64val:=ppufile.getreal;
  1724. aitrealconst_s80bit:
  1725. value.s80val:=ppufile.getreal;
  1726. aitrealconst_s128bit:
  1727. value.s128val:=ppufile.getreal;
  1728. aitrealconst_s64comp:
  1729. value.s64compval:=comp(ppufile.getint64);
  1730. else
  1731. internalerror(2014050602);
  1732. end;
  1733. end;
  1734. procedure tai_realconst.ppuwrite(ppufile: tcompilerppufile);
  1735. var
  1736. c: comp;
  1737. begin
  1738. inherited ppuwrite(ppufile);
  1739. ppufile.putbyte(byte(realtyp));
  1740. {$ifdef ARM}
  1741. ppufile.putbyte(byte(formatoptions));
  1742. {$endif ARM}
  1743. case realtyp of
  1744. aitrealconst_s32bit:
  1745. ppufile.putreal(value.s32val);
  1746. aitrealconst_s64bit:
  1747. ppufile.putreal(value.s64val);
  1748. aitrealconst_s80bit:
  1749. ppufile.putreal(value.s80val);
  1750. aitrealconst_s128bit:
  1751. ppufile.putreal(value.s128val);
  1752. aitrealconst_s64comp:
  1753. begin
  1754. c:=comp(value.s64compval);
  1755. ppufile.putint64(int64(c));
  1756. end
  1757. else
  1758. internalerror(2014050601);
  1759. end;
  1760. end;
  1761. function tai_realconst.getcopy: tlinkedlistitem;
  1762. begin
  1763. result:=inherited getcopy;
  1764. tai_realconst(result).value:=value;
  1765. tai_realconst(result).realtyp:=realtyp;
  1766. tai_realconst(result).savesize:=savesize;
  1767. {$ifdef ARM}
  1768. tai_realconst(result).formatoptions:=formatoptions;
  1769. {$endif ARM}
  1770. end;
  1771. function tai_realconst.datasize: word;
  1772. begin
  1773. case realtyp of
  1774. aitrealconst_s32bit:
  1775. result:=4;
  1776. aitrealconst_s64bit,
  1777. aitrealconst_s64comp:
  1778. result:=8;
  1779. aitrealconst_s80bit:
  1780. result:=10;
  1781. aitrealconst_s128bit:
  1782. result:=16;
  1783. else
  1784. internalerror(2014050603);
  1785. end;
  1786. end;
  1787. {****************************************************************************
  1788. TAI_STRING
  1789. ****************************************************************************}
  1790. constructor tai_string.Create(const _str : string);
  1791. begin
  1792. inherited Create;
  1793. typ:=ait_string;
  1794. len:=length(_str);
  1795. getmem(str,len+1);
  1796. move(_str[1],str^,len);
  1797. str[len]:=#0;
  1798. end;
  1799. constructor tai_string.Create_pchar(_str : pchar;length : longint);
  1800. begin
  1801. inherited Create;
  1802. typ:=ait_string;
  1803. str:=_str;
  1804. len:=length;
  1805. end;
  1806. destructor tai_string.destroy;
  1807. begin
  1808. if str<>nil then
  1809. freemem(str);
  1810. inherited Destroy;
  1811. end;
  1812. constructor tai_string.ppuload(t:taitype;ppufile:tcompilerppufile);
  1813. begin
  1814. inherited ppuload(t,ppufile);
  1815. len:=ppufile.getlongint;
  1816. getmem(str,len+1);
  1817. ppufile.getdata(str^,len);
  1818. str[len]:=#0
  1819. end;
  1820. procedure tai_string.ppuwrite(ppufile:tcompilerppufile);
  1821. begin
  1822. inherited ppuwrite(ppufile);
  1823. ppufile.putlongint(len);
  1824. ppufile.putdata(str^,len);
  1825. end;
  1826. function tai_string.getcopy : tlinkedlistitem;
  1827. var
  1828. p : tlinkedlistitem;
  1829. begin
  1830. p:=inherited getcopy;
  1831. getmem(tai_string(p).str,len);
  1832. move(str^,tai_string(p).str^,len);
  1833. getcopy:=p;
  1834. end;
  1835. {****************************************************************************
  1836. TAI_LABEL
  1837. ****************************************************************************}
  1838. constructor tai_label.Create(_labsym : tasmlabel);
  1839. begin
  1840. inherited Create;
  1841. typ:=ait_label;
  1842. labsym:=_labsym;
  1843. labsym.is_set:=true;
  1844. end;
  1845. constructor tai_label.ppuload(t:taitype;ppufile:tcompilerppufile);
  1846. begin
  1847. inherited ppuload(t,ppufile);
  1848. labsym:=tasmlabel(ppufile.getasmsymbol);
  1849. ppufile.getbyte; { was is_global flag, now unused }
  1850. end;
  1851. procedure tai_label.ppuwrite(ppufile:tcompilerppufile);
  1852. begin
  1853. inherited ppuwrite(ppufile);
  1854. ppufile.putasmsymbol(labsym);
  1855. ppufile.putbyte(0); { was is_global flag, now unused }
  1856. end;
  1857. procedure tai_label.derefimpl;
  1858. begin
  1859. labsym.is_set:=true;
  1860. end;
  1861. {****************************************************************************
  1862. tai_comment comment to be inserted in the assembler file
  1863. ****************************************************************************}
  1864. constructor tai_comment.Create(_str : pchar);
  1865. begin
  1866. inherited Create;
  1867. typ:=ait_comment;
  1868. str:=_str;
  1869. end;
  1870. destructor tai_comment.destroy;
  1871. begin
  1872. freemem(str);
  1873. inherited Destroy;
  1874. end;
  1875. constructor tai_comment.ppuload(t:taitype;ppufile:tcompilerppufile);
  1876. var
  1877. len : longint;
  1878. begin
  1879. inherited ppuload(t,ppufile);
  1880. len:=ppufile.getlongint;
  1881. getmem(str,len+1);
  1882. ppufile.getdata(str^,len);
  1883. str[len]:=#0;
  1884. end;
  1885. procedure tai_comment.ppuwrite(ppufile:tcompilerppufile);
  1886. var
  1887. len : longint;
  1888. begin
  1889. inherited ppuwrite(ppufile);
  1890. len:=strlen(str);
  1891. ppufile.putlongint(len);
  1892. ppufile.putdata(str^,len);
  1893. end;
  1894. function tai_comment.getcopy : tlinkedlistitem;
  1895. var
  1896. p : tlinkedlistitem;
  1897. begin
  1898. p:=inherited getcopy;
  1899. getmem(tai_comment(p).str,strlen(str)+1);
  1900. move(str^,tai_comment(p).str^,strlen(str)+1);
  1901. getcopy:=p;
  1902. end;
  1903. {****************************************************************************
  1904. TAI_STABS
  1905. ****************************************************************************}
  1906. constructor tai_stab.create(_stabtype:TStabType;_str : pchar);
  1907. begin
  1908. inherited create;
  1909. typ:=ait_stab;
  1910. str:=_str;
  1911. stabtype:=_stabtype;
  1912. end;
  1913. constructor tai_stab.create_str(_stabtype:TStabType;const s:string);
  1914. begin
  1915. self.create(_stabtype,strpnew(s));
  1916. end;
  1917. constructor tai_stab.create_ansistr(_stabtype:TStabType;const s:ansistring);
  1918. begin
  1919. inherited create;
  1920. typ:=ait_stab;
  1921. stabtype:=_stabtype;
  1922. getmem(str,length(s)+1);
  1923. if length(s)>0 then
  1924. move(s[1],str^,length(s)+1)
  1925. else
  1926. str^:=#0;
  1927. end;
  1928. destructor tai_stab.destroy;
  1929. begin
  1930. freemem(str);
  1931. inherited destroy;
  1932. end;
  1933. {****************************************************************************
  1934. TAI_FORCE_LINE
  1935. ****************************************************************************}
  1936. constructor tai_force_line.create;
  1937. begin
  1938. inherited create;
  1939. typ:=ait_force_line;
  1940. end;
  1941. {****************************************************************************
  1942. TAI_FUNCTION_NAME
  1943. ****************************************************************************}
  1944. constructor tai_function_name.create(const s:string);
  1945. begin
  1946. inherited create;
  1947. typ:=ait_function_name;
  1948. funcname:=stringdup(s);
  1949. end;
  1950. destructor tai_function_name.destroy;
  1951. begin
  1952. stringdispose(funcname);
  1953. inherited destroy;
  1954. end;
  1955. {****************************************************************************
  1956. TAI_CUTOBJECT
  1957. ****************************************************************************}
  1958. constructor tai_cutobject.Create;
  1959. begin
  1960. inherited Create;
  1961. typ:=ait_cutobject;
  1962. place:=cut_normal;
  1963. end;
  1964. constructor tai_cutobject.Create_begin;
  1965. begin
  1966. inherited Create;
  1967. typ:=ait_cutobject;
  1968. place:=cut_begin;
  1969. end;
  1970. constructor tai_cutobject.Create_end;
  1971. begin
  1972. inherited Create;
  1973. typ:=ait_cutobject;
  1974. place:=cut_end;
  1975. end;
  1976. constructor tai_cutobject.ppuload(t:taitype;ppufile:tcompilerppufile);
  1977. begin
  1978. inherited ppuload(t,ppufile);
  1979. place:=TCutPlace(ppufile.getbyte);
  1980. end;
  1981. procedure tai_cutobject.ppuwrite(ppufile:tcompilerppufile);
  1982. begin
  1983. inherited ppuwrite(ppufile);
  1984. ppufile.putbyte(byte(place));
  1985. end;
  1986. {****************************************************************************
  1987. Tai_Marker
  1988. ****************************************************************************}
  1989. constructor Tai_Marker.Create(_Kind: TAsmMarker);
  1990. begin
  1991. Inherited Create;
  1992. typ := ait_marker;
  1993. Kind := _Kind;
  1994. end;
  1995. constructor Tai_Marker.ppuload(t:taitype;ppufile:tcompilerppufile);
  1996. begin
  1997. inherited ppuload(t,ppufile);
  1998. kind:=TAsmMarker(ppufile.getbyte);
  1999. end;
  2000. procedure Tai_Marker.ppuwrite(ppufile:tcompilerppufile);
  2001. begin
  2002. inherited ppuwrite(ppufile);
  2003. ppufile.putbyte(byte(kind));
  2004. end;
  2005. {*****************************************************************************
  2006. tai_tempalloc
  2007. *****************************************************************************}
  2008. constructor tai_tempalloc.alloc(pos,size:longint);
  2009. begin
  2010. inherited Create;
  2011. typ:=ait_tempalloc;
  2012. allocation:=true;
  2013. temppos:=pos;
  2014. tempsize:=size;
  2015. {$ifdef EXTDEBUG}
  2016. problem:=nil;
  2017. {$endif EXTDEBUG}
  2018. end;
  2019. destructor tai_tempalloc.destroy;
  2020. begin
  2021. {$ifdef EXTDEBUG}
  2022. stringdispose(problem);
  2023. {$endif EXTDEBUG}
  2024. inherited destroy;
  2025. end;
  2026. constructor tai_tempalloc.dealloc(pos,size:longint);
  2027. begin
  2028. inherited Create;
  2029. typ:=ait_tempalloc;
  2030. allocation:=false;
  2031. temppos:=pos;
  2032. tempsize:=size;
  2033. {$ifdef EXTDEBUG}
  2034. problem:=nil;
  2035. {$endif EXTDEBUG}
  2036. end;
  2037. {$ifdef EXTDEBUG}
  2038. constructor tai_tempalloc.allocinfo(pos,size:longint;const st:string);
  2039. begin
  2040. inherited Create;
  2041. typ:=ait_tempalloc;
  2042. allocation:=false;
  2043. temppos:=pos;
  2044. tempsize:=size;
  2045. problem:=stringdup(st);
  2046. end;
  2047. {$endif EXTDEBUG}
  2048. constructor tai_tempalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  2049. begin
  2050. inherited ppuload(t,ppufile);
  2051. temppos:=ppufile.getlongint;
  2052. tempsize:=ppufile.getlongint;
  2053. allocation:=boolean(ppufile.getbyte);
  2054. {$ifdef EXTDEBUG}
  2055. problem:=nil;
  2056. {$endif EXTDEBUG}
  2057. end;
  2058. procedure tai_tempalloc.ppuwrite(ppufile:tcompilerppufile);
  2059. begin
  2060. inherited ppuwrite(ppufile);
  2061. ppufile.putlongint(temppos);
  2062. ppufile.putlongint(tempsize);
  2063. ppufile.putbyte(byte(allocation));
  2064. end;
  2065. {*****************************************************************************
  2066. tai_regalloc
  2067. *****************************************************************************}
  2068. constructor tai_regalloc.alloc(r : tregister;ainstr:tai);
  2069. begin
  2070. inherited create;
  2071. typ:=ait_regalloc;
  2072. ratype:=ra_alloc;
  2073. reg:=r;
  2074. { ainstr must be an instruction }
  2075. if assigned(ainstr) and
  2076. (ainstr.typ<>ait_instruction) then
  2077. internalerror(200411011);
  2078. instr:=ainstr;
  2079. end;
  2080. constructor tai_regalloc.dealloc(r : tregister;ainstr:tai);
  2081. begin
  2082. inherited create;
  2083. typ:=ait_regalloc;
  2084. ratype:=ra_dealloc;
  2085. reg:=r;
  2086. { ainstr must be an instruction }
  2087. if assigned(ainstr) and
  2088. (ainstr.typ<>ait_instruction) then
  2089. internalerror(200411012);
  2090. instr:=ainstr;
  2091. end;
  2092. constructor tai_regalloc.sync(r : tregister);
  2093. begin
  2094. inherited create;
  2095. typ:=ait_regalloc;
  2096. ratype:=ra_sync;
  2097. reg:=r;
  2098. end;
  2099. constructor tai_regalloc.resize(r : tregister);
  2100. begin
  2101. inherited create;
  2102. typ:=ait_regalloc;
  2103. ratype:=ra_resize;
  2104. reg:=r;
  2105. end;
  2106. constructor tai_regalloc.markused(r : tregister);
  2107. begin
  2108. inherited create;
  2109. typ:=ait_regalloc;
  2110. ratype:=ra_markused;
  2111. reg:=r;
  2112. end;
  2113. constructor tai_regalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  2114. begin
  2115. inherited ppuload(t,ppufile);
  2116. ppufile.getdata(reg,sizeof(Tregister));
  2117. ratype:=tregalloctype(ppufile.getbyte);
  2118. keep:=boolean(ppufile.getbyte);
  2119. end;
  2120. procedure tai_regalloc.ppuwrite(ppufile:tcompilerppufile);
  2121. begin
  2122. inherited ppuwrite(ppufile);
  2123. ppufile.putdata(reg,sizeof(Tregister));
  2124. ppufile.putbyte(byte(ratype));
  2125. ppufile.putbyte(byte(keep));
  2126. end;
  2127. {*****************************************************************************
  2128. TaiInstruction
  2129. *****************************************************************************}
  2130. constructor tai_cpu_abstract.Create(op : tasmop);
  2131. begin
  2132. inherited create;
  2133. typ:=ait_instruction;
  2134. is_jmp:=false;
  2135. opcode:=op;
  2136. ops:=0;
  2137. end;
  2138. destructor tai_cpu_abstract.Destroy;
  2139. var
  2140. i : integer;
  2141. begin
  2142. for i:=0 to opercnt-1 do
  2143. freeop(i);
  2144. inherited destroy;
  2145. end;
  2146. { ---------------------------------------------------------------------
  2147. Loading of operands.
  2148. ---------------------------------------------------------------------}
  2149. procedure tai_cpu_abstract.allocate_oper(opers:longint);
  2150. begin
  2151. while (opers>opercnt) do
  2152. begin
  2153. new(oper[opercnt]);
  2154. fillchar(oper[opercnt]^,sizeof(toper),0);
  2155. inc(opercnt);
  2156. end;
  2157. end;
  2158. procedure tai_cpu_abstract.loadconst(opidx:longint;l:tcgint);
  2159. begin
  2160. allocate_oper(opidx+1);
  2161. with oper[opidx]^ do
  2162. begin
  2163. if typ<>top_const then
  2164. clearop(opidx);
  2165. val:=l;
  2166. typ:=top_const;
  2167. end;
  2168. end;
  2169. procedure tai_cpu_abstract.loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  2170. var
  2171. r : treference;
  2172. begin
  2173. reference_reset_symbol(r,s,sofs,1,[]);
  2174. r.refaddr:=addr_full;
  2175. loadref(opidx,r);
  2176. end;
  2177. procedure tai_cpu_abstract.loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  2178. begin
  2179. if not assigned(s) then
  2180. internalerror(200204251);
  2181. allocate_oper(opidx+1);
  2182. with oper[opidx]^ do
  2183. begin
  2184. if typ<>top_local then
  2185. begin
  2186. clearop(opidx);
  2187. new(localoper);
  2188. end;
  2189. with oper[opidx]^.localoper^ do
  2190. begin
  2191. localsym:=s;
  2192. localsymofs:=sofs;
  2193. localindexreg:=indexreg;
  2194. localscale:=scale;
  2195. localgetoffset:=getoffset;
  2196. localforceref:=forceref;
  2197. end;
  2198. typ:=top_local;
  2199. end;
  2200. end;
  2201. procedure tai_cpu_abstract.loadref(opidx:longint;const r:treference);
  2202. begin
  2203. allocate_oper(opidx+1);
  2204. with oper[opidx]^ do
  2205. begin
  2206. if typ<>top_ref then
  2207. begin
  2208. clearop(opidx);
  2209. new(ref);
  2210. end;
  2211. ref^:=r;
  2212. {$ifdef x86}
  2213. { We allow this exception for x86, since overloading this would be
  2214. too much of a a speed penalty}
  2215. if is_x86_parameterized_string_instruction_op(opcode) then
  2216. begin
  2217. if ((ref^.base=NR_NO) or (getsupreg(ref^.base)<>RS_EDI)) and
  2218. ((ref^.index=NR_NO) or (getsupreg(ref^.index)<>RS_EDI)) and
  2219. (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  2220. segprefix:=ref^.segment;
  2221. end
  2222. else if (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  2223. segprefix:=ref^.segment;
  2224. {$endif}
  2225. {$ifndef llvm}
  2226. if (cs_create_pic in current_settings.moduleswitches) and
  2227. assigned(r.symbol) and
  2228. not assigned(r.relsymbol) and
  2229. (r.refaddr=addr_no)
  2230. {$ifdef ARM}
  2231. and not(r.base=NR_R15)
  2232. {$endif ARM}
  2233. {$ifdef aarch64}
  2234. and not(r.refaddr in [addr_full,addr_gotpageoffset,addr_gotpage])
  2235. {$endif aarch64}
  2236. then
  2237. internalerror(200502052);
  2238. {$endif not llvm}
  2239. typ:=top_ref;
  2240. if assigned(add_reg_instruction_hook) then
  2241. begin
  2242. add_reg_instruction_hook(self,ref^.base);
  2243. add_reg_instruction_hook(self,ref^.index);
  2244. end;
  2245. { mark symbol as used }
  2246. if assigned(ref^.symbol) then
  2247. ref^.symbol.increfs;
  2248. if assigned(ref^.relsymbol) then
  2249. ref^.relsymbol.increfs;
  2250. end;
  2251. end;
  2252. procedure tai_cpu_abstract.loadreg(opidx:longint;r:tregister);
  2253. begin
  2254. allocate_oper(opidx+1);
  2255. with oper[opidx]^ do
  2256. begin
  2257. if typ<>top_reg then
  2258. clearop(opidx);
  2259. reg:=r;
  2260. typ:=top_reg;
  2261. end;
  2262. if assigned(add_reg_instruction_hook) then
  2263. add_reg_instruction_hook(self,r);
  2264. {$ifdef ARM}
  2265. { R15 is the PC on the ARM thus moves to R15 are jumps.
  2266. Due to speed considerations we don't use a virtual overridden method here.
  2267. Because the pc/r15 isn't handled by the reg. allocator this should never cause
  2268. problems with iregs getting r15.
  2269. }
  2270. is_jmp:=(opcode=A_MOV) and (opidx=0) and (r=NR_R15);
  2271. {$endif ARM}
  2272. end;
  2273. procedure tai_cpu_abstract.loadoper(opidx:longint;o:toper);
  2274. begin
  2275. allocate_oper(opidx+1);
  2276. clearop(opidx);
  2277. oper[opidx]^:=o;
  2278. { copy also the reference }
  2279. with oper[opidx]^ do
  2280. begin
  2281. case typ of
  2282. top_reg:
  2283. begin
  2284. if assigned(add_reg_instruction_hook) then
  2285. add_reg_instruction_hook(self,reg);
  2286. end;
  2287. top_ref:
  2288. begin
  2289. new(ref);
  2290. ref^:=o.ref^;
  2291. {$ifdef x86}
  2292. if is_x86_parameterized_string_instruction_op(opcode) then
  2293. begin
  2294. if ((ref^.base=NR_NO) or (getsupreg(ref^.base)<>RS_EDI)) and
  2295. ((ref^.index=NR_NO) or (getsupreg(ref^.index)<>RS_EDI)) and
  2296. (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  2297. segprefix:=ref^.segment;
  2298. end
  2299. else if (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  2300. segprefix:=ref^.segment;
  2301. {$endif x86}
  2302. if assigned(add_reg_instruction_hook) then
  2303. begin
  2304. add_reg_instruction_hook(self,ref^.base);
  2305. add_reg_instruction_hook(self,ref^.index);
  2306. end;
  2307. end;
  2308. {$ifdef ARM}
  2309. top_shifterop:
  2310. begin
  2311. new(shifterop);
  2312. shifterop^:=o.shifterop^;
  2313. if assigned(add_reg_instruction_hook) then
  2314. add_reg_instruction_hook(self,shifterop^.rs);
  2315. end;
  2316. {$endif ARM}
  2317. end;
  2318. end;
  2319. end;
  2320. procedure tai_cpu_abstract.clearop(opidx:longint);
  2321. begin
  2322. with oper[opidx]^ do
  2323. begin
  2324. case typ of
  2325. top_ref:
  2326. dispose(ref);
  2327. top_local:
  2328. dispose(localoper);
  2329. {$ifdef ARM}
  2330. top_shifterop:
  2331. dispose(shifterop);
  2332. top_regset:
  2333. dispose(regset);
  2334. {$endif ARM}
  2335. {$ifdef jvm}
  2336. top_string:
  2337. freemem(pcval);
  2338. top_wstring:
  2339. donewidestring(pwstrval);
  2340. {$endif jvm}
  2341. end;
  2342. typ:=top_none;
  2343. end;
  2344. end;
  2345. procedure tai_cpu_abstract.freeop(opidx:longint);
  2346. begin
  2347. clearop(opidx);
  2348. dispose(oper[opidx]);
  2349. end;
  2350. { ---------------------------------------------------------------------
  2351. Miscellaneous methods.
  2352. ---------------------------------------------------------------------}
  2353. procedure tai_cpu_abstract.SetCondition(const c:TAsmCond);
  2354. begin
  2355. condition:=c;
  2356. end;
  2357. Function tai_cpu_abstract.getcopy:TLinkedListItem;
  2358. var
  2359. i : longint;
  2360. p : tai_cpu_abstract;
  2361. begin
  2362. p:=tai_cpu_abstract(inherited getcopy);
  2363. { make a copy of the references }
  2364. p.opercnt:=0;
  2365. p.allocate_oper(ops);
  2366. for i:=0 to ops-1 do
  2367. begin
  2368. p.oper[i]^:=oper[i]^;
  2369. case oper[i]^.typ of
  2370. top_local :
  2371. begin
  2372. new(p.oper[i]^.localoper);
  2373. p.oper[i]^.localoper^:=oper[i]^.localoper^;
  2374. end;
  2375. top_ref :
  2376. begin
  2377. new(p.oper[i]^.ref);
  2378. p.oper[i]^.ref^:=oper[i]^.ref^;
  2379. end;
  2380. {$ifdef ARM}
  2381. top_shifterop:
  2382. begin
  2383. new(p.oper[i]^.shifterop);
  2384. p.oper[i]^.shifterop^:=oper[i]^.shifterop^;
  2385. end;
  2386. {$endif ARM}
  2387. end;
  2388. end;
  2389. getcopy:=p;
  2390. end;
  2391. function tai_cpu_abstract.is_same_reg_move(regtype: Tregistertype):boolean;
  2392. begin
  2393. { When the generic RA is used this needs to be overridden, we don't use
  2394. virtual;abstract; to prevent a lot of warnings of unimplemented abstract methods
  2395. when tai_cpu is created (PFV) }
  2396. internalerror(2004040901);
  2397. result:=false;
  2398. end;
  2399. function tai_cpu_abstract.spilling_get_operation_type(opnr: longint): topertype;
  2400. begin
  2401. internalerror(2004040902);
  2402. result:=operand_readwrite;
  2403. end;
  2404. function tai_cpu_abstract.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  2405. begin
  2406. result := operand_read;
  2407. end;
  2408. constructor tai_cpu_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  2409. var
  2410. i : integer;
  2411. begin
  2412. inherited ppuload(t,ppufile);
  2413. { hopefully, we don't get problems with big/litte endian here when cross compiling :/ }
  2414. ppufile.getdata(condition,sizeof(tasmcond));
  2415. allocate_oper(ppufile.getbyte);
  2416. for i:=0 to ops-1 do
  2417. ppuloadoper(ppufile,oper[i]^);
  2418. opcode:=tasmop(ppufile.getword);
  2419. {$ifdef x86}
  2420. ppufile.getdata(segprefix,sizeof(Tregister));
  2421. {$endif x86}
  2422. is_jmp:=boolean(ppufile.getbyte);
  2423. end;
  2424. procedure tai_cpu_abstract.ppuwrite(ppufile:tcompilerppufile);
  2425. var
  2426. i : integer;
  2427. begin
  2428. inherited ppuwrite(ppufile);
  2429. ppufile.putdata(condition,sizeof(tasmcond));
  2430. ppufile.putbyte(ops);
  2431. for i:=0 to ops-1 do
  2432. ppuwriteoper(ppufile,oper[i]^);
  2433. ppufile.putword(word(opcode));
  2434. {$ifdef x86}
  2435. ppufile.putdata(segprefix,sizeof(Tregister));
  2436. {$endif x86}
  2437. ppufile.putbyte(byte(is_jmp));
  2438. end;
  2439. procedure tai_cpu_abstract.buildderefimpl;
  2440. var
  2441. i : integer;
  2442. begin
  2443. for i:=0 to ops-1 do
  2444. ppubuildderefimploper(oper[i]^);
  2445. end;
  2446. procedure tai_cpu_abstract.derefimpl;
  2447. var
  2448. i : integer;
  2449. begin
  2450. for i:=0 to ops-1 do
  2451. ppuderefoper(oper[i]^);
  2452. end;
  2453. procedure tai_cpu_abstract.resetpass1;
  2454. begin
  2455. end;
  2456. procedure tai_cpu_abstract.resetpass2;
  2457. begin
  2458. end;
  2459. function tai_cpu_abstract.Pass1(objdata:TObjData):longint;
  2460. begin
  2461. result:=0;
  2462. end;
  2463. procedure tai_cpu_abstract.Pass2(objdata:TObjData);
  2464. begin
  2465. end;
  2466. procedure tai_cpu_abstract.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  2467. begin
  2468. o.typ:=toptype(ppufile.getbyte);
  2469. o.ot:=ppufile.getlongint;
  2470. case o.typ of
  2471. top_reg :
  2472. ppufile.getdata(o.reg,sizeof(Tregister));
  2473. top_ref :
  2474. begin
  2475. new(o.ref);
  2476. {$ifdef x86}
  2477. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  2478. {$endif x86}
  2479. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  2480. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  2481. ppufile.getdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  2482. o.ref^.scalefactor:=ppufile.getbyte;
  2483. o.ref^.offset:=ppufile.getaint;
  2484. o.ref^.symbol:=ppufile.getasmsymbol;
  2485. o.ref^.relsymbol:=ppufile.getasmsymbol;
  2486. end;
  2487. top_const :
  2488. o.val:=ppufile.getaint;
  2489. top_local :
  2490. begin
  2491. new(o.localoper);
  2492. with o.localoper^ do
  2493. begin
  2494. ppufile.getderef(localsymderef);
  2495. localsymofs:=ppufile.getaint;
  2496. localindexreg:=tregister(ppufile.getlongint);
  2497. localscale:=ppufile.getbyte;
  2498. localgetoffset:=(ppufile.getbyte<>0);
  2499. end;
  2500. end;
  2501. else
  2502. internalerror(2007010210);
  2503. end;
  2504. end;
  2505. procedure tai_cpu_abstract.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  2506. begin
  2507. ppufile.putbyte(byte(o.typ));
  2508. ppufile.putlongint(o.ot);
  2509. case o.typ of
  2510. top_reg :
  2511. ppufile.putdata(o.reg,sizeof(Tregister));
  2512. top_ref :
  2513. begin
  2514. {$ifdef x86}
  2515. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  2516. {$endif x86}
  2517. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  2518. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  2519. ppufile.putdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  2520. ppufile.putbyte(o.ref^.scalefactor);
  2521. ppufile.putaint(o.ref^.offset);
  2522. ppufile.putasmsymbol(o.ref^.symbol);
  2523. ppufile.putasmsymbol(o.ref^.relsymbol);
  2524. end;
  2525. top_const :
  2526. ppufile.putaint(o.val);
  2527. top_local :
  2528. begin
  2529. with o.localoper^ do
  2530. begin
  2531. ppufile.putderef(localsymderef);
  2532. ppufile.putaint(localsymofs);
  2533. ppufile.putlongint(longint(localindexreg));
  2534. ppufile.putbyte(localscale);
  2535. ppufile.putbyte(byte(localgetoffset));
  2536. end;
  2537. end;
  2538. else
  2539. internalerror(2007010211);
  2540. end;
  2541. end;
  2542. {****************************************************************************
  2543. tai_align_abstract
  2544. ****************************************************************************}
  2545. constructor tai_align_abstract.Create(b: byte);
  2546. begin
  2547. inherited Create;
  2548. typ:=ait_align;
  2549. {$ifdef EXTDEBUG}
  2550. if upper(classname)='TAI_ALIGN_ABSTRACT' then
  2551. internalerror(200709191);
  2552. {$endif EXTDEBUG}
  2553. if b in [1,2,4,8,16,32,64] then
  2554. aligntype := b
  2555. else
  2556. aligntype := 1;
  2557. fillsize:=0;
  2558. fillop:=0;
  2559. use_op:=false;
  2560. end;
  2561. constructor tai_align_abstract.Create_op(b: byte; _op: byte);
  2562. begin
  2563. inherited Create;
  2564. typ:=ait_align;
  2565. if b in [1,2,4,8,16,32] then
  2566. aligntype := b
  2567. else
  2568. aligntype := 1;
  2569. fillsize:=0;
  2570. fillop:=_op;
  2571. use_op:=true;
  2572. end;
  2573. constructor tai_align_abstract.Create_zeros(b: byte);
  2574. begin
  2575. inherited Create;
  2576. typ:=ait_align;
  2577. if b in [1,2,4,8,16,32] then
  2578. aligntype := b
  2579. else
  2580. aligntype := 1;
  2581. use_op:=true;
  2582. fillsize:=0;
  2583. fillop:=0;
  2584. end;
  2585. function tai_align_abstract.calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;
  2586. begin
  2587. if fillsize>sizeof(buf) then
  2588. internalerror(200404293);
  2589. fillchar(buf,high(buf),fillop);
  2590. calculatefillbuf:=pchar(@buf);
  2591. end;
  2592. constructor tai_align_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  2593. begin
  2594. inherited ppuload(t,ppufile);
  2595. aligntype:=ppufile.getbyte;
  2596. fillsize:=0;
  2597. fillop:=ppufile.getbyte;
  2598. use_op:=boolean(ppufile.getbyte);
  2599. end;
  2600. procedure tai_align_abstract.ppuwrite(ppufile:tcompilerppufile);
  2601. begin
  2602. inherited ppuwrite(ppufile);
  2603. ppufile.putbyte(aligntype);
  2604. ppufile.putbyte(fillop);
  2605. ppufile.putbyte(byte(use_op));
  2606. end;
  2607. {****************************************************************************
  2608. tai_seh_directive
  2609. ****************************************************************************}
  2610. const
  2611. datatypemap: array[TAsmSehDirective] of TSehDirectiveDatatype=(
  2612. sd_string, { proc }
  2613. sd_none, { endproc }
  2614. sd_none, { endprologue }
  2615. sd_string, { handler }
  2616. sd_none, { handlerdata }
  2617. sd_none,sd_none,sd_none, { eh, 32, no32 }
  2618. sd_regoffset, { setframe }
  2619. sd_offset, { stackalloc }
  2620. sd_reg, { pushreg }
  2621. sd_regoffset, { savereg }
  2622. sd_regoffset, { savexmm }
  2623. sd_none { pushframe }
  2624. );
  2625. constructor tai_seh_directive.create(_kind:TAsmSehDirective);
  2626. begin
  2627. inherited Create;
  2628. typ:=ait_seh_directive;
  2629. kind:=_kind;
  2630. data.typ:=datatypemap[_kind];
  2631. end;
  2632. constructor tai_seh_directive.create_name(_kind:TAsmSehDirective;const _name:string);
  2633. begin
  2634. create(_kind);
  2635. data.name:=stringdup(_name);
  2636. end;
  2637. constructor tai_seh_directive.create_reg(_kind:TAsmSehDirective;r:TRegister);
  2638. begin
  2639. create(_kind);
  2640. data.reg:=r;
  2641. end;
  2642. constructor tai_seh_directive.create_offset(_kind:TAsmSehDirective;ofs:dword);
  2643. begin
  2644. create(_kind);
  2645. data.offset:=ofs;
  2646. end;
  2647. constructor tai_seh_directive.create_reg_offset(_kind:TAsmSehDirective;
  2648. r:TRegister;ofs:dword);
  2649. begin
  2650. create(_kind);
  2651. data.offset:=ofs;
  2652. data.reg:=r;
  2653. end;
  2654. constructor tai_seh_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  2655. begin
  2656. inherited ppuload(t, ppufile);
  2657. kind:=TAsmSehDirective(ppufile.getbyte);
  2658. data.typ:=datatypemap[kind];
  2659. case data.typ of
  2660. sd_none: ;
  2661. sd_string:
  2662. begin
  2663. data.name:=ppufile.getpshortstring;
  2664. data.flags:=ppufile.getbyte;
  2665. end;
  2666. sd_reg,sd_offset,sd_regoffset:
  2667. begin
  2668. ppufile.getdata(data.reg,sizeof(TRegister));
  2669. data.offset:=ppufile.getdword;
  2670. end;
  2671. else
  2672. InternalError(2011091201);
  2673. end;
  2674. end;
  2675. destructor tai_seh_directive.destroy;
  2676. begin
  2677. if data.typ=sd_string then
  2678. stringdispose(data.name);
  2679. inherited destroy;
  2680. end;
  2681. procedure tai_seh_directive.ppuwrite(ppufile:tcompilerppufile);
  2682. begin
  2683. inherited ppuwrite(ppufile);
  2684. ppufile.putbyte(ord(kind));
  2685. case data.typ of
  2686. sd_none: ;
  2687. sd_string:
  2688. begin
  2689. ppufile.putstring(data.name^);
  2690. ppufile.putbyte(data.flags);
  2691. end;
  2692. sd_reg,sd_offset,sd_regoffset:
  2693. begin
  2694. ppufile.putdata(data.reg,sizeof(TRegister));
  2695. ppufile.putdword(data.offset);
  2696. end;
  2697. else
  2698. InternalError(2011091202);
  2699. end;
  2700. end;
  2701. procedure tai_seh_directive.generate_code(objdata:TObjData);
  2702. begin
  2703. end;
  2704. {$ifdef JVM}
  2705. {****************************************************************************
  2706. tai_jvar
  2707. ****************************************************************************}
  2708. constructor tai_jvar.Create(_stackslot: longint; const _desc: shortstring; _startlab, _stoplab: TAsmSymbol);
  2709. begin
  2710. Inherited create;
  2711. typ:=ait_jvar;
  2712. stackslot:=_stackslot;
  2713. desc:=stringdup(_desc);
  2714. startlab:=_startlab;
  2715. stoplab:=_stoplab;
  2716. end;
  2717. constructor tai_jvar.ppuload(t: taitype; ppufile: tcompilerppufile);
  2718. begin
  2719. inherited ppuload(t, ppufile);
  2720. stackslot:=ppufile.getlongint;
  2721. desc:=ppufile.getpshortstring;
  2722. startlab:=ppufile.getasmsymbol;
  2723. stoplab:=ppufile.getasmsymbol;
  2724. end;
  2725. procedure tai_jvar.ppuwrite(ppufile: tcompilerppufile);
  2726. begin
  2727. inherited ppuwrite(ppufile);
  2728. ppufile.putlongint(stackslot);
  2729. ppufile.putstring(desc^);
  2730. ppufile.putasmsymbol(startlab);
  2731. ppufile.putasmsymbol(stoplab);
  2732. end;
  2733. destructor tai_jvar.destroy;
  2734. begin
  2735. stringdispose(desc);
  2736. inherited destroy;
  2737. end;
  2738. {****************************************************************************
  2739. tai_jcatch
  2740. ****************************************************************************}
  2741. constructor tai_jcatch.Create(const _name: shortstring; _startlab, _stoplab, _handlerlab: TAsmSymbol);
  2742. begin
  2743. Inherited create;
  2744. typ:=ait_jcatch;
  2745. name:=stringdup(_name);
  2746. startlab:=_startlab;
  2747. startlab.increfs;
  2748. stoplab:=_stoplab;
  2749. stoplab.increfs;
  2750. handlerlab:=_handlerlab;
  2751. handlerlab.increfs;
  2752. end;
  2753. destructor tai_jcatch.destroy;
  2754. begin
  2755. stringdispose(name);
  2756. inherited destroy;
  2757. end;
  2758. constructor tai_jcatch.ppuload(t: taitype; ppufile: tcompilerppufile);
  2759. begin
  2760. inherited ppuload(t, ppufile);
  2761. name:=ppufile.getpshortstring;
  2762. startlab:=ppufile.getasmsymbol;
  2763. startlab.increfs;
  2764. stoplab:=ppufile.getasmsymbol;
  2765. stoplab.increfs;
  2766. handlerlab:=ppufile.getasmsymbol;
  2767. handlerlab.increfs;
  2768. end;
  2769. procedure tai_jcatch.ppuwrite(ppufile: tcompilerppufile);
  2770. begin
  2771. inherited ppuwrite(ppufile);
  2772. ppufile.putstring(name^);
  2773. ppufile.putasmsymbol(startlab);
  2774. ppufile.putasmsymbol(stoplab);
  2775. ppufile.putasmsymbol(handlerlab);
  2776. end;
  2777. {$endif JVM}
  2778. begin
  2779. {$push}{$warnings off}
  2780. { taitype should fit into a 4 byte set for speed reasons }
  2781. if ord(high(taitype))>31 then
  2782. internalerror(201108181);
  2783. {$pop}
  2784. end.