aasmtai.pas 97 KB

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