aasmcnst.pas 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348
  1. {
  2. Copyright (c) 2014 by Jonas Maebe, member of the Free Pascal development
  3. team
  4. This unit implements typed constant data elements at the assembler level
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit aasmcnst;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,globtype,constexp,widestr,
  23. aasmbase,aasmdata,aasmtai,
  24. symconst,symbase,symtype,symdef,symsym;
  25. type
  26. { typed const: integer/floating point/string/pointer/... const along with
  27. tdef info }
  28. ttypedconstkind = (tck_simple, tck_array, tck_record);
  29. { the type of the element and its def }
  30. tai_abstracttypedconst = class abstract (tai)
  31. private
  32. procedure setdef(def: tdef);
  33. protected
  34. fadetyp: ttypedconstkind;
  35. { the def of this element }
  36. fdef: tdef;
  37. public
  38. constructor create(_adetyp: ttypedconstkind; _def: tdef);
  39. property adetyp: ttypedconstkind read fadetyp;
  40. property def: tdef read fdef write setdef;
  41. end;
  42. { a simple data element; the value is stored as a tai }
  43. tai_simpletypedconst = class(tai_abstracttypedconst)
  44. private
  45. procedure setval(AValue: tai);
  46. protected
  47. fval: tai;
  48. public
  49. constructor create(_def: tdef; _val: tai);
  50. destructor destroy; override;
  51. property val: tai read fval write setval;
  52. end;
  53. { an aggregate data element (record or array). Values are stored as an
  54. array of tsimpledataelement. }
  55. tai_aggregatetypedconst = class(tai_abstracttypedconst)
  56. public type
  57. { iterator to walk over all individual items in the aggregate }
  58. tadeenumerator = class(tobject)
  59. private
  60. fvalues: tfpobjectlist;
  61. fvaluespos: longint;
  62. function getcurrent: tai_abstracttypedconst;
  63. public
  64. constructor create(data: tai_aggregatetypedconst);
  65. function movenext: boolean;
  66. procedure reset;
  67. property current: tai_abstracttypedconst read getcurrent;
  68. end;
  69. protected
  70. fvalues: tfpobjectlist;
  71. fisstring: boolean;
  72. { converts the existing data to a single tai_string }
  73. procedure convert_to_string;
  74. procedure add_to_string(strtai: tai_string; othertai: tai);
  75. public
  76. constructor create(_adetyp: ttypedconstkind; _fdef: tdef);
  77. function getenumerator: tadeenumerator;
  78. procedure addvalue(val: tai_abstracttypedconst); virtual;
  79. function valuecount: longint;
  80. procedure insertvaluebeforepos(val: tai_abstracttypedconst; pos: longint);
  81. procedure replacevalueatpos(val: tai_abstracttypedconst; pos: longint);
  82. { change the type to a record, regardless of how the aggregate was created;
  83. the size of the original type and the record must match }
  84. procedure changetorecord(_def: trecorddef);
  85. procedure finish;
  86. destructor destroy; override;
  87. end;
  88. tasmlabofs = record
  89. lab: tasmlabel;
  90. ofs: asizeint;
  91. end;
  92. { flags for the finalisation of the typed const builder asmlist }
  93. ttcasmlistoption = (
  94. { the tasmsymbol is a tasmlabel }
  95. tcalo_is_lab,
  96. { start a new section (e.g., because we don't know the current section
  97. type) }
  98. tcalo_new_section,
  99. { this symbol is the start of a block of data that should be
  100. dead-stripable/smartlinkable; may imply starting a new section, but
  101. not necessarily (depends on what the platform requirements are) }
  102. tcalo_make_dead_strippable,
  103. { this symbol should never be removed by the linker }
  104. tcalo_no_dead_strip,
  105. { start of a vectorized but individually dead strippable list of elements,
  106. like the resource strings of a unit: they have to stay in this order,
  107. but individual elements can be removed }
  108. tcalo_vectorized_dead_strip_start,
  109. { item in the above list }
  110. tcalo_vectorized_dead_strip_item,
  111. { end of the above list }
  112. tcalo_vectorized_dead_strip_end,
  113. { symbol should be weakly defined }
  114. tcalo_weak,
  115. { symbol should be registered with the unit's public assembler symbols }
  116. tcalo_is_public_asm,
  117. { symbol should be declared with AT_DATA_FORCEINDIRECT }
  118. tcalo_data_force_indirect,
  119. { apply const_align() to the alignment, for user-defined data }
  120. tcalo_apply_constalign
  121. );
  122. ttcasmlistoptions = set of ttcasmlistoption;
  123. ttcdeadstripsectionsymboloption = (
  124. { define the symbol }
  125. tcdssso_define,
  126. { register the assembler symbol either with the public or extern assembler
  127. symbols of the unit }
  128. tcdssso_register_asmsym,
  129. { use the indirect symbol }
  130. tcdssso_use_indirect
  131. );
  132. ttcdeadstripsectionsymboloptions = set of ttcdeadstripsectionsymboloption;
  133. { information about aggregates we are parsing }
  134. taggregateinformation = class
  135. private
  136. fnextfieldname: TIDString;
  137. function getcuroffset: asizeint;
  138. procedure setnextfieldname(const AValue: TIDString);
  139. protected
  140. { type of the aggregate }
  141. fdef: tdef;
  142. { type of the aggregate }
  143. ftyp: ttypedconstkind;
  144. { symtable entry of the previously emitted field in case of a
  145. record/object (nil if none emitted yet), used to insert alignment bytes
  146. if necessary for variant records and objects }
  147. fcurfield,
  148. { field corresponding to the data that will be emitted next in case of a
  149. record/object (nil if not set), used to handle variant records and
  150. objects }
  151. fnextfield: tfieldvarsym;
  152. { similar as the fcurfield/fnextfield above, but instead of fieldvarsyms
  153. these are indices in the symlist of a recorddef that correspond to
  154. fieldvarsyms. These are used only for non-variant records, simply
  155. traversing the fields in order. We could use the above method here as
  156. well, but to find the next field we'd always have to use
  157. symlist.indexof(fcurfield), which would be quite slow. These have -1 as
  158. value if they're not set }
  159. fcurindex,
  160. fnextindex: longint;
  161. { anonymous record that is being built as we add constant data }
  162. fanonrecord: boolean;
  163. property curindex: longint read fcurindex write fcurindex;
  164. property nextindex: longint read fnextindex write fnextindex;
  165. public
  166. constructor create(_def: tdef; _typ: ttypedconstkind); virtual;
  167. { calculated padding bytes for alignment if needed, and add the def of the
  168. next field in case we are constructing an anonymous record }
  169. function prepare_next_field(nextfielddef: tdef): asizeint; virtual;
  170. property def: tdef read fdef;
  171. property typ: ttypedconstkind read ftyp;
  172. property curfield: tfieldvarsym read fcurfield write fcurfield;
  173. property nextfield: tfieldvarsym read fnextfield write fnextfield;
  174. property nextfieldname: TIDString write setnextfieldname;
  175. property curoffset: asizeint read getcuroffset;
  176. property anonrecord: boolean read fanonrecord write fanonrecord;
  177. end;
  178. taggregateinformationclass = class of taggregateinformation;
  179. { information about a placeholder element that has been added, and which has
  180. to be replaced later with a real data element }
  181. ttypedconstplaceholder = class abstract
  182. def: tdef;
  183. constructor create(d: tdef);
  184. { same usage as ttai_typedconstbuilder.emit_tai }
  185. procedure replace(ai: tai; d: tdef); virtual; abstract;
  186. end;
  187. { Warning: never directly create a ttai_typedconstbuilder instance,
  188. instead create a cai_typedconstbuilder (this class can be overridden) }
  189. ttai_typedconstbuilder = class abstract
  190. { class type to use when creating new aggregate information instances }
  191. protected class var
  192. caggregateinformation: taggregateinformationclass;
  193. private
  194. function getcurragginfo: taggregateinformation;
  195. procedure set_next_field(AValue: tfieldvarsym);
  196. procedure set_next_field_name(const AValue: TIDString);
  197. protected
  198. { temporary list in which all data is collected }
  199. fasmlist: tasmlist;
  200. { options for the final asmlist }
  201. foptions: ttcasmlistoptions;
  202. { while queueing elements of a compound expression, this is the current
  203. offset in the top-level array/record }
  204. fqueue_offset: asizeint;
  205. fqueued_def: tdef;
  206. { array of caggregateinformation instances }
  207. faggregateinformation: tfpobjectlist;
  208. { Support for generating data that is only referenced from the typed
  209. constant data that we are currently generated. Such data can all be put
  210. in the same dead-strippable unit, as it's either all included or none of
  211. it is included. This data can be spread over multiple kinds of sections
  212. though (e.g. rodata and rodata_no_rel), so per section keep track whether
  213. we already started a dead-strippable unit and if so, what the section
  214. name was (so that on platforms that perform the dead stripping based on
  215. sections, we put all data for one typed constant into a single section
  216. with the same name) }
  217. protected type
  218. tinternal_data_section_info = record
  219. secname: TSymStr;
  220. sectype: TAsmSectiontype;
  221. end;
  222. protected var
  223. { all internally generated data must be stored in the same list, as it must
  224. be consecutive (if it's spread over multiple lists, we don't know in
  225. which order they'll be concatenated) -> keep track of this list }
  226. finternal_data_asmlist: tasmlist;
  227. { kind of the last section we started in the finternal_data_asmlist, to
  228. avoid creating unnecessary section statements }
  229. finternal_data_current_section: TAsmSectiontype;
  230. { info about in which kinds of sections we have already emitted internal
  231. data, and what their names were }
  232. finternal_data_section_info: array of tinternal_data_section_info;
  233. { ensure that finalize_asmlist is called only once }
  234. fasmlist_finalized: boolean;
  235. { ensure that if it's vectorized dead strippable data, we called
  236. finalize_vectorized_dead_strip_asmlist instead of finalize_asmlist }
  237. fvectorized_finalize_called: boolean;
  238. { returns whether def must be handled as an aggregate on the current
  239. platform }
  240. function aggregate_kind(def: tdef): ttypedconstkind; virtual;
  241. { finalize the asmlist: add the necessary symbols etc }
  242. procedure finalize_asmlist(asmsym: tasmsymbol; sym: tsym; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions); virtual;
  243. procedure finalize_asmlist_add_indirect_sym(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions); virtual;
  244. { prepare finalization (common for the default and overridden versions }
  245. procedure finalize_asmlist_prepare(const options: ttcasmlistoptions; var alignment: shortint);
  246. { functionality of the above for vectorized dead strippable sections }
  247. procedure finalize_vectorized_dead_strip_asmlist(sym: tsym; def: tdef; const basename, itemname: TSymStr; st: tsymtable; alignment: shortint; options: ttcasmlistoptions); virtual;
  248. { called by the public emit_tai() routines to actually add the typed
  249. constant data; the public ones also take care of adding extra padding
  250. bytes etc (by calling this one) }
  251. procedure do_emit_tai(p: tai; def: tdef); virtual;
  252. { calls prepare_next_field() and adds the padding bytes in the current
  253. location }
  254. procedure pad_next_field(nextfielddef: tdef);
  255. { returns the index in finternal_data_section_info of the info for the
  256. section of type typ. Returns -1 if there is no such info yet }
  257. function get_internal_data_section_index(typ: TAsmSectiontype): longint;
  258. { get a start label for an internal data section (at the start of a
  259. potentially dead-strippable part) }
  260. function get_internal_data_section_start_label: tasmlabel; virtual;
  261. { get a label in the middle of an internal data section (no dead
  262. stripping) }
  263. function get_internal_data_section_internal_label: tasmlabel; virtual;
  264. { adds a new entry to current_module.linkorderedsymbols }
  265. procedure add_link_ordered_symbol(sym: tasmsymbol; const secname: TSymStr); virtual;
  266. { easy access to the top level aggregate information instance }
  267. property curagginfo: taggregateinformation read getcurragginfo;
  268. public
  269. constructor create(const options: ttcasmlistoptions); virtual;
  270. destructor destroy; override;
  271. public
  272. { returns a builder for generating data that is only referrenced by the
  273. typed constant date we are currently generating (e.g. string data for a
  274. pchar constant). Also returns the label that will be placed at the start
  275. of that data. list is the tasmlist to which the data will be added.
  276. secname can be empty to use a default }
  277. procedure start_internal_data_builder(list: tasmlist; sectype: TAsmSectiontype; const secname: TSymStr; out tcb: ttai_typedconstbuilder; out l: tasmlabel);
  278. { finish a previously started internal data builder, including
  279. concatenating all generated data to the provided list and freeing the
  280. builder }
  281. procedure finish_internal_data_builder(var tcb: ttai_typedconstbuilder; l: tasmlabel; def: tdef; alignment: longint);
  282. { add a simple constant data element (p) to the typed constant.
  283. def is the type of the added value }
  284. procedure emit_tai(p: tai; def: tdef); virtual;
  285. { same as above, for a special case: when the def is a procvardef and we
  286. want to use it explicitly as a procdef (i.e., not as a record with a
  287. code and data pointer in case of a complex procvardef) }
  288. procedure emit_tai_procvar2procdef(p: tai; pvdef: tprocvardef); virtual;
  289. protected
  290. procedure maybe_emit_tail_padding(def: tdef); virtual;
  291. function emit_string_const_common(stringtype: tstringtype; len: asizeint; encoding: tstringencoding; var startlab: tasmlabel):tasmlabofs;
  292. function get_dynstring_def_for_type(stringtype: tstringtype; winlikewidestring: boolean): tstringdef;
  293. procedure begin_aggregate_internal(def: tdef; anonymous: boolean); virtual;
  294. procedure end_aggregate_internal(def: tdef; anonymous: boolean); virtual;
  295. { when building an anonymous record, we cannot immediately insert the
  296. alignment before it in case it's nested, since we only know the required
  297. alignment once all fields have been inserted -> mark the location before
  298. the anonymous record, and insert the alignment once it's finished }
  299. procedure mark_anon_aggregate_alignment; virtual; abstract;
  300. procedure insert_marked_aggregate_alignment(def: tdef); virtual; abstract;
  301. class function get_vectorized_dead_strip_section_symbol(const basename: string; st: tsymtable; options: ttcdeadstripsectionsymboloptions; start: boolean): tasmsymbol; virtual;
  302. public
  303. class function get_vectorized_dead_strip_custom_section_name(const basename: TSymStr; st: tsymtable; options: ttcasmlistoptions; out secname: TSymStr): boolean; virtual;
  304. { get the start/end symbol for a dead stripable vectorized section, such
  305. as the resourcestring data of a unit }
  306. class function get_vectorized_dead_strip_section_symbol_start(const basename: string; st: tsymtable; options: ttcdeadstripsectionsymboloptions): tasmsymbol; virtual;
  307. class function get_vectorized_dead_strip_section_symbol_end(const basename: string; st: tsymtable; options: ttcdeadstripsectionsymboloptions): tasmsymbol; virtual;
  308. { returns true if smartlinking of the dead stripable vectorized lists is supported }
  309. class function is_smartlink_vectorized_dead_strip: boolean; virtual;
  310. class function get_dynstring_rec_name(typ: tstringtype; winlike: boolean; len: asizeint): TSymStr;
  311. class function get_dynstring_rec(typ: tstringtype; winlike: boolean; len: asizeint): trecorddef;
  312. { the datalist parameter specifies where the data for the string constant
  313. will be emitted (via an internal data builder)
  314. Note: data does not have to be #0-terminated (len specifies the length of the valid data) }
  315. function emit_ansistring_const(datalist: TAsmList; data: pchar; len: asizeint; encoding: tstringencoding): tasmlabofs;
  316. function emit_unicodestring_const(datalist: TAsmList; data: tcompilerwidestring; encoding: tstringencoding; winlike: boolean):tasmlabofs;
  317. { emits a tasmlabofs as returned by emit_*string_const }
  318. procedure emit_string_offset(const ll: tasmlabofs; const strlength: longint; const st: tstringtype; const winlikewidestring: boolean; const charptrdef: tdef);virtual;
  319. { emits a tasmlabofs as returned by begin_dynarray_const }
  320. procedure emit_dynarray_offset(const ll:tasmlabofs;const arrlength:asizeint;const arrdef:tarraydef; const arrconstdatadef: trecorddef);virtual;
  321. { starts a dynamic array constant so that its data can be emitted directly afterwards }
  322. function begin_dynarray_const(arrdef:tdef;var startlab:tasmlabel;out arrlengthloc:ttypedconstplaceholder):tasmlabofs;virtual;
  323. { ends a dynamic array constant by updating the count field; the arrlengthloc will be freed }
  324. function end_dynarray_const(arrdef:tdef;arrlength:asizeint;arrlengthloc:ttypedconstplaceholder;llofs:tasmlabofs):tdef;virtual;
  325. { emit a shortstring constant, and return its def }
  326. function emit_shortstring_const(const str: shortstring): tdef;
  327. { emit a pchar string constant (the characters, not a pointer to them), and return its def;
  328. len does not include the terminating #0 (will be added) }
  329. function emit_pchar_const(str: pchar; len: pint): tdef;
  330. { emit a guid constant }
  331. procedure emit_guid_const(const guid: tguid);
  332. { emit a procdef constant }
  333. procedure emit_procdef_const(pd: tprocdef);
  334. { emit an ordinal constant }
  335. procedure emit_ord_const(value: int64; def: tdef);
  336. { emit a reference to a pooled shortstring constant }
  337. procedure emit_pooled_shortstring_const_ref(const str:shortstring);
  338. { begin a potential aggregate type. Must be called for any type
  339. that consists of multiple tai constant data entries, or that
  340. represents an aggregate at the Pascal level (a record, a non-dynamic
  341. array, ... }
  342. procedure maybe_begin_aggregate(def: tdef);
  343. { end a potential aggregate type. Must be paired with every
  344. maybe_begin_aggregate }
  345. procedure maybe_end_aggregate(def: tdef);
  346. { similar as above, but in case
  347. a) it's definitely a record
  348. b) the def of the record should be automatically constructed based on
  349. the types of the emitted fields
  350. packrecords: same as "packrecords x"
  351. recordalign: specify the (minimum) alignment of the start of the record
  352. (no equivalent in source code), used as an alternative for explicit
  353. align statements. Use "1" if it should be calculated based on the
  354. fields
  355. recordalignmin: same as "codealign recordmin=x"
  356. maxcrecordalign: specify maximum C record alignment (no equivalent in
  357. source code)
  358. }
  359. function begin_anonymous_record(const optionalname: string; packrecords, recordalign, recordalignmin: shortint): trecorddef; virtual;
  360. function end_anonymous_record: trecorddef; virtual;
  361. { add a placeholder element at the current position that later can be
  362. filled in with the actual data (via ttypedconstplaceholder.replace)
  363. useful in case you have table preceded by the number of elements, and
  364. you count the elements while building the table }
  365. function emit_placeholder(def: tdef): ttypedconstplaceholder; virtual; abstract;
  366. { Add a comment line
  367. }
  368. procedure emit_comment(const comment : string);
  369. protected
  370. { common code to check whether a placeholder can be added at the current
  371. position }
  372. procedure check_add_placeholder(def: tdef);
  373. public
  374. { The next group of routines are for constructing complex expressions.
  375. While parsing a typed constant these operators are encountered from
  376. outer to inner, so that is also the order in which they should be
  377. added to the queue. Only one queue can be active at a time. }
  378. { Init the queue. Gives an internalerror if a queue was already active }
  379. procedure queue_init(todef: tdef); virtual;
  380. { queue an array/string indexing operation (performs all range checking,
  381. so it doesn't have to be duplicated in all descendents). }
  382. procedure queue_vecn(def: tdef; const index: tconstexprint); virtual;
  383. { queue a subscripting operation }
  384. procedure queue_subscriptn(def: tabstractrecorddef; vs: tfieldvarsym); virtual;
  385. { queue indexing a record recursively via several field names. The fields
  386. are specified in the inner to outer order (i.e., def.field1.field2) }
  387. function queue_subscriptn_multiple_by_name(def: tabstractrecorddef; const fields: array of TIDString): tdef;
  388. { queue a type conversion operation }
  389. procedure queue_typeconvn(fromdef, todef: tdef); virtual;
  390. { queue a pointer add operation }
  391. procedure queue_pointeraddn(def: tpointerdef; const index: tconstexprint); virtual;
  392. { queue a pointer sub operation }
  393. procedure queue_pointersubn(def: tpointerdef; const index: tconstexprint); virtual;
  394. { finalise the queue (so a new one can be created) and flush the
  395. previously queued operations, applying them in reverse order on a...}
  396. { ... procdef }
  397. procedure queue_emit_proc(pd: tprocdef); virtual;
  398. { ... staticvarsym }
  399. procedure queue_emit_staticvar(vs: tstaticvarsym); virtual;
  400. { ... labelsym }
  401. procedure queue_emit_label(l: tlabelsym); virtual;
  402. { ... constsym }
  403. procedure queue_emit_const(cs: tconstsym); virtual;
  404. { ... asmsym/asmlabel }
  405. procedure queue_emit_asmsym(sym: tasmsymbol; def: tdef); virtual;
  406. { ... an ordinal constant }
  407. procedure queue_emit_ordconst(value: int64; def: tdef); virtual;
  408. protected
  409. { returns whether queue_init has been called without a corresponding
  410. queue_emit_* to finish it }
  411. function queue_is_active: boolean;
  412. public
  413. { finalize the internal asmlist (if necessary) and return it.
  414. This asmlist will be freed when the builder is destroyed, so add its
  415. contents to another list first. This property should only be accessed
  416. once all data has been added. }
  417. function get_final_asmlist(asmsym: tasmsymbol; sym: tsym; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: longint): tasmlist;
  418. { same as above, passes nil as sym }
  419. function get_final_asmlist(asmsym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: longint): tasmlist; inline;
  420. function get_final_asmlist_vectorized_dead_strip(sym: tsym; def: tdef; const basename, itemname: TSymStr; st: TSymtable; alignment: longint): tasmlist;
  421. { returns the offset of the string data relative to ansi/unicode/widestring
  422. constant labels. On most platforms, this is 0 (with the header at a
  423. negative offset), but on some platforms such negative offsets are not
  424. supported this is equal to the header size }
  425. class function get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint; virtual;
  426. { returns the offset of the array data relatve to dynamic array constant
  427. labels. On most platforms, this is 0 (with the header at a negative
  428. offset), but on some platforms such negative offsets are not supported
  429. and thus this is equal to the header size }
  430. class function get_dynarray_symofs:pint;virtual;
  431. { set the fieldvarsym whose data we will emit next; needed
  432. in case of variant records, so we know which part of the variant gets
  433. initialised. Also in case of objects, because the fieldvarsyms are spread
  434. over the symtables of the entire inheritance tree }
  435. property next_field: tfieldvarsym write set_next_field;
  436. { set the name of the next field that will be emitted for an anonymous
  437. record (also if that field is a nested anonymous record) }
  438. property next_field_name: TIDString write set_next_field_name;
  439. protected
  440. { these ones always return the actual offset, called by the above (and
  441. overridden versions) }
  442. class function get_string_header_size(typ: tstringtype; winlikewidestring: boolean): pint;
  443. class function get_dynarray_header_size:pint;
  444. end;
  445. ttai_typedconstbuilderclass = class of ttai_typedconstbuilder;
  446. tlowlevelaggregateinformation = class(taggregateinformation)
  447. protected
  448. fanonrecmarker: tai;
  449. public
  450. property anonrecmarker: tai read fanonrecmarker write fanonrecmarker;
  451. end;
  452. tlowleveltypedconstplaceholder = class(ttypedconstplaceholder)
  453. list: tasmlist;
  454. insertpos: tai;
  455. constructor create(l: tasmlist; pos: tai; d: tdef);
  456. procedure replace(ai: tai; d: tdef); override;
  457. end;
  458. ttai_lowleveltypedconstbuilder = class(ttai_typedconstbuilder)
  459. protected
  460. procedure mark_anon_aggregate_alignment; override;
  461. procedure insert_marked_aggregate_alignment(def: tdef); override;
  462. procedure finalize_asmlist(asmsym: tasmsymbol; sym: tsym; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions); override;
  463. public
  464. { set the default value for caggregateinformation (= tlowlevelaggregateinformation) }
  465. class constructor classcreate;
  466. function emit_placeholder(def: tdef): ttypedconstplaceholder; override;
  467. end;
  468. var
  469. ctai_typedconstbuilder: ttai_typedconstbuilderclass;
  470. implementation
  471. uses
  472. cutils,
  473. verbose,globals,systems,
  474. fmodule,
  475. symtable,symutil,defutil;
  476. {****************************************************************************
  477. taggregateinformation
  478. ****************************************************************************}
  479. function taggregateinformation.getcuroffset: asizeint;
  480. var
  481. field: tfieldvarsym;
  482. begin
  483. if assigned(curfield) then
  484. result:=curfield.fieldoffset+curfield.vardef.size
  485. else if curindex<>-1 then
  486. begin
  487. field:=tfieldvarsym(tabstractrecorddef(def).symtable.symlist[curindex]);
  488. result:=field.fieldoffset+field.vardef.size
  489. end
  490. else
  491. result:=0
  492. end;
  493. procedure taggregateinformation.setnextfieldname(const AValue: TIDString);
  494. begin
  495. if (fnextfieldname<>'') or
  496. not anonrecord then
  497. internalerror(2015071503);
  498. fnextfieldname:=AValue;
  499. end;
  500. constructor taggregateinformation.create(_def: tdef; _typ: ttypedconstkind);
  501. begin
  502. fdef:=_def;
  503. ftyp:=_typ;
  504. fcurindex:=-1;
  505. fnextindex:=-1;
  506. end;
  507. function taggregateinformation.prepare_next_field(nextfielddef: tdef): asizeint;
  508. var
  509. sym: tsym;
  510. currentoffset,nextoffset: asizeint;
  511. i: longint;
  512. begin
  513. { get the next field and its offset, and make that next field the current
  514. one }
  515. if assigned(nextfield) then
  516. begin
  517. nextoffset:=nextfield.fieldoffset;
  518. currentoffset:=curoffset;
  519. curfield:=nextfield;
  520. end
  521. else
  522. begin
  523. { must set nextfield for unions and objects, as we cannot
  524. automatically detect the "next" field in that case }
  525. if ((def.typ=recorddef) and
  526. trecorddef(def).isunion) or
  527. is_object(def) then
  528. internalerror(2014091202);
  529. { if we are constructing this record as data gets emitted, add a field
  530. for this data }
  531. if anonrecord then
  532. begin
  533. trecorddef(def).add_field_by_def(fnextfieldname,nextfielddef);
  534. fnextfieldname:='';
  535. end
  536. else if fnextfieldname<>'' then
  537. internalerror(2015071501);
  538. currentoffset:=curoffset;
  539. { find next field }
  540. i:=curindex;
  541. repeat
  542. inc(i);
  543. sym:=tsym(tabstractrecorddef(def).symtable.symlist[i]);
  544. until is_normal_fieldvarsym(sym);
  545. curfield:=tfieldvarsym(sym);
  546. nextoffset:=curfield.fieldoffset;
  547. curindex:=i;
  548. end;
  549. { need padding? }
  550. result:=nextoffset-currentoffset;
  551. end;
  552. {****************************************************************************
  553. ttypedconstplaceholder
  554. ****************************************************************************}
  555. constructor ttypedconstplaceholder.create(d: tdef);
  556. begin
  557. def:=d;
  558. end;
  559. {****************************************************************************
  560. tai_abstracttypedconst
  561. ****************************************************************************}
  562. procedure tai_abstracttypedconst.setdef(def: tdef);
  563. begin
  564. { should not be changed, rewrite the calling code if this happens }
  565. if assigned(fdef) then
  566. Internalerror(2014080203);
  567. fdef:=def;
  568. end;
  569. constructor tai_abstracttypedconst.create(_adetyp: ttypedconstkind; _def: tdef);
  570. begin
  571. inherited create;
  572. typ:=ait_typedconst;
  573. fadetyp:=_adetyp;
  574. fdef:=_def;
  575. end;
  576. {****************************************************************************
  577. tai_simpletypedconst
  578. ****************************************************************************}
  579. procedure tai_simpletypedconst.setval(AValue: tai);
  580. begin
  581. fval:=AValue;
  582. end;
  583. constructor tai_simpletypedconst.create(_def: tdef; _val: tai);
  584. begin
  585. inherited create(tck_simple,_def);
  586. fval:=_val;
  587. end;
  588. destructor tai_simpletypedconst.destroy;
  589. begin
  590. fval.free;
  591. inherited destroy;
  592. end;
  593. {****************************************************************************
  594. tai_aggregatetypedconst.tadeenumerator
  595. ****************************************************************************}
  596. constructor tai_aggregatetypedconst.tadeenumerator.create(data: tai_aggregatetypedconst);
  597. begin
  598. fvalues:=data.fvalues;
  599. fvaluespos:=-1;
  600. end;
  601. function tai_aggregatetypedconst.tadeenumerator.getcurrent: tai_abstracttypedconst;
  602. begin
  603. result:=tai_abstracttypedconst(fvalues[fvaluespos]);
  604. end;
  605. function tai_aggregatetypedconst.tadeenumerator.movenext: boolean;
  606. begin
  607. if fvaluespos<pred(fvalues.count) then
  608. begin
  609. inc(fvaluespos);
  610. result:=true
  611. end
  612. else
  613. result:=false;
  614. end;
  615. procedure tai_aggregatetypedconst.tadeenumerator.reset;
  616. begin
  617. fvaluespos:=0
  618. end;
  619. {****************************************************************************
  620. tai_aggregatetypedconst
  621. ****************************************************************************}
  622. procedure tai_aggregatetypedconst.convert_to_string;
  623. var
  624. ai: tai_abstracttypedconst;
  625. newstr: tai_string;
  626. begin
  627. newstr:=tai_string.Create('');
  628. for ai in self do
  629. begin
  630. if ai.adetyp<>tck_simple then
  631. internalerror(2014070103);
  632. add_to_string(newstr,tai_simpletypedconst(ai).val);
  633. ai.free;
  634. end;
  635. fvalues.count:=0;
  636. { the "nil" def will be replaced with an array def of the appropriate
  637. size once we're finished adding data, so we don't create intermediate
  638. arraydefs all the time }
  639. fvalues.add(tai_simpletypedconst.create(nil,newstr));
  640. end;
  641. procedure tai_aggregatetypedconst.add_to_string(strtai: tai_string; othertai: tai);
  642. var
  643. len1,len2,lent : Integer;
  644. lother_string : tai_string absolute othertai;
  645. begin
  646. case othertai.typ of
  647. ait_string:
  648. begin
  649. // lengths without terminating 0
  650. len1:=strtai.len;
  651. len2:=lother_string.len;
  652. lent:=len1+len2;
  653. SetLength(strtai.str,lent+1);
  654. { also copy null terminator }
  655. move(lother_string.str[0],strtai.str[len1],len2+1);
  656. end;
  657. ait_const:
  658. begin
  659. if tai_const(othertai).size<>1 then
  660. internalerror(2014070101);
  661. { it was already len+1 to hold the #0 -> realloc to len+2 }
  662. len1:=length(strtai.str);
  663. SetLength(strtai.str,len1+1);
  664. strtai.str[len1-1]:=ansichar(tai_const(othertai).value);
  665. strtai.str[len1]:=#0;
  666. end;
  667. else
  668. internalerror(2014070102);
  669. end;
  670. end;
  671. constructor tai_aggregatetypedconst.create(_adetyp: ttypedconstkind; _fdef: tdef);
  672. begin
  673. inherited;
  674. fisstring:=false;
  675. fvalues:=tfpobjectlist.create(true);
  676. end;
  677. function tai_aggregatetypedconst.getenumerator: tadeenumerator;
  678. begin
  679. result:=tadeenumerator.create(self);
  680. end;
  681. procedure tai_aggregatetypedconst.addvalue(val: tai_abstracttypedconst);
  682. begin
  683. { merge string constants and ordinal constants added in an array of
  684. char, to unify the length and the string data }
  685. if fisstring or
  686. ((val.adetyp=tck_simple) and
  687. (tai_simpletypedconst(val).val.typ=ait_string)) then
  688. begin
  689. if not fisstring and
  690. (fvalues.count>0) then
  691. convert_to_string;
  692. fisstring:=true;
  693. case fvalues.count of
  694. 0: fvalues.add(val);
  695. 1:
  696. begin
  697. add_to_string(tai_string(tai_simpletypedconst(fvalues[0]).val),tai_simpletypedconst(val).val);
  698. val.free
  699. end
  700. else
  701. internalerror(2014070104);
  702. end;
  703. end
  704. else
  705. fvalues.add(val);
  706. end;
  707. function tai_aggregatetypedconst.valuecount: longint;
  708. begin
  709. result:=fvalues.count;
  710. end;
  711. procedure tai_aggregatetypedconst.insertvaluebeforepos(val: tai_abstracttypedconst; pos: longint);
  712. begin
  713. fvalues.insert(pos,val);
  714. end;
  715. procedure tai_aggregatetypedconst.replacevalueatpos(val: tai_abstracttypedconst; pos: longint);
  716. begin
  717. { since fvalues owns its elements, it will automatically free the old value }
  718. fvalues[pos]:=val;
  719. end;
  720. procedure tai_aggregatetypedconst.changetorecord(_def: trecorddef);
  721. begin
  722. { must be a record of the same size as the current data }
  723. if assigned(fdef) and
  724. (fdef.size<>_def.size) then
  725. internalerror(2015122402);
  726. fdef:=_def;
  727. fadetyp:=tck_record;
  728. end;
  729. procedure tai_aggregatetypedconst.finish;
  730. var
  731. lString : tai_string;
  732. len : integer;
  733. begin
  734. if fisstring then
  735. begin
  736. { set the def: an array of char with the same length as the string
  737. data }
  738. if fvalues.count<>1 then
  739. internalerror(2014070105);
  740. lString:=tai_string(tai_simpletypedconst(fvalues[0]).val);
  741. len:=lString.len;
  742. tai_simpletypedconst(fvalues[0]).fdef:=carraydef.getreusable(cansichartype,len);
  743. end;
  744. end;
  745. destructor tai_aggregatetypedconst.destroy;
  746. begin
  747. fvalues.free;
  748. inherited destroy;
  749. end;
  750. {*****************************************************************************
  751. ttai_typedconstbuilder
  752. *****************************************************************************}
  753. function ttai_typedconstbuilder.getcurragginfo: taggregateinformation;
  754. begin
  755. if assigned(faggregateinformation) and
  756. (faggregateinformation.count>0) then
  757. result:=taggregateinformation(faggregateinformation[faggregateinformation.count-1])
  758. else
  759. result:=nil;
  760. end;
  761. procedure ttai_typedconstbuilder.set_next_field(AValue: tfieldvarsym);
  762. var
  763. info: taggregateinformation;
  764. begin
  765. info:=curagginfo;
  766. if not assigned(info) then
  767. internalerror(2014091206);
  768. info.nextfield:=AValue;
  769. end;
  770. procedure ttai_typedconstbuilder.set_next_field_name(const AValue: TIDString);
  771. var
  772. info: taggregateinformation;
  773. begin
  774. info:=curagginfo;
  775. if not assigned(info) then
  776. internalerror(2015071502);
  777. info.nextfieldname:='$'+AValue;
  778. end;
  779. procedure ttai_typedconstbuilder.pad_next_field(nextfielddef: tdef);
  780. var
  781. fillbytes: asizeint;
  782. begin
  783. fillbytes:=curagginfo.prepare_next_field(nextfielddef);
  784. while fillbytes>0 do
  785. begin
  786. do_emit_tai(tai_const.create_8bit(0),u8inttype);
  787. dec(fillbytes);
  788. end;
  789. end;
  790. function ttai_typedconstbuilder.get_internal_data_section_index(typ: TAsmSectiontype): longint;
  791. begin
  792. { avoid wrong warning by -Oodfa }
  793. result:=-1;
  794. for result:=low(finternal_data_section_info) to high(finternal_data_section_info) do
  795. if finternal_data_section_info[result].sectype=typ then
  796. exit;
  797. result:=-1;
  798. end;
  799. function ttai_typedconstbuilder.get_internal_data_section_start_label: tasmlabel;
  800. begin
  801. { on Darwin, dead code/data stripping happens based on non-temporary
  802. labels (any label that doesn't start with "L" -- it doesn't have
  803. to be global) }
  804. if target_info.system in systems_darwin then
  805. current_asmdata.getstaticdatalabel(result)
  806. else if create_smartlink_library then
  807. current_asmdata.getglobaldatalabel(result)
  808. else
  809. current_asmdata.getlocaldatalabel(result);
  810. end;
  811. function ttai_typedconstbuilder.get_internal_data_section_internal_label: tasmlabel;
  812. begin
  813. if create_smartlink_library then
  814. { all labels need to be global in case they're in another object }
  815. current_asmdata.getglobaldatalabel(result)
  816. else
  817. { no special requirement for the label -> just get a local one }
  818. current_asmdata.getlocaldatalabel(result);
  819. end;
  820. procedure ttai_typedconstbuilder.add_link_ordered_symbol(sym: tasmsymbol; const secname: TSymStr);
  821. begin
  822. current_module.linkorderedsymbols.concat(sym.Name);
  823. end;
  824. function ttai_typedconstbuilder.aggregate_kind(def: tdef): ttypedconstkind;
  825. begin
  826. if (def.typ in [recorddef,filedef,variantdef]) or
  827. is_object(def) or
  828. ((def.typ=procvardef) and
  829. not tprocvardef(def).is_addressonly and
  830. not is_block(def)) then
  831. result:=tck_record
  832. else if ((def.typ=arraydef) and
  833. not is_dynamic_array(def)) or
  834. ((def.typ=setdef) and
  835. not is_smallset(def)) or
  836. is_shortstring(def) then
  837. result:=tck_array
  838. else
  839. result:=tck_simple;
  840. end;
  841. procedure ttai_typedconstbuilder.finalize_asmlist_prepare(const options: ttcasmlistoptions; var alignment: shortint);
  842. begin
  843. if tcalo_apply_constalign in options then
  844. alignment:=const_align(alignment);
  845. { have we finished all aggregates? }
  846. if (getcurragginfo<>nil) and
  847. { in case of syntax errors, the aggregate may not have been finished }
  848. (ErrorCount=0) then
  849. internalerror(2015072301);
  850. { must call finalize_vectorized_dead_strip_asmlist() instead }
  851. if (([tcalo_vectorized_dead_strip_start,
  852. tcalo_vectorized_dead_strip_item,
  853. tcalo_vectorized_dead_strip_end]*options)<>[]) and
  854. not fvectorized_finalize_called then
  855. internalerror(2015110602);
  856. end;
  857. procedure ttai_typedconstbuilder.finalize_asmlist(asmsym: tasmsymbol; sym: tsym; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions);
  858. var
  859. prelist: tasmlist;
  860. begin
  861. finalize_asmlist_prepare(options, alignment);
  862. prelist:=tasmlist.create;
  863. { only now add items based on the symbolname, because it may be
  864. modified by the "section" specifier in case of a typed constant }
  865. { both in case the data should be dead strippable and never dead
  866. stripped, it should be in a separate section (so this property doesn't
  867. affect other data) }
  868. if ([tcalo_no_dead_strip,tcalo_make_dead_strippable]*options)<>[] then
  869. begin
  870. maybe_new_object_file(prelist);
  871. { we always need a new section here, since if we started a new
  872. object file then we have to say what the section is, and otherwise
  873. we need a new section because that's how the dead stripping works }
  874. new_section(prelist,section,secname,alignment);
  875. end
  876. else if tcalo_new_section in options then
  877. new_section(prelist,section,secname,alignment)
  878. else
  879. prelist.concat(cai_align.Create(alignment));
  880. { On Darwin, use .reference to ensure the data doesn't get dead stripped.
  881. On other platforms, the data must be in the .fpc section (which is
  882. kept via the linker script) }
  883. if tcalo_no_dead_strip in options then
  884. begin
  885. if (target_info.system in systems_darwin) then
  886. begin
  887. { Objective-C section declarations contain "no_dead_strip"
  888. attributes if none of their symbols need to be stripped -> don't
  889. add extra ".reference" statement for their symbols (gcc/clang
  890. don't either) }
  891. if not(section in [low(TObjCAsmSectionType)..high(TObjCAsmSectionType)]) then
  892. prelist.concat(tai_directive.Create(asd_reference,asmsym.name))
  893. end
  894. else if section<>sec_fpc then
  895. internalerror(2015101402);
  896. end;
  897. if not(tcalo_is_lab in options) then
  898. if asmsym.bind=AB_LOCAL then
  899. prelist.concat(tai_symbol.Create(asmsym,0))
  900. else
  901. prelist.concat(tai_symbol.Create_Global(asmsym,0))
  902. else
  903. prelist.concat(tai_label.Create(tasmlabel(asmsym)));
  904. { Newly referenced symbol }
  905. asmsym.increfs;
  906. if tcalo_weak in options then
  907. prelist.concat(tai_directive.Create(asd_weak_definition,asmsym.name));
  908. { insert the symbol information before the data }
  909. fasmlist.insertlist(prelist);
  910. { end of the symbol }
  911. fasmlist.concat(tai_symbol_end.Createname(asmsym.name));
  912. { free the temporary list }
  913. prelist.free;
  914. end;
  915. procedure ttai_typedconstbuilder.finalize_asmlist_add_indirect_sym(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions);
  916. var
  917. ptrdef : tdef;
  918. symind : tasmsymbol;
  919. indtcb : ttai_typedconstbuilder;
  920. indsecname : tsymstr;
  921. begin
  922. if (tcalo_data_force_indirect in options) and
  923. (sym.bind in [AB_GLOBAL,AB_COMMON]) and
  924. (sym.typ=AT_DATA) then
  925. begin
  926. ptrdef:=cpointerdef.getreusable(def);
  927. symind:=current_asmdata.DefineAsmSymbol(sym.name,AB_INDIRECT,AT_DATA,ptrdef);
  928. { reuse the section if possible }
  929. if section=sec_rodata then
  930. indsecname:=secname
  931. else
  932. indsecname:=lower(symind.name);
  933. indtcb:=ctai_typedconstbuilder.create([tcalo_new_section,tcalo_make_dead_strippable]);
  934. indtcb.emit_tai(tai_const.create_sym_offset(sym,0),ptrdef);
  935. current_asmdata.asmlists[al_indirectglobals].concatlist(indtcb.get_final_asmlist(
  936. symind,
  937. ptrdef,
  938. sec_rodata,
  939. indsecname,
  940. ptrdef.alignment));
  941. symind.increfs;
  942. indtcb.free;
  943. if not (target_info.system in systems_indirect_var_imports) then
  944. current_module.add_public_asmsym(symind.name,AB_INDIRECT,AT_DATA);
  945. end;
  946. end;
  947. procedure ttai_typedconstbuilder.finalize_vectorized_dead_strip_asmlist(sym: tsym; def: tdef; const basename, itemname: TSymStr; st: tsymtable; alignment: shortint; options: ttcasmlistoptions);
  948. var
  949. asmsym: tasmsymbol;
  950. secname: TSymStr;
  951. sectype: TAsmSectiontype;
  952. asmtype : TAsmsymtype;
  953. customsecname: boolean;
  954. dsopts : ttcdeadstripsectionsymboloptions;
  955. begin
  956. fvectorized_finalize_called:=true;
  957. asmsym:=nil;
  958. customsecname:=get_vectorized_dead_strip_custom_section_name(basename,st,options,secname);
  959. if customsecname then
  960. sectype:=sec_user
  961. else
  962. sectype:=sec_data;
  963. dsopts:=[tcdssso_define];
  964. if tcalo_is_public_asm in options then
  965. include(dsopts,tcdssso_register_asmsym);
  966. if tcalo_data_force_indirect in options then
  967. include(dsopts,tcdssso_use_indirect);
  968. if tcalo_vectorized_dead_strip_start in options then
  969. begin
  970. { the start and end names are predefined }
  971. if itemname<>'' then
  972. internalerror(2015110801);
  973. asmsym:=get_vectorized_dead_strip_section_symbol_start(basename,st,dsopts);
  974. if not customsecname then
  975. secname:=make_mangledname(basename,st,'1_START');
  976. end
  977. else if tcalo_vectorized_dead_strip_end in options then
  978. begin
  979. { the start and end names are predefined }
  980. if itemname<>'' then
  981. internalerror(2015110802);
  982. asmsym:=get_vectorized_dead_strip_section_symbol_end(basename,st,dsopts);
  983. if not customsecname then
  984. secname:=make_mangledname(basename,st,'3_END');
  985. end
  986. else if tcalo_vectorized_dead_strip_item in options then
  987. begin
  988. if tcalo_data_force_indirect in options then
  989. asmtype:=AT_DATA_FORCEINDIRECT
  990. else
  991. asmtype:=AT_DATA;
  992. asmsym:=current_asmdata.DefineAsmSymbol(make_mangledname(basename,st,itemname),AB_GLOBAL,asmtype,def);
  993. if tcalo_is_public_asm in options then
  994. current_module.add_public_asmsym(asmsym);
  995. if not customsecname then
  996. secname:=make_mangledname(basename,st,'2_'+itemname);
  997. end;
  998. add_link_ordered_symbol(asmsym,secname);
  999. if is_smartlink_vectorized_dead_strip then
  1000. options:=options+[tcalo_new_section,tcalo_make_dead_strippable]
  1001. else
  1002. begin
  1003. { if smartlinking of vectorized lists is not supported,
  1004. put the whole list into a single section. }
  1005. options:=options-[tcalo_new_section,tcalo_make_dead_strippable];
  1006. if tcalo_vectorized_dead_strip_start in options then
  1007. include(options,tcalo_new_section);
  1008. end;
  1009. finalize_asmlist(asmsym,sym,def,sectype,secname,alignment,options);
  1010. end;
  1011. procedure ttai_typedconstbuilder.do_emit_tai(p: tai; def: tdef);
  1012. begin
  1013. { by default we don't care about the type }
  1014. fasmlist.concat(p);
  1015. end;
  1016. function ttai_typedconstbuilder.get_final_asmlist(asmsym: tasmsymbol; sym: tsym; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: longint): tasmlist;
  1017. begin
  1018. if not fasmlist_finalized then
  1019. begin
  1020. finalize_asmlist(asmsym,sym,def,section,secname,alignment,foptions);
  1021. finalize_asmlist_add_indirect_sym(asmsym,def,section,secname,alignment,foptions);
  1022. fasmlist_finalized:=true;
  1023. end;
  1024. result:=fasmlist;
  1025. end;
  1026. function ttai_typedconstbuilder.get_final_asmlist(asmsym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: longint): tasmlist; inline;
  1027. begin
  1028. result:=get_final_asmlist(asmsym,nil,def,section,secname,alignment);
  1029. end;
  1030. function ttai_typedconstbuilder.get_final_asmlist_vectorized_dead_strip(sym: tsym; def: tdef; const basename, itemname: TSymStr; st: TSymtable; alignment: longint): tasmlist;
  1031. begin
  1032. if not fasmlist_finalized then
  1033. begin
  1034. finalize_vectorized_dead_strip_asmlist(sym,def,basename,itemname,st,alignment,foptions);
  1035. fasmlist_finalized:=true;
  1036. end;
  1037. result:=fasmlist;
  1038. end;
  1039. class function ttai_typedconstbuilder.get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint;
  1040. begin
  1041. { darwin's linker does not support negative offsets }
  1042. if not(target_info.system in systems_darwin+systems_wasm) and
  1043. { it seems that clang's assembler has a bug with the ADRP instruction... }
  1044. (target_info.system<>system_aarch64_win64) then
  1045. result:=0
  1046. else
  1047. result:=get_string_header_size(typ,winlikewidestring);
  1048. end;
  1049. class function ttai_typedconstbuilder.get_dynarray_symofs:pint;
  1050. begin
  1051. { darwin's linker does not support negative offsets }
  1052. if not (target_info.system in systems_darwin) and
  1053. { it seems that clang's assembler has a bug with the ADRP instruction... }
  1054. (target_info.system<>system_aarch64_win64) then
  1055. result:=0
  1056. else
  1057. result:=get_dynarray_header_size;
  1058. end;
  1059. class function ttai_typedconstbuilder.get_string_header_size(typ: tstringtype; winlikewidestring: boolean): pint;
  1060. var
  1061. ansistring_header_size: pint;
  1062. unicodestring_header_size: pint;
  1063. begin
  1064. ansistring_header_size:=
  1065. { encoding }
  1066. 2 +
  1067. { elesize }
  1068. 2 +
  1069. { reference count }
  1070. {$ifdef cpu64bitaddr}
  1071. s32inttype.size +
  1072. {$else !cpu64bitaddr}
  1073. sizesinttype.size +
  1074. {$endif !cpu64bitaddr}
  1075. { length }
  1076. sizesinttype.size;
  1077. unicodestring_header_size:=ansistring_header_size;
  1078. case typ of
  1079. st_ansistring:
  1080. result:=ansistring_header_size;
  1081. st_unicodestring:
  1082. result:=unicodestring_header_size;
  1083. st_widestring:
  1084. if winlikewidestring then
  1085. result:=0
  1086. else
  1087. result:=unicodestring_header_size;
  1088. else
  1089. result:=0;
  1090. end;
  1091. end;
  1092. class function ttai_typedconstbuilder.get_dynarray_header_size:pint;
  1093. begin
  1094. result:=
  1095. { reference count }
  1096. ptrsinttype.size +
  1097. { high value }
  1098. sizesinttype.size;
  1099. end;
  1100. constructor ttai_typedconstbuilder.create(const options: ttcasmlistoptions);
  1101. begin
  1102. inherited create;
  1103. fasmlist:=tasmlist.create;
  1104. foptions:=options;
  1105. { queue is empty }
  1106. fqueue_offset:=low(fqueue_offset);
  1107. finternal_data_current_section:=sec_none;
  1108. end;
  1109. destructor ttai_typedconstbuilder.destroy;
  1110. begin
  1111. { the queue should have been flushed if it was used
  1112. (but if there were errors, we may have aborted before that happened) }
  1113. if (fqueue_offset<>low(fqueue_offset)) and
  1114. (ErrorCount=0) then
  1115. internalerror(2014062901);
  1116. faggregateinformation.free;
  1117. fasmlist.free;
  1118. inherited destroy;
  1119. end;
  1120. procedure ttai_typedconstbuilder.start_internal_data_builder(list: tasmlist; sectype: TAsmSectiontype; const secname: TSymStr; out tcb: ttai_typedconstbuilder; out l: tasmlabel);
  1121. var
  1122. options: ttcasmlistoptions;
  1123. foundsec: longint;
  1124. begin
  1125. { you can't start multiple concurrent internal data builders for the
  1126. same tcb, finish the first before starting another }
  1127. if finternal_data_current_section<>sec_none then
  1128. internalerror(2016082801);
  1129. { we don't know what was previously added to this list, so always add
  1130. a section header. We'll use the same section name in case multiple
  1131. items are added to the same kind of section (rodata, rodata_no_rel,
  1132. ...), so that everything will still end up in the same section even if
  1133. there are multiple section headers }
  1134. options:=[tcalo_is_lab,tcalo_new_section];
  1135. finternal_data_current_section:=sectype;
  1136. l:=nil;
  1137. { did we already create a section of this type for the internal data of
  1138. this builder? }
  1139. foundsec:=get_internal_data_section_index(sectype);
  1140. if foundsec=-1 then
  1141. begin
  1142. { we only need to start a dead-strippable section of data at the
  1143. start of the first subsection of this kind for this block.
  1144. exception: if dead stripping happens based on objects/libraries,
  1145. then we only have to create a new object file for the first
  1146. internal data section of any kind (all the rest will simply be put
  1147. in the same object file) }
  1148. if create_smartlink then
  1149. begin
  1150. if not create_smartlink_library or
  1151. (length(finternal_data_section_info)=0) then
  1152. include(options,tcalo_make_dead_strippable);
  1153. { on Darwin, dead code/data stripping happens based on non-
  1154. temporary labels (any label that doesn't start with "L" -- it
  1155. doesn't have to be global) -> add a non-temporary lobel at the
  1156. start of every kind of subsection created in this builder }
  1157. if target_info.system in systems_darwin then
  1158. l:=get_internal_data_section_start_label;
  1159. end;
  1160. foundsec:=length(finternal_data_section_info);
  1161. setlength(finternal_data_section_info,foundsec+1);
  1162. finternal_data_section_info[foundsec].sectype:=sectype;
  1163. end;
  1164. if not assigned(finternal_data_asmlist) and
  1165. (cs_create_smart in current_settings.moduleswitches) then
  1166. begin
  1167. l:=get_internal_data_section_start_label;
  1168. { the internal data list should only be assigned by this routine,
  1169. the first time that an internal data block is started }
  1170. if not assigned(list) or
  1171. assigned(finternal_data_asmlist) then
  1172. internalerror(2015032101);
  1173. finternal_data_asmlist:=list;
  1174. end
  1175. { all internal data for this tcb must go to the same list (otherwise all
  1176. data we want to add to the dead-strippable block is not guaranteed to
  1177. be sequential and e.g. in the same object file in case of library-based
  1178. dead stripping) }
  1179. else if (assigned(finternal_data_asmlist) and
  1180. (list<>finternal_data_asmlist)) or
  1181. not assigned(list) then
  1182. internalerror(2015032102);
  1183. finternal_data_asmlist:=list;
  1184. if not assigned(l) then
  1185. l:=get_internal_data_section_internal_label;
  1186. { first section of this kind -> set name }
  1187. if finternal_data_section_info[foundsec].secname='' then
  1188. if secname='' then
  1189. finternal_data_section_info[foundsec].secname:=l.Name
  1190. else
  1191. finternal_data_section_info[foundsec].secname:=secname
  1192. { if the name is specified multiple times, it must match }
  1193. else if (secname<>'') and
  1194. (finternal_data_section_info[foundsec].secname<>secname) then
  1195. internalerror(2015032401);
  1196. tcb:=ttai_typedconstbuilderclass(classtype).create(options);
  1197. end;
  1198. procedure ttai_typedconstbuilder.finish_internal_data_builder(var tcb: ttai_typedconstbuilder; l: tasmlabel; def: tdef; alignment: longint);
  1199. begin
  1200. finternal_data_asmlist.concatList(tcb.get_final_asmlist(l,def,
  1201. finternal_data_current_section,
  1202. finternal_data_section_info[get_internal_data_section_index(finternal_data_current_section)].secname,
  1203. alignment));
  1204. tcb.free;
  1205. tcb:=nil;
  1206. finternal_data_current_section:=sec_none;
  1207. end;
  1208. procedure ttai_typedconstbuilder.emit_tai(p: tai; def: tdef);
  1209. var
  1210. kind: ttypedconstkind;
  1211. info: taggregateinformation;
  1212. begin
  1213. { these elements can be aggregates themselves, e.g. a shortstring can
  1214. be emitted as a series of bytes and char arrays }
  1215. kind:=aggregate_kind(def);
  1216. info:=curagginfo;
  1217. if (kind<>tck_simple) and
  1218. (not assigned(info) or
  1219. (info.typ<>kind)) then
  1220. internalerror(2014091001);
  1221. { if we're emitting a record, handle the padding bytes, and in case of
  1222. an anonymous record also add the next field }
  1223. if assigned(info) then
  1224. begin
  1225. { queue_init already adds padding }
  1226. if not queue_is_active and
  1227. (is_record(info.def) or
  1228. is_object(info.def)) and
  1229. { may add support for these later }
  1230. not is_packed_record_or_object(info.def) then
  1231. pad_next_field(def);
  1232. end;
  1233. { emit the data }
  1234. do_emit_tai(p,def);
  1235. end;
  1236. procedure ttai_typedconstbuilder.emit_tai_procvar2procdef(p: tai; pvdef: tprocvardef);
  1237. begin
  1238. { nothing special by default, since we don't care about the type }
  1239. emit_tai(p,pvdef);
  1240. end;
  1241. procedure ttai_typedconstbuilder.maybe_emit_tail_padding(def: tdef);
  1242. var
  1243. info: taggregateinformation;
  1244. fillbytes: asizeint;
  1245. begin
  1246. info:=curagginfo;
  1247. if not assigned(info) then
  1248. internalerror(2014091002);
  1249. if def<>info.def then
  1250. internalerror(2014091205);
  1251. if (is_record(def) or
  1252. is_object(def)) and
  1253. not is_packed_record_or_object(def) then
  1254. begin
  1255. fillbytes:=def.size-info.curoffset;
  1256. while fillbytes>0 do
  1257. begin
  1258. do_emit_tai(Tai_const.Create_8bit(0),u8inttype);
  1259. dec(fillbytes)
  1260. end;
  1261. end;
  1262. end;
  1263. function ttai_typedconstbuilder.emit_string_const_common(stringtype: tstringtype; len: asizeint; encoding: tstringencoding; var startlab: tasmlabel): tasmlabofs;
  1264. var
  1265. string_symofs: asizeint;
  1266. charptrdef: tdef;
  1267. elesize: word;
  1268. begin
  1269. result.lab:=startlab;
  1270. result.ofs:=0;
  1271. { pack the data, so that we don't add unnecessary null bytes after the
  1272. constant string }
  1273. begin_anonymous_record('$'+get_dynstring_rec_name(stringtype,false,len),1,sizeof(TConstPtrUInt),1);
  1274. string_symofs:=get_string_symofs(stringtype,false);
  1275. { encoding }
  1276. emit_tai(tai_const.create_16bit(encoding),u16inttype);
  1277. inc(result.ofs,2);
  1278. { element size }
  1279. case stringtype of
  1280. st_ansistring:
  1281. begin
  1282. elesize:=1;
  1283. charptrdef:=charpointertype;
  1284. end;
  1285. st_unicodestring:
  1286. begin
  1287. elesize:=2;
  1288. charptrdef:=widecharpointertype;
  1289. end
  1290. else
  1291. internalerror(2014080401);
  1292. end;
  1293. emit_tai(tai_const.create_16bit(elesize),u16inttype);
  1294. inc(result.ofs,2);
  1295. {$ifdef cpu64bitaddr}
  1296. emit_tai(tai_const.Create_32bit(-1),s32inttype);
  1297. inc(result.ofs,s32inttype.size);
  1298. {$else !cpu64bitaddr}
  1299. emit_tai(tai_const.create_sizeint(-1),sizesinttype);
  1300. inc(result.ofs,sizesinttype.size);
  1301. {$endif !cpu64bitaddr}
  1302. emit_tai(tai_const.create_sizeint(len),sizesinttype);
  1303. inc(result.ofs,sizesinttype.size);
  1304. if string_symofs=0 then
  1305. begin
  1306. { results in slightly more efficient code }
  1307. emit_tai(tai_label.create(result.lab),charptrdef);
  1308. result.ofs:=0;
  1309. { create new label of the same kind (including whether or not the
  1310. name starts with target_asm.labelprefix in case it's AB_LOCAL,
  1311. so we keep the difference depending on whether the original was
  1312. allocated via getstatic/getlocal/getglobal datalabel) }
  1313. startlab:=tasmlabel.create(current_asmdata.AsmSymbolDict,startlab.name+'$strlab',startlab.bind,startlab.typ);
  1314. end;
  1315. { sanity check }
  1316. if result.ofs<>string_symofs then
  1317. internalerror(2012051701);
  1318. end;
  1319. function ttai_typedconstbuilder.get_dynstring_def_for_type(stringtype: tstringtype; winlikewidestring: boolean): tstringdef;
  1320. begin
  1321. if stringtype=st_ansistring then
  1322. result:=tstringdef(cansistringtype)
  1323. else if (stringtype=st_unicodestring) or
  1324. ((stringtype=st_widestring) and
  1325. not winlikewidestring) then
  1326. result:=tstringdef(cunicodestringtype)
  1327. else if stringtype=st_widestring then
  1328. result:=tstringdef(cwidestringtype)
  1329. else
  1330. internalerror(2015122101);
  1331. end;
  1332. procedure ttai_typedconstbuilder.begin_aggregate_internal(def: tdef; anonymous: boolean);
  1333. var
  1334. info: taggregateinformation;
  1335. tck: ttypedconstkind;
  1336. begin
  1337. tck:=aggregate_kind(def);
  1338. if tck=tck_simple then
  1339. exit;
  1340. if not assigned(faggregateinformation) then
  1341. faggregateinformation:=tfpobjectlist.create
  1342. { if we're starting an anonymous record, we can't align it yet because
  1343. the alignment depends on the fields that will be added -> we'll do
  1344. it at the end }
  1345. else if not anonymous or
  1346. ((def.typ<>recorddef) and
  1347. not is_object(def)) then
  1348. begin
  1349. { add padding if necessary, and update the current field/offset }
  1350. info:=curagginfo;
  1351. if (is_record(curagginfo.def) or
  1352. is_object(curagginfo.def)) and
  1353. not is_packed_record_or_object(curagginfo.def) then
  1354. begin
  1355. if queue_is_active then
  1356. internalerror(2015073001);
  1357. pad_next_field(def);
  1358. end;
  1359. end
  1360. { if this is the outer record, no padding is required; the alignment
  1361. has to be specified explicitly in that case via get_final_asmlist() }
  1362. else if assigned(curagginfo) and
  1363. (curagginfo.def.typ=recorddef) then
  1364. { mark where we'll have to insert the padding bytes at the end }
  1365. mark_anon_aggregate_alignment;
  1366. info:=caggregateinformation.create(def,aggregate_kind(def));
  1367. faggregateinformation.add(info);
  1368. end;
  1369. procedure ttai_typedconstbuilder.end_aggregate_internal(def: tdef; anonymous: boolean);
  1370. var
  1371. info: taggregateinformation;
  1372. tck: ttypedconstkind;
  1373. begin
  1374. tck:=aggregate_kind(def);
  1375. if tck=tck_simple then
  1376. exit;
  1377. { add tail padding if necessary }
  1378. maybe_emit_tail_padding(def);
  1379. { pop and free the information }
  1380. info:=curagginfo;
  1381. faggregateinformation.count:=faggregateinformation.count-1;
  1382. info.free;
  1383. end;
  1384. class function ttai_typedconstbuilder.get_vectorized_dead_strip_section_symbol(const basename: string; st: tsymtable; options: ttcdeadstripsectionsymboloptions; start: boolean): tasmsymbol;
  1385. var
  1386. name: TSymStr;
  1387. asmtype : TAsmsymtype;
  1388. begin
  1389. if start then
  1390. name:=make_mangledname(basename,st,'START')
  1391. else
  1392. name:=make_mangledname(basename,st,'END');
  1393. if tcdssso_define in options then
  1394. begin
  1395. if tcdssso_use_indirect in options then
  1396. asmtype:=AT_DATA_FORCEINDIRECT
  1397. else
  1398. asmtype:=AT_DATA;
  1399. result:=current_asmdata.DefineAsmSymbol(name,AB_GLOBAL,asmtype,voidpointertype);
  1400. if tcdssso_register_asmsym in options then
  1401. current_module.add_public_asmsym(result);
  1402. end
  1403. else
  1404. begin
  1405. result:=current_asmdata.RefAsmSymbol(name,AT_DATA,tcdssso_use_indirect in options);
  1406. if tcdssso_register_asmsym in options then
  1407. current_module.add_extern_asmsym(result);
  1408. end;
  1409. end;
  1410. class function ttai_typedconstbuilder.get_vectorized_dead_strip_custom_section_name(const basename: TSymStr; st: tsymtable; options: ttcasmlistoptions; out secname: TSymStr): boolean;
  1411. begin
  1412. result:=false;
  1413. end;
  1414. class function ttai_typedconstbuilder.get_vectorized_dead_strip_section_symbol_start(const basename: string; st: tsymtable; options: ttcdeadstripsectionsymboloptions): tasmsymbol;
  1415. begin
  1416. result:=get_vectorized_dead_strip_section_symbol(basename,st,options,true);
  1417. end;
  1418. class function ttai_typedconstbuilder.get_vectorized_dead_strip_section_symbol_end(const basename: string; st: tsymtable; options: ttcdeadstripsectionsymboloptions): tasmsymbol;
  1419. begin
  1420. result:=get_vectorized_dead_strip_section_symbol(basename,st,options,false);
  1421. end;
  1422. class function ttai_typedconstbuilder.is_smartlink_vectorized_dead_strip: boolean;
  1423. begin
  1424. result:=(tf_smartlink_sections in target_info.flags) and
  1425. (not(target_info.system in systems_darwin) or
  1426. (tf_supports_symbolorderfile in target_info.flags));
  1427. end;
  1428. class function ttai_typedconstbuilder.get_dynstring_rec_name(typ: tstringtype; winlike: boolean; len: asizeint): TSymStr;
  1429. begin
  1430. case typ of
  1431. st_ansistring:
  1432. result:='ansistrrec';
  1433. st_unicodestring,
  1434. st_widestring:
  1435. if (typ=st_unicodestring) or
  1436. not winlike then
  1437. result:='unicodestrrec'
  1438. else
  1439. result:='widestrrec';
  1440. else
  1441. internalerror(2014080402);
  1442. end;
  1443. result:=result+tostr(len);
  1444. end;
  1445. class function ttai_typedconstbuilder.get_dynstring_rec(typ: tstringtype; winlike: boolean; len: asizeint): trecorddef;
  1446. var
  1447. name: TSymStr;
  1448. streledef: tdef;
  1449. strtypesym: ttypesym;
  1450. srsym: tsym;
  1451. srsymtable: tsymtable;
  1452. begin
  1453. name:=get_dynstring_rec_name(typ,winlike,len);
  1454. { search in the interface of all units for the type to reuse it }
  1455. if searchsym_type(name,srsym,srsymtable) then
  1456. begin
  1457. result:=trecorddef(ttypesym(srsym).typedef);
  1458. exit;
  1459. end
  1460. else
  1461. begin
  1462. { also search the implementation of the current unit }
  1463. strtypesym:=try_search_current_module_type(name);
  1464. if assigned(strtypesym) then
  1465. begin
  1466. result:=trecorddef(strtypesym.typedef);
  1467. exit;
  1468. end;
  1469. end;
  1470. if (typ<>st_widestring) or
  1471. not winlike then
  1472. begin
  1473. result:=crecorddef.create_global_internal('$'+name,1,1);
  1474. { encoding }
  1475. result.add_field_by_def('',u16inttype);
  1476. { element size }
  1477. result.add_field_by_def('',u16inttype);
  1478. { elements }
  1479. case typ of
  1480. st_ansistring:
  1481. streledef:=cansichartype;
  1482. st_unicodestring:
  1483. streledef:=cwidechartype;
  1484. else
  1485. internalerror(2016082301);
  1486. end;
  1487. { reference count }
  1488. {$ifdef cpu64bitaddr}
  1489. result.add_field_by_def('',s32inttype);
  1490. {$else !cpu64bitaddr}
  1491. result.add_field_by_def('',sizesinttype);
  1492. {$endif !cpu64bitaddr}
  1493. { length in elements }
  1494. result.add_field_by_def('',sizesinttype);
  1495. end
  1496. else
  1497. begin
  1498. result:=crecorddef.create_global_internal('$'+name,4,
  1499. targetinfos[target_info.system]^.alignment.recordalignmin);
  1500. { length in bytes }
  1501. result.add_field_by_def('',s32inttype);
  1502. streledef:=cwidechartype;
  1503. end;
  1504. { data (include zero terminator) }
  1505. result.add_field_by_def('',carraydef.getreusable(streledef,len+1));
  1506. trecordsymtable(trecorddef(result).symtable).addalignmentpadding;
  1507. end;
  1508. function ttai_typedconstbuilder.emit_ansistring_const(datalist: TAsmList; data: pchar; len: asizeint; encoding: tstringencoding): tasmlabofs;
  1509. var
  1510. startlab: tasmlabel;
  1511. ansistrrecdef: trecorddef;
  1512. datadef: tdef;
  1513. datatcb: ttai_typedconstbuilder;
  1514. ts : Tai_string;
  1515. begin
  1516. start_internal_data_builder(datalist,sec_rodata_norel,'',datatcb,startlab);
  1517. result:=datatcb.emit_string_const_common(st_ansistring,len,encoding,startlab);
  1518. { add room for the #0-terminator }
  1519. datadef:=carraydef.getreusable(cansichartype,len+1);
  1520. datatcb.maybe_begin_aggregate(datadef);
  1521. ts:=tai_string.Create_Data(data,len,true);
  1522. datatcb.emit_tai(ts,datadef);
  1523. datatcb.maybe_end_aggregate(datadef);
  1524. ansistrrecdef:=datatcb.end_anonymous_record;
  1525. finish_internal_data_builder(datatcb,startlab,ansistrrecdef,const_align(voidpointertype.alignment));
  1526. end;
  1527. function ttai_typedconstbuilder.emit_unicodestring_const(datalist: TAsmList; data: tcompilerwidestring; encoding: tstringencoding; winlike: boolean):tasmlabofs;
  1528. var
  1529. i, strlength: longint;
  1530. string_symofs: asizeint;
  1531. startlab: tasmlabel;
  1532. datadef: tdef;
  1533. datatcb: ttai_typedconstbuilder;
  1534. unicodestrrecdef: trecorddef;
  1535. begin
  1536. start_internal_data_builder(datalist,sec_rodata_norel,'',datatcb,startlab);
  1537. strlength:=getlengthwidestring(data);
  1538. if winlike then
  1539. begin
  1540. result.lab:=startlab;
  1541. datatcb.begin_anonymous_record('$'+get_dynstring_rec_name(st_widestring,true,strlength),
  1542. 4,4,
  1543. targetinfos[target_info.system]^.alignment.recordalignmin);
  1544. datatcb.emit_tai(Tai_const.Create_32bit(strlength*cwidechartype.size),s32inttype);
  1545. { can we optimise by placing the string constant label at the
  1546. required offset? }
  1547. string_symofs:=get_string_symofs(st_widestring,true);
  1548. if string_symofs=0 then
  1549. begin
  1550. { yes }
  1551. datatcb.emit_tai(Tai_label.Create(result.lab),widecharpointertype);
  1552. { allocate a separate label for the start of the data (see
  1553. emit_string_const_common() for explanation) }
  1554. startlab:=tasmlabel.create(current_asmdata.AsmSymbolDict,startlab.name+'$strlab',startlab.bind,startlab.typ);
  1555. end
  1556. else
  1557. internalerror(2015031502);
  1558. result.ofs:=string_symofs;
  1559. end
  1560. else
  1561. begin
  1562. result:=datatcb.emit_string_const_common(st_unicodestring,strlength,encoding,startlab);
  1563. end;
  1564. if cwidechartype.size = 2 then
  1565. begin
  1566. datadef:=carraydef.getreusable(cwidechartype,strlength+1);
  1567. datatcb.maybe_begin_aggregate(datadef);
  1568. for i:=0 to strlength-1 do
  1569. datatcb.emit_tai(Tai_const.Create_16bit(data.data[i]),cwidechartype);
  1570. { ending #0 }
  1571. datatcb.emit_tai(Tai_const.Create_16bit(0),cwidechartype);
  1572. datatcb.maybe_end_aggregate(datadef);
  1573. unicodestrrecdef:=datatcb.end_anonymous_record;
  1574. end
  1575. else
  1576. { code generation for other sizes must be written }
  1577. internalerror(200904271);
  1578. finish_internal_data_builder(datatcb,startlab,unicodestrrecdef,const_align(voidpointertype.alignment));
  1579. end;
  1580. procedure ttai_typedconstbuilder.emit_string_offset(const ll: tasmlabofs; const strlength: longint; const st: tstringtype; const winlikewidestring: boolean; const charptrdef: tdef);
  1581. begin
  1582. emit_tai(Tai_const.Create_sym_offset(ll.lab,ll.ofs),get_dynstring_def_for_type(st,winlikewidestring));
  1583. end;
  1584. procedure ttai_typedconstbuilder.emit_dynarray_offset(const ll:tasmlabofs;const arrlength:asizeint;const arrdef:tarraydef; const arrconstdatadef: trecorddef);
  1585. begin
  1586. emit_tai(tai_const.create_sym_offset(ll.lab,ll.ofs),arrdef);
  1587. end;
  1588. function ttai_typedconstbuilder.begin_dynarray_const(arrdef:tdef;var startlab:tasmlabel;out arrlengthloc:ttypedconstplaceholder):tasmlabofs;
  1589. var
  1590. dynarray_symofs: asizeint;
  1591. begin
  1592. result.lab:=startlab;
  1593. result.ofs:=0;
  1594. { pack the data, so that we don't add unnecessary null bytes after the
  1595. constant string }
  1596. begin_anonymous_record('',1,sizeof(TConstPtrUInt),1);
  1597. dynarray_symofs:=get_dynarray_symofs;
  1598. { what to do if ptrsinttype <> sizesinttype??? }
  1599. emit_tai(tai_const.create_sizeint(-1),ptrsinttype);
  1600. inc(result.ofs,ptrsinttype.size);
  1601. arrlengthloc:=emit_placeholder(sizesinttype);
  1602. inc(result.ofs,sizesinttype.size);
  1603. if dynarray_symofs=0 then
  1604. begin
  1605. { results in slightly more efficient code }
  1606. emit_tai(tai_label.create(result.lab),arrdef);
  1607. result.ofs:=0;
  1608. { create new label of the same kind (including whether or not the
  1609. name starts with target_asm.labelprefix in case it's AB_LOCAL,
  1610. so we keep the difference depending on whether the original was
  1611. allocated via getstatic/getlocal/getglobal datalabel) }
  1612. startlab:=tasmlabel.create(current_asmdata.AsmSymbolDict,startlab.name+'$dynarrlab',startlab.bind,startlab.typ);
  1613. end;
  1614. { sanity check }
  1615. if result.ofs<>dynarray_symofs then
  1616. internalerror(2018020601);
  1617. end;
  1618. function ttai_typedconstbuilder.end_dynarray_const(arrdef:tdef;arrlength:asizeint;arrlengthloc:ttypedconstplaceholder;llofs:tasmlabofs):tdef;
  1619. begin
  1620. { we emit the high value, not the count }
  1621. arrlengthloc.replace(tai_const.Create_sizeint(arrlength-1),sizesinttype);
  1622. arrlengthloc.free;
  1623. if get_dynarray_symofs=0 then
  1624. emit_tai(tai_symbol_end.create(llofs.lab),arrdef);
  1625. result:=end_anonymous_record;
  1626. end;
  1627. function ttai_typedconstbuilder.emit_shortstring_const(const str: shortstring): tdef;
  1628. begin
  1629. { we use an arraydef instead of a shortstringdef, because we don't have
  1630. functionality in place yet to reuse shortstringdefs of the same length
  1631. and neither the lowlevel nor the llvm typedconst builder cares about
  1632. this difference }
  1633. result:=carraydef.getreusable(cansichartype,length(str)+1);
  1634. maybe_begin_aggregate(result);
  1635. emit_tai(Tai_const.Create_8bit(length(str)),u8inttype);
  1636. if str<>'' then
  1637. emit_tai(Tai_string.Create(str),carraydef.getreusable(cansichartype,length(str)));
  1638. maybe_end_aggregate(result);
  1639. end;
  1640. function ttai_typedconstbuilder.emit_pchar_const(str: pchar; len: pint): tdef;
  1641. begin
  1642. result:=carraydef.getreusable(cansichartype,len+1);
  1643. maybe_begin_aggregate(result);
  1644. if len=0 then
  1645. emit_tai(Tai_const.Create_8bit(0),cansichartype)
  1646. else
  1647. emit_tai(Tai_string.Create_Data(str,len,true),result);
  1648. maybe_end_aggregate(result);
  1649. end;
  1650. procedure ttai_typedconstbuilder.emit_guid_const(const guid: tguid);
  1651. var
  1652. i: longint;
  1653. field: tfieldvarsym;
  1654. begin
  1655. maybe_begin_aggregate(rec_tguid);
  1656. { variant record -> must specify which fields get initialised }
  1657. next_field:=tfieldvarsym(rec_tguid.symtable.Find('DATA1'));
  1658. emit_tai(Tai_const.Create_32bit(longint(guid.D1)),u32inttype);
  1659. next_field:=tfieldvarsym(rec_tguid.symtable.Find('DATA2'));
  1660. emit_tai(Tai_const.Create_16bit(guid.D2),u16inttype);
  1661. next_field:=tfieldvarsym(rec_tguid.symtable.Find('DATA3'));
  1662. emit_tai(Tai_const.Create_16bit(guid.D3),u16inttype);
  1663. field:=tfieldvarsym(rec_tguid.symtable.Find('DATA4'));
  1664. next_field:=field;
  1665. { the array }
  1666. maybe_begin_aggregate(field.vardef);
  1667. for i:=Low(guid.D4) to High(guid.D4) do
  1668. emit_tai(Tai_const.Create_8bit(guid.D4[i]),u8inttype);
  1669. maybe_end_aggregate(field.vardef);
  1670. maybe_end_aggregate(rec_tguid);
  1671. end;
  1672. procedure ttai_typedconstbuilder.emit_procdef_const(pd: tprocdef);
  1673. begin
  1674. emit_tai(Tai_const.Createname(pd.mangledname,AT_FUNCTION,0),cprocvardef.getreusableprocaddr(pd,pc_address_only));
  1675. end;
  1676. procedure ttai_typedconstbuilder.emit_ord_const(value: int64; def: tdef);
  1677. begin
  1678. case def.size of
  1679. 1:
  1680. emit_tai(Tai_const.Create_8bit(byte(value)),def);
  1681. 2:
  1682. emit_tai(Tai_const.Create_16bit(word(value)),def);
  1683. 4:
  1684. emit_tai(Tai_const.Create_32bit(longint(value)),def);
  1685. 8:
  1686. emit_tai(Tai_const.Create_64bit(value),def);
  1687. else
  1688. internalerror(2014100501);
  1689. end;
  1690. end;
  1691. procedure ttai_typedconstbuilder.emit_pooled_shortstring_const_ref(const str:shortstring);
  1692. var
  1693. pool : thashset;
  1694. entry : phashsetitem;
  1695. strlab : tasmlabel;
  1696. l : longint;
  1697. datadef : tdef;
  1698. strtcb : ttai_typedconstbuilder;
  1699. begin
  1700. pool:=current_asmdata.ConstPools[sp_shortstr];
  1701. entry:=pool.FindOrAdd(@str[1],length(str));
  1702. { :-(, we must generate a new entry }
  1703. if not assigned(entry^.Data) then
  1704. begin
  1705. current_asmdata.getglobaldatalabel(strlab);
  1706. { include length and terminating zero for quick conversion to pchar }
  1707. l:=length(str);
  1708. datadef:=carraydef.getreusable(cansichartype,l+2);
  1709. { we start a new constbuilder as we don't know whether we're called
  1710. from inside an internal constbuilder }
  1711. strtcb:=ctai_typedconstbuilder.create([tcalo_is_lab,tcalo_make_dead_strippable,tcalo_apply_constalign]);
  1712. strtcb.maybe_begin_aggregate(datadef);
  1713. { l+1: include length byte; true: add terminating #0 }
  1714. strtcb.emit_tai(Tai_string.Create_Data(@str[0],l+1,true),datadef);
  1715. strtcb.maybe_end_aggregate(datadef);
  1716. current_asmdata.asmlists[al_typedconsts].concatList(
  1717. strtcb.get_final_asmlist(strlab,datadef,sec_rodata_norel,strlab.name,const_align(sizeof(pint)))
  1718. );
  1719. strtcb.free;
  1720. entry^.Data:=strlab;
  1721. { Make sure strlab has a reference }
  1722. strlab.increfs;
  1723. end
  1724. else
  1725. strlab:=tasmlabel(entry^.Data);
  1726. emit_tai(tai_const.Create_sym(strlab),charpointertype);
  1727. end;
  1728. procedure ttai_typedconstbuilder.maybe_begin_aggregate(def: tdef);
  1729. begin
  1730. begin_aggregate_internal(def,false);
  1731. end;
  1732. procedure ttai_typedconstbuilder.maybe_end_aggregate(def: tdef);
  1733. begin
  1734. end_aggregate_internal(def,false);
  1735. end;
  1736. function ttai_typedconstbuilder.begin_anonymous_record(const optionalname: string; packrecords, recordalign, recordalignmin: shortint): trecorddef;
  1737. var
  1738. anonrecorddef: trecorddef;
  1739. typesym: ttypesym;
  1740. begin
  1741. { if the name is specified, we create a typesym with that name in order
  1742. to ensure we can find it again later with that name -> reuse here as
  1743. well if possible (and that also avoids duplicate type name issues) }
  1744. if optionalname<>'' then
  1745. begin
  1746. typesym:=try_search_current_module_type(optionalname);
  1747. if assigned(typesym) then
  1748. begin
  1749. if typesym.typedef.typ<>recorddef then
  1750. internalerror(2015071401);
  1751. result:=trecorddef(typesym.typedef);
  1752. maybe_begin_aggregate(result);
  1753. exit;
  1754. end;
  1755. end;
  1756. { create skeleton def }
  1757. anonrecorddef:=crecorddef.create_global_internal(optionalname,packrecords,recordalignmin);
  1758. trecordsymtable(anonrecorddef.symtable).recordalignment:=recordalign;
  1759. { generic aggregate housekeeping }
  1760. begin_aggregate_internal(anonrecorddef,true);
  1761. { mark as anonymous record }
  1762. curagginfo.anonrecord:=true;
  1763. { in case a descendent wants to do something with the anonrecorddef too }
  1764. result:=anonrecorddef;
  1765. end;
  1766. function ttai_typedconstbuilder.end_anonymous_record: trecorddef;
  1767. var
  1768. info: taggregateinformation;
  1769. anonrecord: boolean;
  1770. begin
  1771. info:=curagginfo;
  1772. if not assigned(info) or
  1773. (info.def.typ<>recorddef) then
  1774. internalerror(2014080201);
  1775. result:=trecorddef(info.def);
  1776. { make a copy, as we need it after info has been freed by
  1777. maybe_end_aggregate(result) }
  1778. anonrecord:=info.anonrecord;
  1779. { finalise the record skeleton (all fields have been added already by
  1780. emit_tai()) -- anonrecord may not be set in case we reused an earlier
  1781. constructed def }
  1782. if anonrecord then
  1783. trecordsymtable(result.symtable).addalignmentpadding;
  1784. end_aggregate_internal(result,true);
  1785. if anonrecord and
  1786. assigned(curagginfo) and
  1787. (curagginfo.def.typ=recorddef) then
  1788. insert_marked_aggregate_alignment(result);
  1789. end;
  1790. procedure ttai_typedconstbuilder.emit_comment(const comment: string);
  1791. var
  1792. comm: tai_comment;
  1793. begin
  1794. if (length(comment)=0) then
  1795. exit;
  1796. comm:=tai_comment.Create(strpnew(comment));
  1797. fasmlist.concat(comm);
  1798. end;
  1799. procedure ttai_typedconstbuilder.check_add_placeholder(def: tdef);
  1800. begin
  1801. { it only makes sense to add a placeholder inside an aggregate
  1802. (otherwise there can be but one element)
  1803. we cannot add a placeholder in the middle of a queued expression
  1804. either
  1805. the placeholder cannot be an aggregate }
  1806. if not assigned(curagginfo) or
  1807. queue_is_active or
  1808. (aggregate_kind(def)<>tck_simple) then
  1809. internalerror(2015091001);
  1810. end;
  1811. procedure ttai_typedconstbuilder.queue_init(todef: tdef);
  1812. var
  1813. info: taggregateinformation;
  1814. begin
  1815. { nested call to init? }
  1816. if fqueue_offset<>low(fqueue_offset) then
  1817. internalerror(2014062101);
  1818. { insert padding bytes before starting the queue, so that the first
  1819. padding byte won't be interpreted as the emitted value for this queue }
  1820. info:=curagginfo;
  1821. if assigned(info) then
  1822. begin
  1823. if ((info.def.typ=recorddef) or
  1824. is_object(info.def)) and
  1825. { may add support for these later }
  1826. not is_packed_record_or_object(info.def) then
  1827. pad_next_field(todef);
  1828. end;
  1829. fqueue_offset:=0;
  1830. fqueued_def:=todef;
  1831. end;
  1832. procedure ttai_typedconstbuilder.queue_vecn(def: tdef; const index: tconstexprint);
  1833. var
  1834. elelen,
  1835. vecbase: asizeint;
  1836. v: tconstexprint;
  1837. begin
  1838. elelen:=1;
  1839. vecbase:=0;
  1840. case def.typ of
  1841. stringdef :
  1842. ;
  1843. arraydef :
  1844. begin
  1845. if not is_packed_array(def) then
  1846. begin
  1847. elelen:=tarraydef(def).elesize;
  1848. vecbase:=tarraydef(def).lowrange;
  1849. end
  1850. else
  1851. Message(parser_e_packed_dynamic_open_array);
  1852. end;
  1853. else
  1854. Message(parser_e_illegal_expression);
  1855. end;
  1856. { Prevent overflow }
  1857. v:=index-vecbase;
  1858. if (v<int64(low(fqueue_offset))) or (v>int64(high(fqueue_offset))) then
  1859. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(fqueue_offset)),tostr(high(fqueue_offset)));
  1860. if high(fqueue_offset)-fqueue_offset div elelen>v then
  1861. inc(fqueue_offset,elelen*v.svalue)
  1862. else
  1863. message3(type_e_range_check_error_bounds,tostr(index),tostr(vecbase),tostr(high(fqueue_offset)-fqueue_offset div elelen+vecbase))
  1864. end;
  1865. procedure ttai_typedconstbuilder.queue_pointeraddn(def: tpointerdef; const index: tconstexprint);
  1866. begin
  1867. inc(fqueue_offset,def.pointeddef.size*int64(index));
  1868. end;
  1869. procedure ttai_typedconstbuilder.queue_pointersubn(def: tpointerdef; const index: tconstexprint);
  1870. begin
  1871. dec(fqueue_offset,def.pointeddef.size*int64(index));
  1872. end;
  1873. procedure ttai_typedconstbuilder.queue_subscriptn(def: tabstractrecorddef; vs: tfieldvarsym);
  1874. begin
  1875. inc(fqueue_offset,vs.fieldoffset);
  1876. end;
  1877. function ttai_typedconstbuilder.queue_subscriptn_multiple_by_name(def: tabstractrecorddef; const fields: array of TIDString): tdef;
  1878. var
  1879. syms,
  1880. parentdefs: tfplist;
  1881. sym: tsym;
  1882. curdef: tdef;
  1883. i: longint;
  1884. begin
  1885. result:=nil;
  1886. if length(fields)=0 then
  1887. internalerror(2015071601);
  1888. syms:=tfplist.Create;
  1889. syms.count:=length(fields);
  1890. parentdefs:=tfplist.create;
  1891. parentdefs.Count:=length(fields);
  1892. curdef:=def;
  1893. for i:=low(fields) to high(fields) do
  1894. begin
  1895. sym:=search_struct_member_no_helper(tabstractrecorddef(curdef),fields[i]);
  1896. if not assigned(sym) or
  1897. not is_normal_fieldvarsym(sym) or
  1898. ((i<>high(fields)) and
  1899. not(tfieldvarsym(sym).vardef.typ in [objectdef,recorddef])) then
  1900. internalerror(2015071505);
  1901. syms[i]:=sym;
  1902. parentdefs[i]:=curdef;
  1903. curdef:=tfieldvarsym(sym).vardef;
  1904. result:=curdef;
  1905. end;
  1906. for i:=high(fields) downto low(fields) do
  1907. queue_subscriptn(tabstractrecorddef(parentdefs[i]),tfieldvarsym(syms[i]));
  1908. syms.free;
  1909. parentdefs.free;
  1910. end;
  1911. procedure ttai_typedconstbuilder.queue_typeconvn(fromdef, todef: tdef);
  1912. begin
  1913. { do nothing }
  1914. end;
  1915. procedure ttai_typedconstbuilder.queue_emit_proc(pd: tprocdef);
  1916. begin
  1917. if fqueue_offset<>0 then
  1918. internalerror(2014092101);
  1919. emit_procdef_const(pd);
  1920. fqueue_offset:=low(fqueue_offset);
  1921. end;
  1922. procedure ttai_typedconstbuilder.queue_emit_staticvar(vs: tstaticvarsym);
  1923. begin
  1924. { pointerdef because we are emitting a pointer to the staticvarsym
  1925. data, not the data itself }
  1926. emit_tai(Tai_const.Createname(vs.mangledname,AT_DATA,fqueue_offset),cpointerdef.getreusable(vs.vardef));
  1927. fqueue_offset:=low(fqueue_offset);
  1928. end;
  1929. procedure ttai_typedconstbuilder.queue_emit_label(l: tlabelsym);
  1930. begin
  1931. emit_tai(Tai_const.Createname(l.mangledname,fqueue_offset),voidcodepointertype);
  1932. fqueue_offset:=low(fqueue_offset);
  1933. end;
  1934. procedure ttai_typedconstbuilder.queue_emit_const(cs: tconstsym);
  1935. var
  1936. resourcestrrec: trecorddef;
  1937. begin
  1938. if not (cs.consttyp in [constresourcestring,constwresourcestring]) then
  1939. internalerror(2014062102);
  1940. if fqueue_offset<>0 then
  1941. internalerror(2014062103);
  1942. { warning: update if/when the type of resource strings changes }
  1943. case cs.consttyp of
  1944. constwresourcestring,
  1945. constresourcestring:
  1946. begin
  1947. resourcestrrec:=trecorddef(search_system_type('TRESOURCESTRINGRECORD').typedef);
  1948. queue_subscriptn_multiple_by_name(resourcestrrec,['CURRENTVALUE']);
  1949. queue_emit_asmsym(current_asmdata.RefAsmSymbol(
  1950. make_mangledname('RESSTR',cs.owner,cs.name),AT_DATA),resourcestrrec
  1951. );
  1952. end;
  1953. { can these occur? }
  1954. constord,
  1955. conststring,constreal,
  1956. constset,constpointer,constnil,
  1957. constwstring,constguid:
  1958. internalerror(2015090903);
  1959. else
  1960. internalerror(2015090904);
  1961. end;
  1962. fqueue_offset:=low(fqueue_offset);
  1963. end;
  1964. procedure ttai_typedconstbuilder.queue_emit_asmsym(sym: tasmsymbol; def: tdef);
  1965. begin
  1966. { pointerdef, because "sym" represents the address of whatever the
  1967. data is }
  1968. def:=cpointerdef.getreusable(def);
  1969. emit_tai(Tai_const.Create_sym_offset(sym,fqueue_offset),def);
  1970. fqueue_offset:=low(fqueue_offset);
  1971. end;
  1972. procedure ttai_typedconstbuilder.queue_emit_ordconst(value: int64; def: tdef);
  1973. begin
  1974. emit_ord_const(value,def);
  1975. fqueue_offset:=low(fqueue_offset);
  1976. end;
  1977. function ttai_typedconstbuilder.queue_is_active: boolean;
  1978. begin
  1979. result:=fqueue_offset<>low(fqueue_offset)
  1980. end;
  1981. {****************************************************************************
  1982. tlowleveltypedconstplaceholder
  1983. ****************************************************************************}
  1984. constructor tlowleveltypedconstplaceholder.create(l: tasmlist; pos: tai; d: tdef);
  1985. begin
  1986. inherited create(d);
  1987. list:=l;
  1988. insertpos:=pos;
  1989. end;
  1990. procedure tlowleveltypedconstplaceholder.replace(ai: tai; d: tdef);
  1991. begin
  1992. if d<>def then
  1993. internalerror(2015091007);
  1994. list.insertafter(ai,insertpos);
  1995. list.remove(insertpos);
  1996. insertpos.free;
  1997. end;
  1998. {****************************************************************************
  1999. tai_abstracttypedconst
  2000. ****************************************************************************}
  2001. class constructor ttai_lowleveltypedconstbuilder.classcreate;
  2002. begin
  2003. caggregateinformation:=tlowlevelaggregateinformation;
  2004. end;
  2005. function ttai_lowleveltypedconstbuilder.emit_placeholder(def: tdef): ttypedconstplaceholder;
  2006. var
  2007. p: tai;
  2008. begin
  2009. check_add_placeholder(def);
  2010. p:=tai_marker.Create(mark_position);
  2011. emit_tai(p,def);
  2012. result:=tlowleveltypedconstplaceholder.create(fasmlist,p,def);
  2013. end;
  2014. procedure ttai_lowleveltypedconstbuilder.mark_anon_aggregate_alignment;
  2015. var
  2016. marker: tai_marker;
  2017. begin
  2018. marker:=tai_marker.Create(mark_position);
  2019. fasmlist.concat(marker);
  2020. tlowlevelaggregateinformation(curagginfo).anonrecmarker:=marker;
  2021. end;
  2022. procedure ttai_lowleveltypedconstbuilder.insert_marked_aggregate_alignment(def: tdef);
  2023. var
  2024. info: tlowlevelaggregateinformation;
  2025. fillbytes: asizeint;
  2026. begin
  2027. info:=tlowlevelaggregateinformation(curagginfo);
  2028. if not assigned(info.anonrecmarker) then
  2029. internalerror(2014091401);
  2030. fillbytes:=info.prepare_next_field(def);
  2031. while fillbytes>0 do
  2032. begin
  2033. fasmlist.insertafter(tai_const.create_8bit(0),info.anonrecmarker);
  2034. dec(fillbytes);
  2035. end;
  2036. fasmlist.remove(info.anonrecmarker);
  2037. info.anonrecmarker.free;
  2038. info.anonrecmarker:=nil;
  2039. end;
  2040. procedure ttai_lowleveltypedconstbuilder.finalize_asmlist(asmsym: tasmsymbol; sym: tsym; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions);
  2041. begin
  2042. inherited;
  2043. { The darwin/ppc64 assembler or linker seems to have trouble }
  2044. { if a section ends with a global label without any data after it. }
  2045. { So for safety, just put a dummy value here. }
  2046. { Further, the regular linker also kills this symbol when turning }
  2047. { on smart linking in case no value appears after it, so put the }
  2048. { dummy byte there always }
  2049. { Update: the Mac OS X 10.6 linker orders data that needs to be }
  2050. { relocated before all other data, so make this data relocatable, }
  2051. { otherwise the end label won't be moved with the rest }
  2052. if (tcalo_vectorized_dead_strip_end in options) and
  2053. (target_info.system in (systems_darwin+systems_aix)) then
  2054. fasmlist.concat(Tai_const.create_sym(asmsym));
  2055. end;
  2056. begin
  2057. ctai_typedconstbuilder:=ttai_lowleveltypedconstbuilder;
  2058. end.