aasmtai.pas 112 KB

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