aasmcnst.pas 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907
  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,
  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(_adetyp: ttypedconstkind; _def: tdef; _val: tai);
  50. property val: tai read fval write setval;
  51. end;
  52. { an aggregate data element (record or array). Values are stored as an
  53. array of tsimpledataelement. }
  54. tai_aggregatetypedconst = class(tai_abstracttypedconst)
  55. public type
  56. { iterator to walk over all individual items in the aggregate }
  57. tadeenumerator = class(tobject)
  58. private
  59. fvalues: tfplist;
  60. fvaluespos: longint;
  61. function getcurrent: tai_abstracttypedconst;
  62. public
  63. constructor create(data: tai_aggregatetypedconst);
  64. function movenext: boolean;
  65. procedure reset;
  66. property current: tai_abstracttypedconst read getcurrent;
  67. end;
  68. protected
  69. fvalues: tfplist;
  70. fisstring: boolean;
  71. { converts the existing data to a single tai_string }
  72. procedure convert_to_string;
  73. procedure add_to_string(strtai: tai_string; othertai: tai);
  74. public
  75. constructor create(_adetyp: ttypedconstkind; _fdef: tdef);
  76. function getenumerator: tadeenumerator;
  77. procedure addvalue(val: tai_abstracttypedconst);
  78. function valuecount: longint;
  79. procedure insertvaluebeforepos(val: tai_abstracttypedconst; pos: longint);
  80. function replacevalueatpos(val: tai_abstracttypedconst; pos: longint): tai_abstracttypedconst;
  81. procedure finish;
  82. destructor destroy; override;
  83. end;
  84. tasmlabofs = record
  85. lab: tasmlabel;
  86. ofs: asizeint;
  87. end;
  88. { flags for the finalisation of the typed const builder asmlist }
  89. ttcasmlistoption = (
  90. { the tasmsymbol is a tasmlabel }
  91. tcalo_is_lab,
  92. { start a new section (e.g., because we don't know the current section
  93. type) }
  94. tcalo_new_section,
  95. { this symbol is the start of a block of data that should be
  96. dead-stripable/smartlinkable; may imply starting a new section, but
  97. not necessarily (depends on what the platform requirements are) }
  98. tcalo_make_dead_strippable,
  99. { this symbol should never be removed by the linker }
  100. tcalo_no_dead_strip,
  101. { start of a vectorized but individually dead strippable list of elements,
  102. like the resource strings of a unit: they have to stay in this order,
  103. but individual elements can be removed }
  104. tcalo_vectorized_dead_strip_start,
  105. { item in the above list }
  106. tcalo_vectorized_dead_strip_item,
  107. { end of the above list }
  108. tcalo_vectorized_dead_strip_end
  109. );
  110. ttcasmlistoptions = set of ttcasmlistoption;
  111. { information about aggregates we are parsing }
  112. taggregateinformation = class
  113. private
  114. fnextfieldname: TIDString;
  115. function getcuroffset: asizeint;
  116. function getfieldoffset(l: longint): asizeint;
  117. procedure setnextfieldname(AValue: TIDString);
  118. protected
  119. { type of the aggregate }
  120. fdef: tdef;
  121. { type of the aggregate }
  122. ftyp: ttypedconstkind;
  123. { symtable entry of the previously emitted field in case of a
  124. record/object (nil if none emitted yet), used to insert alignment bytes
  125. if necessary for variant records and objects }
  126. fcurfield,
  127. { field corresponding to the data that will be emitted next in case of a
  128. record/object (nil if not set), used to handle variant records and
  129. objects }
  130. fnextfield: tfieldvarsym;
  131. { similar as the fcurfield/fnextfield above, but instead of fieldvarsyms
  132. these are indices in the symlist of a recorddef that correspond to
  133. fieldvarsyms. These are used only for non-variant records, simply
  134. traversing the fields in order. We could use the above method here as
  135. well, but to find the next field we'd always have to use
  136. symlist.indexof(fcurfield), which would be quite slow. These have -1 as
  137. value if they're not set }
  138. fcurindex,
  139. fnextindex: longint;
  140. { anonymous record that is being built as we add constant data }
  141. fanonrecord: boolean;
  142. property curindex: longint read fcurindex write fcurindex;
  143. property nextindex: longint read fnextindex write fnextindex;
  144. public
  145. constructor create(_def: tdef; _typ: ttypedconstkind); virtual;
  146. { calculated padding bytes for alignment if needed, and add the def of the
  147. next field in case we are constructing an anonymous record }
  148. function prepare_next_field(nextfielddef: tdef): asizeint; virtual;
  149. property def: tdef read fdef;
  150. property typ: ttypedconstkind read ftyp;
  151. property curfield: tfieldvarsym read fcurfield write fcurfield;
  152. property nextfield: tfieldvarsym read fnextfield write fnextfield;
  153. property nextfieldname: TIDString write setnextfieldname;
  154. property fieldoffset[l: longint]: asizeint read getfieldoffset;
  155. property curoffset: asizeint read getcuroffset;
  156. property anonrecord: boolean read fanonrecord write fanonrecord;
  157. end;
  158. taggregateinformationclass = class of taggregateinformation;
  159. { information about a placeholder element that has been added, and which has
  160. to be replaced later with a real data element }
  161. ttypedconstplaceholder = class abstract
  162. def: tdef;
  163. constructor create(d: tdef);
  164. procedure replace(ai: tai; d: tdef); virtual; abstract;
  165. end;
  166. { Warning: never directly create a ttai_typedconstbuilder instance,
  167. instead create a cai_typedconstbuilder (this class can be overridden) }
  168. ttai_typedconstbuilder = class abstract
  169. { class type to use when creating new aggregate information instances }
  170. protected class var
  171. caggregateinformation: taggregateinformationclass;
  172. private
  173. function getcurragginfo: taggregateinformation;
  174. procedure set_next_field(AValue: tfieldvarsym);
  175. procedure set_next_field_name(AValue: TIDString);
  176. protected
  177. { temporary list in which all data is collected }
  178. fasmlist: tasmlist;
  179. { options for the final asmlist }
  180. foptions: ttcasmlistoptions;
  181. { while queueing elements of a compound expression, this is the current
  182. offset in the top-level array/record }
  183. fqueue_offset: asizeint;
  184. fqueued_def: tdef;
  185. { array of caggregateinformation instances }
  186. faggregateinformation: tfpobjectlist;
  187. { Support for generating data that is only referenced from the typed
  188. constant data that we are currently generated. Such data can all be put
  189. in the same dead-strippable unit, as it's either all included or none of
  190. it is included. This data can be spread over multiple kinds of sections
  191. though (e.g. rodata and rodata_no_rel), so per section keep track whether
  192. we already started a dead-strippable unit and if so, what the section
  193. name was (so that on platforms that perform the dead stripping based on
  194. sections, we put all data for one typed constant into a single section
  195. with the same name) }
  196. protected type
  197. tinternal_data_section_info = record
  198. secname: TSymStr;
  199. sectype: TAsmSectiontype;
  200. end;
  201. protected var
  202. { all internally generated data must be stored in the same list, as it must
  203. be consecutive (if it's spread over multiple lists, we don't know in
  204. which order they'll be concatenated) -> keep track of this list }
  205. finternal_data_asmlist: tasmlist;
  206. { kind of the last section we started in the finternal_data_asmlist, to
  207. avoid creating unnecessary section statements }
  208. finternal_data_current_section: TAsmSectiontype;
  209. { info about in which kinds of sections we have already emitted internal
  210. data, and what their names were }
  211. finternal_data_section_info: array of tinternal_data_section_info;
  212. { ensure that finalize_asmlist is called only once }
  213. fasmlist_finalized: boolean;
  214. { ensure that if it's vectorized dead strippable data, we called
  215. finalize_vectorized_dead_strip_asmlist instead of finalize_asmlist }
  216. fvectorized_finalize_called: boolean;
  217. { returns whether def must be handled as an aggregate on the current
  218. platform }
  219. function aggregate_kind(def: tdef): ttypedconstkind; virtual;
  220. { finalize the asmlist: add the necessary symbols etc }
  221. procedure finalize_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions); virtual;
  222. { functionality of the above for vectorized dead strippable sections }
  223. procedure finalize_vectorized_dead_strip_asmlist(def: tdef; const basename, itemname: TSymStr; st: tsymtable; alignment: shortint; options: ttcasmlistoptions); virtual;
  224. { called by the public emit_tai() routines to actually add the typed
  225. constant data; the public ones also take care of adding extra padding
  226. bytes etc (by calling this one) }
  227. procedure do_emit_tai(p: tai; def: tdef); virtual;
  228. { calls prepare_next_field() and adds the padding bytes in the current
  229. location }
  230. procedure pad_next_field(nextfielddef: tdef);
  231. { returns the index in finternal_data_section_info of the info for the
  232. section of type typ. Returns -1 if there is no such info yet }
  233. function get_internal_data_section_index(typ: TAsmSectiontype): longint;
  234. { get a start label for an internal data section (at the start of a
  235. potentially dead-strippable part) }
  236. function get_internal_data_section_start_label: tasmlabel; virtual;
  237. { get a label in the middle of an internal data section (no dead
  238. stripping) }
  239. function get_internal_data_section_internal_label: tasmlabel; virtual;
  240. { easy access to the top level aggregate information instance }
  241. property curagginfo: taggregateinformation read getcurragginfo;
  242. public
  243. constructor create(const options: ttcasmlistoptions); virtual;
  244. destructor destroy; override;
  245. public
  246. { returns a builder for generating data that is only referrenced by the
  247. typed constant date we are currently generating (e.g. string data for a
  248. pchar constant). Also returns the label that will be placed at the start
  249. of that data. list is the tasmlist to which the data will be added.
  250. secname can be empty to use a default }
  251. procedure start_internal_data_builder(list: tasmlist; sectype: TAsmSectiontype; const secname: TSymStr; out tcb: ttai_typedconstbuilder; out l: tasmlabel);
  252. { finish a previously started internal data builder, including
  253. concatenating all generated data to the provided list and freeing the
  254. builder }
  255. procedure finish_internal_data_builder(var tcb: ttai_typedconstbuilder; l: tasmlabel; def: tdef; alignment: longint);
  256. { add a simple constant data element (p) to the typed constant.
  257. def is the type of the added value }
  258. procedure emit_tai(p: tai; def: tdef); virtual;
  259. { same as above, for a special case: when the def is a procvardef and we
  260. want to use it explicitly as a procdef (i.e., not as a record with a
  261. code and data pointer in case of a complex procvardef) }
  262. procedure emit_tai_procvar2procdef(p: tai; pvdef: tprocvardef); virtual;
  263. protected
  264. procedure maybe_emit_tail_padding(def: tdef); virtual;
  265. function emit_string_const_common(stringtype: tstringtype; len: asizeint; encoding: tstringencoding; var startlab: tasmlabel):tasmlabofs;
  266. procedure begin_aggregate_internal(def: tdef; anonymous: boolean); virtual;
  267. procedure end_aggregate_internal(def: tdef; anonymous: boolean); virtual;
  268. { when building an anonymous record, we cannot immediately insert the
  269. alignment before it in case it's nested, since we only know the required
  270. alignment once all fields have been inserted -> mark the location before
  271. the anonymous record, and insert the alignment once it's finished }
  272. procedure mark_anon_aggregate_alignment; virtual; abstract;
  273. procedure insert_marked_aggregate_alignment(def: tdef); virtual; abstract;
  274. class function get_vectorized_dead_strip_section_symbol(const basename: string; st: tsymtable; define, start: boolean): tasmsymbol; virtual;
  275. public
  276. class function get_vectorized_dead_strip_custom_section_name(const basename: TSymStr; st: tsymtable; out secname: TSymStr): boolean; virtual;
  277. { get the start/end symbol for a dead stripable vectorized section, such
  278. as the resourcestring data of a unit }
  279. class function get_vectorized_dead_strip_section_symbol_start(const basename: string; st: tsymtable; define: boolean): tasmsymbol; virtual;
  280. class function get_vectorized_dead_strip_section_symbol_end(const basename: string; st: tsymtable; define: boolean): tasmsymbol; virtual;
  281. class function get_dynstring_rec_name(typ: tstringtype; winlike: boolean; len: asizeint): string;
  282. { the datalist parameter specifies where the data for the string constant
  283. will be emitted (via an internal data builder) }
  284. function emit_ansistring_const(datalist: TAsmList; data: pchar; len: asizeint; encoding: tstringencoding): tasmlabofs;
  285. function emit_unicodestring_const(datalist: TAsmList; data: pointer; encoding: tstringencoding; winlike: boolean):tasmlabofs;
  286. { emits a tasmlabofs as returned by emit_*string_const }
  287. procedure emit_string_offset(const ll: tasmlabofs; const strlength: longint; const st: tstringtype; const winlikewidestring: boolean; const charptrdef: tdef);virtual;
  288. { emit a shortstring constant, and return its def }
  289. function emit_shortstring_const(const str: shortstring): tdef;
  290. { emit a guid constant }
  291. procedure emit_guid_const(const guid: tguid);
  292. { emit a procdef constant }
  293. procedure emit_procdef_const(pd: tprocdef);
  294. { emit an ordinal constant }
  295. procedure emit_ord_const(value: int64; def: tdef);
  296. { begin a potential aggregate type. Must be called for any type
  297. that consists of multiple tai constant data entries, or that
  298. represents an aggregate at the Pascal level (a record, a non-dynamic
  299. array, ... }
  300. procedure maybe_begin_aggregate(def: tdef);
  301. { end a potential aggregate type. Must be paired with every
  302. maybe_begin_aggregate }
  303. procedure maybe_end_aggregate(def: tdef);
  304. { similar as above, but in case
  305. a) it's definitely a record
  306. b) the def of the record should be automatically constructed based on
  307. the types of the emitted fields
  308. packrecords: same as "pacrecords x"
  309. recordalign: specify the (minimum) alignment of the start of the record
  310. (no equivalent in source code), used as an alternative for explicit
  311. align statements. Use "1" if it should be calculated based on the
  312. fields
  313. recordalignmin: same as "codealign recordmin=x"
  314. maxcrecordalign: specify maximum C record alignment (no equivalent in
  315. source code)
  316. }
  317. function begin_anonymous_record(const optionalname: string; packrecords, recordalign, recordalignmin, maxcrecordalign: shortint): trecorddef; virtual;
  318. function end_anonymous_record: trecorddef; virtual;
  319. { add a placeholder element at the current position that later can be
  320. filled in with the actual data (via ttypedconstplaceholder.replace)
  321. useful in case you have table preceded by the number of elements, and
  322. you cound the elements while building the table }
  323. function emit_placeholder(def: tdef): ttypedconstplaceholder; virtual; abstract;
  324. { common code to check whether a placeholder can be added at the current
  325. position }
  326. procedure check_add_placeholder(def: tdef);
  327. { The next group of routines are for constructing complex expressions.
  328. While parsing a typed constant these operators are encountered from
  329. outer to inner, so that is also the order in which they should be
  330. added to the queue. Only one queue can be active at a time. }
  331. { Init the queue. Gives an internalerror if a queue was already active }
  332. procedure queue_init(todef: tdef); virtual;
  333. { queue an array/string indexing operation (performs all range checking,
  334. so it doesn't have to be duplicated in all descendents). }
  335. procedure queue_vecn(def: tdef; const index: tconstexprint); virtual;
  336. { queue a subscripting operation }
  337. procedure queue_subscriptn(def: tabstractrecorddef; vs: tfieldvarsym); virtual;
  338. { queue indexing a record recursively via several field names. The fields
  339. are specified in the inner to outer order (i.e., def.field1.field2) }
  340. function queue_subscriptn_multiple_by_name(def: tabstractrecorddef; const fields: array of TIDString): tdef;
  341. { queue a type conversion operation }
  342. procedure queue_typeconvn(fromdef, todef: tdef); virtual;
  343. { finalise the queue (so a new one can be created) and flush the
  344. previously queued operations, applying them in reverse order on a...}
  345. { ... procdef }
  346. procedure queue_emit_proc(pd: tprocdef); virtual;
  347. { ... staticvarsym }
  348. procedure queue_emit_staticvar(vs: tstaticvarsym); virtual;
  349. { ... labelsym }
  350. procedure queue_emit_label(l: tlabelsym); virtual;
  351. { ... constsym }
  352. procedure queue_emit_const(cs: tconstsym); virtual;
  353. { ... asmsym/asmlabel }
  354. procedure queue_emit_asmsym(sym: tasmsymbol; def: tdef); virtual;
  355. { ... an ordinal constant }
  356. procedure queue_emit_ordconst(value: int64; def: tdef); virtual;
  357. protected
  358. { returns whether queue_init has been called without a corresponding
  359. queue_emit_* to finish it }
  360. function queue_is_active: boolean;
  361. public
  362. { finalize the internal asmlist (if necessary) and return it.
  363. This asmlist will be freed when the builder is destroyed, so add its
  364. contents to another list first. This property should only be accessed
  365. once all data has been added. }
  366. function get_final_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: longint): tasmlist;
  367. function get_final_asmlist_vectorized_dead_strip(def: tdef; const basename, itemname: TSymStr; st: TSymtable; alignment: longint): tasmlist;
  368. { returns the offset of the string data relative to ansi/unicode/widestring
  369. constant labels. On most platforms, this is 0 (with the header at a
  370. negative offset), but on some platforms such negative offsets are not
  371. supported this is equal to the header size }
  372. class function get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint; virtual;
  373. { set the fieldvarsym whose data we will emit next; needed
  374. in case of variant records, so we know which part of the variant gets
  375. initialised. Also in case of objects, because the fieldvarsyms are spread
  376. over the symtables of the entire inheritance tree }
  377. property next_field: tfieldvarsym write set_next_field;
  378. { set the name of the next field that will be emitted for an anonymous
  379. record (or the next of the next started anonymous record) }
  380. property next_field_name: TIDString write set_next_field_name;
  381. protected
  382. { this one always return the actual offset, called by the above (and
  383. overridden versions) }
  384. class function get_string_header_size(typ: tstringtype; winlikewidestring: boolean): pint;
  385. end;
  386. ttai_typedconstbuilderclass = class of ttai_typedconstbuilder;
  387. tlowlevelaggregateinformation = class(taggregateinformation)
  388. protected
  389. fanonrecmarker: tai;
  390. public
  391. property anonrecmarker: tai read fanonrecmarker write fanonrecmarker;
  392. end;
  393. tlowleveltypedconstplaceholder = class(ttypedconstplaceholder)
  394. list: tasmlist;
  395. insertpos: tai;
  396. constructor create(l: tasmlist; pos: tai; d: tdef);
  397. procedure replace(ai: tai; d: tdef); override;
  398. end;
  399. ttai_lowleveltypedconstbuilder = class(ttai_typedconstbuilder)
  400. protected
  401. procedure mark_anon_aggregate_alignment; override;
  402. procedure insert_marked_aggregate_alignment(def: tdef); override;
  403. procedure finalize_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions); override;
  404. public
  405. { set the default value for caggregateinformation (= tlowlevelaggregateinformation) }
  406. class constructor classcreate;
  407. function emit_placeholder(def: tdef): ttypedconstplaceholder; override;
  408. end;
  409. var
  410. ctai_typedconstbuilder: ttai_typedconstbuilderclass;
  411. implementation
  412. uses
  413. verbose,globals,systems,widestr,
  414. fmodule,
  415. symtable,defutil;
  416. {****************************************************************************
  417. taggregateinformation
  418. ****************************************************************************}
  419. function taggregateinformation.getcuroffset: asizeint;
  420. var
  421. field: tfieldvarsym;
  422. begin
  423. if assigned(curfield) then
  424. result:=curfield.fieldoffset+curfield.vardef.size
  425. else if curindex<>-1 then
  426. begin
  427. field:=tfieldvarsym(tabstractrecorddef(def).symtable.symlist[curindex]);
  428. result:=field.fieldoffset+field.vardef.size
  429. end
  430. else
  431. result:=0
  432. end;
  433. function taggregateinformation.getfieldoffset(l: longint): asizeint;
  434. var
  435. field: tfieldvarsym;
  436. begin
  437. field:=tfieldvarsym(tabstractrecorddef(def).symtable.symlist[l]);
  438. result:=field.fieldoffset;
  439. end;
  440. procedure taggregateinformation.setnextfieldname(AValue: TIDString);
  441. begin
  442. if (fnextfieldname<>'') or
  443. not anonrecord then
  444. internalerror(2015071503);
  445. fnextfieldname:=AValue;
  446. end;
  447. constructor taggregateinformation.create(_def: tdef; _typ: ttypedconstkind);
  448. begin
  449. fdef:=_def;
  450. ftyp:=_typ;
  451. fcurindex:=-1;
  452. fnextindex:=-1;
  453. end;
  454. function taggregateinformation.prepare_next_field(nextfielddef: tdef): asizeint;
  455. var
  456. currentoffset,nextoffset: asizeint;
  457. i: longint;
  458. begin
  459. { get the next field and its offset, and make that next field the current
  460. one }
  461. if assigned(nextfield) then
  462. begin
  463. nextoffset:=nextfield.fieldoffset;
  464. currentoffset:=curoffset;
  465. curfield:=nextfield;
  466. end
  467. else
  468. begin
  469. { must set nextfield for unions and objects, as we cannot
  470. automatically detect the "next" field in that case }
  471. if ((def.typ=recorddef) and
  472. trecorddef(def).isunion) or
  473. is_object(def) then
  474. internalerror(2014091202);
  475. { if we are constructing this record as data gets emitted, add a field
  476. for this data }
  477. if anonrecord then
  478. begin
  479. trecorddef(def).add_field_by_def(fnextfieldname,nextfielddef);
  480. fnextfieldname:='';
  481. end
  482. else if fnextfieldname<>'' then
  483. internalerror(2015071501);
  484. { find next field }
  485. i:=curindex;
  486. repeat
  487. inc(i);
  488. until (tsym(tabstractrecorddef(def).symtable.symlist[i]).typ=fieldvarsym) and
  489. not(sp_static in tsym(tabstractrecorddef(def).symtable.symlist[i]).symoptions);
  490. nextoffset:=fieldoffset[i];
  491. currentoffset:=curoffset;
  492. curindex:=i;
  493. end;
  494. { need padding? }
  495. result:=nextoffset-currentoffset;
  496. end;
  497. {****************************************************************************
  498. ttypedconstplaceholder
  499. ****************************************************************************}
  500. constructor ttypedconstplaceholder.create(d: tdef);
  501. begin
  502. def:=d;
  503. end;
  504. {****************************************************************************
  505. tai_abstracttypedconst
  506. ****************************************************************************}
  507. procedure tai_abstracttypedconst.setdef(def: tdef);
  508. begin
  509. { should not be changed, rewrite the calling code if this happens }
  510. if assigned(fdef) then
  511. Internalerror(2014080203);
  512. fdef:=def;
  513. end;
  514. constructor tai_abstracttypedconst.create(_adetyp: ttypedconstkind; _def: tdef);
  515. begin
  516. inherited create;
  517. typ:=ait_typedconst;
  518. fadetyp:=_adetyp;
  519. fdef:=_def;
  520. end;
  521. {****************************************************************************
  522. tai_simpletypedconst
  523. ****************************************************************************}
  524. procedure tai_simpletypedconst.setval(AValue: tai);
  525. begin
  526. fval:=AValue;
  527. end;
  528. constructor tai_simpletypedconst.create(_adetyp: ttypedconstkind; _def: tdef; _val: tai);
  529. begin
  530. inherited create(_adetyp,_def);
  531. fval:=_val;
  532. end;
  533. {****************************************************************************
  534. tai_aggregatetypedconst.tadeenumerator
  535. ****************************************************************************}
  536. constructor tai_aggregatetypedconst.tadeenumerator.create(data: tai_aggregatetypedconst);
  537. begin
  538. fvalues:=data.fvalues;
  539. fvaluespos:=-1;
  540. end;
  541. function tai_aggregatetypedconst.tadeenumerator.getcurrent: tai_abstracttypedconst;
  542. begin
  543. result:=tai_abstracttypedconst(fvalues[fvaluespos]);
  544. end;
  545. function tai_aggregatetypedconst.tadeenumerator.movenext: boolean;
  546. begin
  547. if fvaluespos<pred(fvalues.count) then
  548. begin
  549. inc(fvaluespos);
  550. result:=true
  551. end
  552. else
  553. result:=false;
  554. end;
  555. procedure tai_aggregatetypedconst.tadeenumerator.reset;
  556. begin
  557. fvaluespos:=0
  558. end;
  559. {****************************************************************************
  560. tai_aggregatetypedconst
  561. ****************************************************************************}
  562. procedure tai_aggregatetypedconst.convert_to_string;
  563. var
  564. ai: tai_abstracttypedconst;
  565. newstr: tai_string;
  566. begin
  567. newstr:=tai_string.Create('');
  568. for ai in self do
  569. begin
  570. if ai.adetyp<>tck_simple then
  571. internalerror(2014070103);
  572. add_to_string(newstr,tai_simpletypedconst(ai).val);
  573. ai.free;
  574. end;
  575. fvalues.count:=0;
  576. { the "nil" def will be replaced with an array def of the appropriate
  577. size once we're finished adding data, so we don't create intermediate
  578. arraydefs all the time }
  579. fvalues.add(tai_simpletypedconst.create(tck_simple,nil,newstr));
  580. end;
  581. procedure tai_aggregatetypedconst.add_to_string(strtai: tai_string; othertai: tai);
  582. begin
  583. case othertai.typ of
  584. ait_string:
  585. begin
  586. strtai.str:=reallocmem(strtai.str,strtai.len+tai_string(othertai).len+1);
  587. { also copy null terminator }
  588. move(tai_string(othertai).str[0],strtai.str[strtai.len],tai_string(othertai).len+1);
  589. { the null terminator is not part of the string data }
  590. strtai.len:=strtai.len+tai_string(othertai).len;
  591. end;
  592. ait_const:
  593. begin
  594. if tai_const(othertai).size<>1 then
  595. internalerror(2014070101);
  596. strtai.str:=reallocmem(strtai.str,strtai.len+1);
  597. strtai.str[strtai.len]:=ansichar(tai_const(othertai).value);
  598. strtai.str[strtai.len+1]:=#0;
  599. inc(strtai.len);
  600. end;
  601. else
  602. internalerror(2014070102);
  603. end;
  604. end;
  605. constructor tai_aggregatetypedconst.create(_adetyp: ttypedconstkind; _fdef: tdef);
  606. begin
  607. inherited;
  608. fisstring:=false;
  609. fvalues:=tfplist.create;
  610. end;
  611. function tai_aggregatetypedconst.getenumerator: tadeenumerator;
  612. begin
  613. result:=tadeenumerator.create(self);
  614. end;
  615. procedure tai_aggregatetypedconst.addvalue(val: tai_abstracttypedconst);
  616. begin
  617. { merge string constants and ordinal constants added in an array of
  618. char, to unify the length and the string data }
  619. if fisstring or
  620. ((val.adetyp=tck_simple) and
  621. (tai_simpletypedconst(val).val.typ=ait_string)) then
  622. begin
  623. if not fisstring and
  624. (fvalues.count>0) then
  625. convert_to_string;
  626. fisstring:=true;
  627. case fvalues.count of
  628. 0: fvalues.add(val);
  629. 1:
  630. begin
  631. add_to_string(tai_string(tai_simpletypedconst(fvalues[0]).val),tai_simpletypedconst(val).val);
  632. val.free
  633. end
  634. else
  635. internalerror(2014070104);
  636. end;
  637. end
  638. else
  639. fvalues.add(val);
  640. end;
  641. function tai_aggregatetypedconst.valuecount: longint;
  642. begin
  643. result:=fvalues.count;
  644. end;
  645. procedure tai_aggregatetypedconst.insertvaluebeforepos(val: tai_abstracttypedconst; pos: longint);
  646. begin
  647. fvalues.insert(pos,val);
  648. end;
  649. function tai_aggregatetypedconst.replacevalueatpos(val: tai_abstracttypedconst; pos: longint): tai_abstracttypedconst;
  650. begin
  651. result:=tai_abstracttypedconst(fvalues[pos]);
  652. fvalues[pos]:=val;
  653. end;
  654. procedure tai_aggregatetypedconst.finish;
  655. begin
  656. if fisstring then
  657. begin
  658. { set the def: an array of char with the same length as the string
  659. data }
  660. if fvalues.count<>1 then
  661. internalerror(2014070105);
  662. tai_simpletypedconst(fvalues[0]).fdef:=
  663. carraydef.getreusable(cansichartype,
  664. tai_string(tai_simpletypedconst(fvalues[0]).val).len);
  665. end;
  666. end;
  667. destructor tai_aggregatetypedconst.destroy;
  668. begin
  669. fvalues.free;
  670. inherited destroy;
  671. end;
  672. {*****************************************************************************
  673. ttai_typedconstbuilder
  674. *****************************************************************************}
  675. function ttai_typedconstbuilder.getcurragginfo: taggregateinformation;
  676. begin
  677. if assigned(faggregateinformation) and
  678. (faggregateinformation.count>0) then
  679. result:=taggregateinformation(faggregateinformation[faggregateinformation.count-1])
  680. else
  681. result:=nil;
  682. end;
  683. procedure ttai_typedconstbuilder.set_next_field(AValue: tfieldvarsym);
  684. var
  685. info: taggregateinformation;
  686. begin
  687. info:=curagginfo;
  688. if not assigned(info) then
  689. internalerror(2014091206);
  690. info.nextfield:=AValue;
  691. end;
  692. procedure ttai_typedconstbuilder.set_next_field_name(AValue: TIDString);
  693. var
  694. info: taggregateinformation;
  695. begin
  696. info:=curagginfo;
  697. if not assigned(info) then
  698. internalerror(2015071502);
  699. info.nextfieldname:='$'+AValue;
  700. end;
  701. procedure ttai_typedconstbuilder.pad_next_field(nextfielddef: tdef);
  702. var
  703. fillbytes: asizeint;
  704. begin
  705. fillbytes:=curagginfo.prepare_next_field(nextfielddef);
  706. while fillbytes>0 do
  707. begin
  708. do_emit_tai(tai_const.create_8bit(0),u8inttype);
  709. dec(fillbytes);
  710. end;
  711. end;
  712. function ttai_typedconstbuilder.get_internal_data_section_index(typ: TAsmSectiontype): longint;
  713. begin
  714. { avoid wrong warning by -Oodfa }
  715. result:=-1;
  716. for result:=low(finternal_data_section_info) to high(finternal_data_section_info) do
  717. if finternal_data_section_info[result].sectype=typ then
  718. exit;
  719. result:=-1;
  720. end;
  721. function ttai_typedconstbuilder.get_internal_data_section_start_label: tasmlabel;
  722. begin
  723. { on Darwin, dead code/data stripping happens based on non-temporary
  724. labels (any label that doesn't start with "L" -- it doesn't have
  725. to be global) }
  726. if target_info.system in systems_darwin then
  727. current_asmdata.getstaticdatalabel(result)
  728. else if create_smartlink_library then
  729. current_asmdata.getglobaldatalabel(result)
  730. else
  731. current_asmdata.getlocaldatalabel(result);
  732. end;
  733. function ttai_typedconstbuilder.get_internal_data_section_internal_label: tasmlabel;
  734. begin
  735. if create_smartlink_library then
  736. { all labels need to be global in case they're in another object }
  737. current_asmdata.getglobaldatalabel(result)
  738. else
  739. { no special requirement for the label -> just get a local one }
  740. current_asmdata.getlocaldatalabel(result);
  741. end;
  742. function ttai_typedconstbuilder.aggregate_kind(def: tdef): ttypedconstkind;
  743. begin
  744. if (def.typ in [recorddef,filedef,variantdef]) or
  745. is_object(def) or
  746. ((def.typ=procvardef) and
  747. not tprocvardef(def).is_addressonly) then
  748. result:=tck_record
  749. else if ((def.typ=arraydef) and
  750. not is_dynamic_array(def)) or
  751. ((def.typ=setdef) and
  752. not is_smallset(def)) or
  753. is_shortstring(def) then
  754. result:=tck_array
  755. else
  756. result:=tck_simple;
  757. end;
  758. procedure ttai_typedconstbuilder.finalize_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions);
  759. var
  760. prelist: tasmlist;
  761. begin
  762. { have we finished all aggregates? }
  763. if (getcurragginfo<>nil) and
  764. { in case of syntax errors, the aggregate may not have been finished }
  765. (ErrorCount=0) then
  766. internalerror(2015072301);
  767. { must call finalize_vectorized_dead_strip_asmlist() instead }
  768. if (([tcalo_vectorized_dead_strip_start,
  769. tcalo_vectorized_dead_strip_item,
  770. tcalo_vectorized_dead_strip_end]*options)<>[]) and
  771. not fvectorized_finalize_called then
  772. internalerror(2015110602);
  773. prelist:=tasmlist.create;
  774. { only now add items based on the symbolname, because it may be
  775. modified by the "section" specifier in case of a typed constant }
  776. { both in case the data should be dead strippable and never dead
  777. stripped, it should be in a separate section (so this property doesn't
  778. affect other data) }
  779. if ([tcalo_no_dead_strip,tcalo_make_dead_strippable]*options)<>[] then
  780. begin
  781. maybe_new_object_file(prelist);
  782. { we always need a new section here, since if we started a new
  783. object file then we have to say what the section is, and otherwise
  784. we need a new section because that's how the dead stripping works }
  785. new_section(prelist,section,secname,const_align(alignment));
  786. end
  787. else if tcalo_new_section in options then
  788. new_section(prelist,section,secname,const_align(alignment))
  789. else
  790. prelist.concat(cai_align.Create(const_align(alignment)));
  791. { On Darwin, use .reference to ensure the data doesn't get dead stripped.
  792. On other platforms, the data must be in the .fpc section (which is
  793. kept via the linker script) }
  794. if tcalo_no_dead_strip in options then
  795. begin
  796. if target_info.system in systems_darwin then
  797. prelist.concat(tai_directive.Create(asd_reference,sym.name))
  798. else if section<>sec_fpc then
  799. internalerror(2015101402);
  800. end;
  801. if not(tcalo_is_lab in options) then
  802. if sym.bind=AB_GLOBAL then
  803. prelist.concat(tai_symbol.Create_Global(sym,0))
  804. else
  805. prelist.concat(tai_symbol.Create(sym,0))
  806. else
  807. prelist.concat(tai_label.Create(tasmlabel(sym)));
  808. { insert the symbol information before the data }
  809. fasmlist.insertlist(prelist);
  810. { end of the symbol }
  811. fasmlist.concat(tai_symbol_end.Createname(sym.name));
  812. { free the temporary list }
  813. prelist.free;
  814. end;
  815. procedure ttai_typedconstbuilder.finalize_vectorized_dead_strip_asmlist(def: tdef; const basename, itemname: TSymStr; st: tsymtable; alignment: shortint; options: ttcasmlistoptions);
  816. var
  817. sym: tasmsymbol;
  818. secname: TSymStr;
  819. sectype: TAsmSectiontype;
  820. customsecname: boolean;
  821. begin
  822. fvectorized_finalize_called:=true;
  823. sym:=nil;
  824. customsecname:=get_vectorized_dead_strip_custom_section_name(basename,st,secname);
  825. if customsecname then
  826. sectype:=sec_user
  827. else
  828. sectype:=sec_data;
  829. if tcalo_vectorized_dead_strip_start in options then
  830. begin
  831. { the start and end names are predefined }
  832. if itemname<>'' then
  833. internalerror(2015110801);
  834. sym:=get_vectorized_dead_strip_section_symbol_start(basename,st,true);
  835. if not customsecname then
  836. secname:=make_mangledname(basename,st,'1_START');
  837. end
  838. else if tcalo_vectorized_dead_strip_end in options then
  839. begin
  840. { the start and end names are predefined }
  841. if itemname<>'' then
  842. internalerror(2015110802);
  843. sym:=get_vectorized_dead_strip_section_symbol_end(basename,st,true);
  844. if not customsecname then
  845. secname:=make_mangledname(basename,st,'3_END');
  846. end
  847. else if tcalo_vectorized_dead_strip_item in options then
  848. begin
  849. sym:=current_asmdata.DefineAsmSymbol(make_mangledname(basename,st,itemname),AB_GLOBAL,AT_DATA);
  850. if not customsecname then
  851. secname:=make_mangledname(basename,st,'2_'+itemname);
  852. exclude(options,tcalo_vectorized_dead_strip_item);
  853. end;
  854. finalize_asmlist(sym,def,sectype,secname,alignment,options);
  855. end;
  856. procedure ttai_typedconstbuilder.do_emit_tai(p: tai; def: tdef);
  857. begin
  858. { by default we don't care about the type }
  859. fasmlist.concat(p);
  860. end;
  861. function ttai_typedconstbuilder.get_final_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: longint): tasmlist;
  862. begin
  863. if not fasmlist_finalized then
  864. begin
  865. finalize_asmlist(sym,def,section,secname,alignment,foptions);
  866. fasmlist_finalized:=true;
  867. end;
  868. result:=fasmlist;
  869. end;
  870. function ttai_typedconstbuilder.get_final_asmlist_vectorized_dead_strip(def: tdef; const basename, itemname: TSymStr; st: TSymtable; alignment: longint): tasmlist;
  871. begin
  872. if not fasmlist_finalized then
  873. begin
  874. finalize_vectorized_dead_strip_asmlist(def,basename,itemname,st,alignment,foptions);
  875. fasmlist_finalized:=true;
  876. end;
  877. result:=fasmlist;
  878. end;
  879. class function ttai_typedconstbuilder.get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint;
  880. begin
  881. { darwin's linker does not support negative offsets }
  882. if not(target_info.system in systems_darwin) then
  883. result:=0
  884. else
  885. result:=get_string_header_size(typ,winlikewidestring);
  886. end;
  887. class function ttai_typedconstbuilder.get_string_header_size(typ: tstringtype; winlikewidestring: boolean): pint;
  888. const
  889. ansistring_header_size =
  890. { encoding }
  891. 2 +
  892. { elesize }
  893. 2 +
  894. {$ifdef cpu64bitaddr}
  895. { alignment }
  896. 4 +
  897. {$endif cpu64bitaddr}
  898. { reference count }
  899. sizeof(pint) +
  900. { length }
  901. sizeof(pint);
  902. unicodestring_header_size = ansistring_header_size;
  903. begin
  904. case typ of
  905. st_ansistring:
  906. result:=ansistring_header_size;
  907. st_unicodestring:
  908. result:=unicodestring_header_size;
  909. st_widestring:
  910. if winlikewidestring then
  911. result:=0
  912. else
  913. result:=unicodestring_header_size;
  914. else
  915. result:=0;
  916. end;
  917. end;
  918. constructor ttai_typedconstbuilder.create(const options: ttcasmlistoptions);
  919. begin
  920. inherited create;
  921. fasmlist:=tasmlist.create;
  922. foptions:=options;
  923. { queue is empty }
  924. fqueue_offset:=low(fqueue_offset);
  925. finternal_data_current_section:=sec_none;
  926. end;
  927. destructor ttai_typedconstbuilder.destroy;
  928. begin
  929. { the queue should have been flushed if it was used }
  930. if fqueue_offset<>low(fqueue_offset) then
  931. internalerror(2014062901);
  932. faggregateinformation.free;
  933. fasmlist.free;
  934. inherited destroy;
  935. end;
  936. procedure ttai_typedconstbuilder.start_internal_data_builder(list: tasmlist; sectype: TAsmSectiontype; const secname: TSymStr; out tcb: ttai_typedconstbuilder; out l: tasmlabel);
  937. var
  938. options: ttcasmlistoptions;
  939. foundsec: longint;
  940. begin
  941. options:=[tcalo_is_lab];
  942. { Add a section header if the previous one was different. We'll use the
  943. same section name in case multiple items are added to the same kind of
  944. section (rodata, rodata_no_rel, ...), so that everything will still
  945. end up in the same section even if there are multiple section headers }
  946. if finternal_data_current_section<>sectype then
  947. include(options,tcalo_new_section);
  948. finternal_data_current_section:=sectype;
  949. l:=nil;
  950. { did we already create a section of this type for the internal data of
  951. this builder? }
  952. foundsec:=get_internal_data_section_index(sectype);
  953. if foundsec=-1 then
  954. begin
  955. { we only need to start a dead-strippable section of data at the
  956. start of the first subsection of this kind for this block.
  957. exception: if dead stripping happens based on objects/libraries,
  958. then we only have to create a new object file for the first
  959. internal data section of any kind (all the rest will simply be put
  960. in the same object file) }
  961. if create_smartlink then
  962. begin
  963. if not create_smartlink_library or
  964. (length(finternal_data_section_info)=0) then
  965. include(options,tcalo_make_dead_strippable);
  966. { on Darwin, dead code/data stripping happens based on non-
  967. temporary labels (any label that doesn't start with "L" -- it
  968. doesn't have to be global) -> add a non-temporary lobel at the
  969. start of every kind of subsection created in this builder }
  970. if target_info.system in systems_darwin then
  971. l:=get_internal_data_section_start_label;
  972. end;
  973. foundsec:=length(finternal_data_section_info);
  974. setlength(finternal_data_section_info,foundsec+1);
  975. finternal_data_section_info[foundsec].sectype:=sectype;
  976. end;
  977. if not assigned(finternal_data_asmlist) and
  978. (cs_create_smart in current_settings.moduleswitches) then
  979. begin
  980. l:=get_internal_data_section_start_label;
  981. { the internal data list should only be assigned by this routine,
  982. the first time that an internal data block is started }
  983. if not assigned(list) or
  984. assigned(finternal_data_asmlist) then
  985. internalerror(2015032101);
  986. finternal_data_asmlist:=list;
  987. end
  988. { all internal data for this tcb must go to the same list (otherwise all
  989. data we want to add to the dead-strippable block is not guaranteed to
  990. be sequential and e.g. in the same object file in case of library-based
  991. dead stripping) }
  992. else if (assigned(finternal_data_asmlist) and
  993. (list<>finternal_data_asmlist)) or
  994. not assigned(list) then
  995. internalerror(2015032101);
  996. finternal_data_asmlist:=list;
  997. if not assigned(l) then
  998. l:=get_internal_data_section_internal_label;
  999. { first section of this kind -> set name }
  1000. if finternal_data_section_info[foundsec].secname='' then
  1001. if secname='' then
  1002. finternal_data_section_info[foundsec].secname:=l.Name
  1003. else
  1004. finternal_data_section_info[foundsec].secname:=secname
  1005. { if the name is specified multiple times, it must match }
  1006. else if (secname<>'') and
  1007. (finternal_data_section_info[foundsec].secname<>secname) then
  1008. internalerror(2015032401);
  1009. tcb:=ttai_typedconstbuilderclass(classtype).create(options);
  1010. end;
  1011. procedure ttai_typedconstbuilder.finish_internal_data_builder(var tcb: ttai_typedconstbuilder; l: tasmlabel; def: tdef; alignment: longint);
  1012. begin
  1013. finternal_data_asmlist.concatList(tcb.get_final_asmlist(l,def,
  1014. finternal_data_current_section,
  1015. finternal_data_section_info[get_internal_data_section_index(finternal_data_current_section)].secname,
  1016. alignment));
  1017. tcb.free;
  1018. tcb:=nil;
  1019. end;
  1020. procedure ttai_typedconstbuilder.emit_tai(p: tai; def: tdef);
  1021. var
  1022. kind: ttypedconstkind;
  1023. info: taggregateinformation;
  1024. begin
  1025. { these elements can be aggregates themselves, e.g. a shortstring can
  1026. be emitted as a series of bytes and char arrays }
  1027. kind:=aggregate_kind(def);
  1028. info:=curagginfo;
  1029. if (kind<>tck_simple) and
  1030. (not assigned(info) or
  1031. (info.typ<>kind)) then
  1032. internalerror(2014091001);
  1033. { if we're emitting a record, handle the padding bytes, and in case of
  1034. an anonymous record also add the next field }
  1035. if assigned(info) then
  1036. begin
  1037. { queue_init already adds padding }
  1038. if not queue_is_active and
  1039. (is_record(info.def) or
  1040. is_object(info.def)) and
  1041. { may add support for these later }
  1042. not is_packed_record_or_object(info.def) then
  1043. pad_next_field(def);
  1044. end;
  1045. { emit the data }
  1046. do_emit_tai(p,def);
  1047. end;
  1048. procedure ttai_typedconstbuilder.emit_tai_procvar2procdef(p: tai; pvdef: tprocvardef);
  1049. begin
  1050. { nothing special by default, since we don't care about the type }
  1051. emit_tai(p,pvdef);
  1052. end;
  1053. procedure ttai_typedconstbuilder.maybe_emit_tail_padding(def: tdef);
  1054. var
  1055. info: taggregateinformation;
  1056. fillbytes: asizeint;
  1057. begin
  1058. info:=curagginfo;
  1059. if not assigned(info) then
  1060. internalerror(2014091002);
  1061. if def<>info.def then
  1062. internalerror(2014091205);
  1063. if (is_record(def) or
  1064. is_object(def)) and
  1065. not is_packed_record_or_object(def) then
  1066. begin
  1067. fillbytes:=def.size-info.curoffset;
  1068. while fillbytes>0 do
  1069. begin
  1070. do_emit_tai(Tai_const.Create_8bit(0),u8inttype);
  1071. dec(fillbytes)
  1072. end;
  1073. end;
  1074. end;
  1075. function ttai_typedconstbuilder.emit_string_const_common(stringtype: tstringtype; len: asizeint; encoding: tstringencoding; var startlab: tasmlabel): tasmlabofs;
  1076. var
  1077. string_symofs: asizeint;
  1078. charptrdef: tdef;
  1079. elesize: word;
  1080. begin
  1081. result.lab:=startlab;
  1082. result.ofs:=0;
  1083. { pack the data, so that we don't add unnecessary null bytes after the
  1084. constant string }
  1085. begin_anonymous_record('$'+get_dynstring_rec_name(stringtype,false,len),1,sizeof(TConstPtrUInt),1,1);
  1086. string_symofs:=get_string_symofs(stringtype,false);
  1087. { encoding }
  1088. emit_tai(tai_const.create_16bit(encoding),u16inttype);
  1089. inc(result.ofs,2);
  1090. { element size }
  1091. case stringtype of
  1092. st_ansistring:
  1093. begin
  1094. elesize:=1;
  1095. charptrdef:=charpointertype;
  1096. end;
  1097. st_unicodestring:
  1098. begin
  1099. elesize:=2;
  1100. charptrdef:=widecharpointertype;
  1101. end
  1102. else
  1103. internalerror(2014080401);
  1104. end;
  1105. emit_tai(tai_const.create_16bit(elesize),u16inttype);
  1106. inc(result.ofs,2);
  1107. {$ifdef cpu64bitaddr}
  1108. { dummy for alignment }
  1109. emit_tai(tai_const.create_32bit(0),u32inttype);
  1110. inc(result.ofs,4);
  1111. {$endif cpu64bitaddr}
  1112. emit_tai(tai_const.create_pint(-1),ptrsinttype);
  1113. inc(result.ofs,sizeof(pint));
  1114. emit_tai(tai_const.create_pint(len),ptrsinttype);
  1115. inc(result.ofs,sizeof(pint));
  1116. if string_symofs=0 then
  1117. begin
  1118. { results in slightly more efficient code }
  1119. emit_tai(tai_label.create(result.lab),charptrdef);
  1120. result.ofs:=0;
  1121. { create new label of the same kind (including whether or not the
  1122. name starts with target_asm.labelprefix in case it's AB_LOCAL,
  1123. so we keep the difference depending on whether the original was
  1124. allocated via getstatic/getlocal/getglobal datalabel) }
  1125. startlab:=tasmlabel.create(current_asmdata.AsmSymbolDict,startlab.name+'$strlab',startlab.bind,startlab.typ);
  1126. end;
  1127. { sanity check }
  1128. if result.ofs<>string_symofs then
  1129. internalerror(2012051701);
  1130. end;
  1131. procedure ttai_typedconstbuilder.begin_aggregate_internal(def: tdef; anonymous: boolean);
  1132. var
  1133. info: taggregateinformation;
  1134. tck: ttypedconstkind;
  1135. begin
  1136. tck:=aggregate_kind(def);
  1137. if tck=tck_simple then
  1138. exit;
  1139. if not assigned(faggregateinformation) then
  1140. faggregateinformation:=tfpobjectlist.create
  1141. { if we're starting an anonymous record, we can't align it yet because
  1142. the alignment depends on the fields that will be added -> we'll do
  1143. it at the end }
  1144. else if not anonymous then
  1145. begin
  1146. { add padding if necessary, and update the current field/offset }
  1147. info:=curagginfo;
  1148. if (is_record(curagginfo.def) or
  1149. is_object(curagginfo.def)) and
  1150. not is_packed_record_or_object(curagginfo.def) then
  1151. begin
  1152. if queue_is_active then
  1153. internalerror(2015073001);
  1154. pad_next_field(def);
  1155. end;
  1156. end
  1157. { if this is the outer record, no padding is required; the alignment
  1158. has to be specified explicitly in that case via get_final_asmlist() }
  1159. else if assigned(curagginfo) and
  1160. (curagginfo.def.typ=recorddef) then
  1161. { mark where we'll have to insert the padding bytes at the end }
  1162. mark_anon_aggregate_alignment;
  1163. info:=caggregateinformation.create(def,aggregate_kind(def));
  1164. faggregateinformation.add(info);
  1165. end;
  1166. procedure ttai_typedconstbuilder.end_aggregate_internal(def: tdef; anonymous: boolean);
  1167. var
  1168. info: taggregateinformation;
  1169. tck: ttypedconstkind;
  1170. begin
  1171. tck:=aggregate_kind(def);
  1172. if tck=tck_simple then
  1173. exit;
  1174. { add tail padding if necessary }
  1175. maybe_emit_tail_padding(def);
  1176. { pop and free the information }
  1177. info:=curagginfo;
  1178. faggregateinformation.count:=faggregateinformation.count-1;
  1179. info.free;
  1180. end;
  1181. class function ttai_typedconstbuilder.get_vectorized_dead_strip_section_symbol(const basename: string; st: tsymtable; define, start: boolean): tasmsymbol;
  1182. var
  1183. name: TSymStr;
  1184. begin
  1185. if start then
  1186. name:=make_mangledname(basename,st,'START')
  1187. else
  1188. name:=make_mangledname(basename,st,'END');
  1189. if define then
  1190. result:=current_asmdata.DefineAsmSymbol(name,AB_GLOBAL,AT_DATA)
  1191. else
  1192. result:=current_asmdata.RefAsmSymbol(name,AT_DATA)
  1193. end;
  1194. class function ttai_typedconstbuilder.get_vectorized_dead_strip_custom_section_name(const basename: TSymStr; st: tsymtable; out secname: TSymStr): boolean;
  1195. begin
  1196. result:=false;
  1197. end;
  1198. class function ttai_typedconstbuilder.get_vectorized_dead_strip_section_symbol_start(const basename: string; st: tsymtable; define: boolean): tasmsymbol;
  1199. begin
  1200. result:=get_vectorized_dead_strip_section_symbol(basename,st,define,true);
  1201. end;
  1202. class function ttai_typedconstbuilder.get_vectorized_dead_strip_section_symbol_end(const basename: string; st: tsymtable; define: boolean): tasmsymbol;
  1203. begin
  1204. result:=get_vectorized_dead_strip_section_symbol(basename,st,define,false);
  1205. end;
  1206. class function ttai_typedconstbuilder.get_dynstring_rec_name(typ: tstringtype; winlike: boolean; len: asizeint): string;
  1207. begin
  1208. case typ of
  1209. st_ansistring:
  1210. result:='ansistrrec';
  1211. st_unicodestring,
  1212. st_widestring:
  1213. if (typ=st_unicodestring) or
  1214. not winlike then
  1215. result:='unicodestrrec'
  1216. else
  1217. result:='widestrrec';
  1218. else
  1219. internalerror(2014080402);
  1220. end;
  1221. result:=result+tostr(len);
  1222. end;
  1223. function ttai_typedconstbuilder.emit_ansistring_const(datalist: TAsmList; data: pchar; len: asizeint; encoding: tstringencoding): tasmlabofs;
  1224. var
  1225. s: PChar;
  1226. startlab: tasmlabel;
  1227. ansistrrecdef: trecorddef;
  1228. datadef: tdef;
  1229. datatcb: ttai_typedconstbuilder;
  1230. begin
  1231. start_internal_data_builder(datalist,sec_rodata_norel,'',datatcb,startlab);
  1232. result:=datatcb.emit_string_const_common(st_ansistring,len,encoding,startlab);
  1233. getmem(s,len+1);
  1234. move(data^,s^,len);
  1235. s[len]:=#0;
  1236. { terminating zero included }
  1237. datadef:=carraydef.getreusable(cansichartype,len+1);
  1238. datatcb.maybe_begin_aggregate(datadef);
  1239. datatcb.emit_tai(tai_string.create_pchar(s,len+1),datadef);
  1240. datatcb.maybe_end_aggregate(datadef);
  1241. ansistrrecdef:=datatcb.end_anonymous_record;
  1242. finish_internal_data_builder(datatcb,startlab,ansistrrecdef,const_align(sizeof(pointer)));
  1243. end;
  1244. function ttai_typedconstbuilder.emit_unicodestring_const(datalist: TAsmList; data: pointer; encoding: tstringencoding; winlike: boolean):tasmlabofs;
  1245. var
  1246. i, strlength: longint;
  1247. string_symofs: asizeint;
  1248. startlab: tasmlabel;
  1249. datadef: tdef;
  1250. datatcb: ttai_typedconstbuilder;
  1251. unicodestrrecdef: trecorddef;
  1252. begin
  1253. start_internal_data_builder(datalist,sec_rodata_norel,'',datatcb,startlab);
  1254. strlength:=getlengthwidestring(pcompilerwidestring(data));
  1255. if winlike then
  1256. begin
  1257. result.lab:=startlab;
  1258. datatcb.begin_anonymous_record('$'+get_dynstring_rec_name(st_widestring,true,strlength),
  1259. 4,4,
  1260. targetinfos[target_info.system]^.alignment.recordalignmin,
  1261. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1262. datatcb.emit_tai(Tai_const.Create_32bit(strlength*cwidechartype.size),s32inttype);
  1263. { can we optimise by placing the string constant label at the
  1264. required offset? }
  1265. string_symofs:=get_string_symofs(st_widestring,true);
  1266. if string_symofs=0 then
  1267. begin
  1268. { yes }
  1269. datatcb.emit_tai(Tai_label.Create(result.lab),widecharpointertype);
  1270. { allocate a separate label for the start of the data (see
  1271. emit_string_const_common() for explanation) }
  1272. startlab:=tasmlabel.create(current_asmdata.AsmSymbolDict,startlab.name+'$strlab',startlab.bind,startlab.typ);
  1273. end
  1274. else
  1275. internalerror(2015031502);
  1276. result.ofs:=string_symofs;
  1277. end
  1278. else
  1279. begin
  1280. result:=datatcb.emit_string_const_common(st_unicodestring,strlength,encoding,startlab);
  1281. end;
  1282. if cwidechartype.size = 2 then
  1283. begin
  1284. datadef:=carraydef.getreusable(cwidechartype,strlength+1);
  1285. datatcb.maybe_begin_aggregate(datadef);
  1286. for i:=0 to strlength-1 do
  1287. datatcb.emit_tai(Tai_const.Create_16bit(pcompilerwidestring(data)^.data[i]),cwidechartype);
  1288. { ending #0 }
  1289. datatcb.emit_tai(Tai_const.Create_16bit(0),cwidechartype);
  1290. datatcb.maybe_end_aggregate(datadef);
  1291. unicodestrrecdef:=datatcb.end_anonymous_record;
  1292. end
  1293. else
  1294. { code generation for other sizes must be written }
  1295. internalerror(200904271);
  1296. finish_internal_data_builder(datatcb,startlab,unicodestrrecdef,const_align(sizeof(pint)));
  1297. end;
  1298. procedure ttai_typedconstbuilder.emit_string_offset(const ll: tasmlabofs; const strlength: longint; const st: tstringtype; const winlikewidestring: boolean; const charptrdef: tdef);
  1299. begin
  1300. emit_tai(Tai_const.Create_sym_offset(ll.lab,ll.ofs),charptrdef);
  1301. end;
  1302. function ttai_typedconstbuilder.emit_shortstring_const(const str: shortstring): tdef;
  1303. begin
  1304. { we use an arraydef instead of a shortstringdef, because we don't have
  1305. functionality in place yet to reuse shortstringdefs of the same length
  1306. and neither the lowlevel nor the llvm typedconst builder cares about
  1307. this difference }
  1308. result:=carraydef.getreusable(cansichartype,length(str)+1);
  1309. maybe_begin_aggregate(result);
  1310. emit_tai(Tai_const.Create_8bit(length(str)),u8inttype);
  1311. if str<>'' then
  1312. emit_tai(Tai_string.Create(str),carraydef.getreusable(cansichartype,length(str)));
  1313. maybe_end_aggregate(result);
  1314. end;
  1315. procedure ttai_typedconstbuilder.emit_guid_const(const guid: tguid);
  1316. var
  1317. i: longint;
  1318. begin
  1319. maybe_begin_aggregate(rec_tguid);
  1320. { variant record -> must specify which fields get initialised }
  1321. next_field:=tfieldvarsym(rec_tguid.symtable.symlist[0]);
  1322. emit_tai(Tai_const.Create_32bit(longint(guid.D1)),u32inttype);
  1323. next_field:=tfieldvarsym(rec_tguid.symtable.symlist[1]);
  1324. emit_tai(Tai_const.Create_16bit(guid.D2),u16inttype);
  1325. next_field:=tfieldvarsym(rec_tguid.symtable.symlist[2]);
  1326. emit_tai(Tai_const.Create_16bit(guid.D3),u16inttype);
  1327. next_field:=tfieldvarsym(rec_tguid.symtable.symlist[3]);
  1328. { the array }
  1329. maybe_begin_aggregate(tfieldvarsym(rec_tguid.symtable.symlist[3]).vardef);
  1330. for i:=Low(guid.D4) to High(guid.D4) do
  1331. emit_tai(Tai_const.Create_8bit(guid.D4[i]),u8inttype);
  1332. maybe_end_aggregate(tfieldvarsym(rec_tguid.symtable.symlist[3]).vardef);
  1333. maybe_end_aggregate(rec_tguid);
  1334. end;
  1335. procedure ttai_typedconstbuilder.emit_procdef_const(pd: tprocdef);
  1336. begin
  1337. emit_tai(Tai_const.Createname(pd.mangledname,AT_FUNCTION,0),cprocvardef.getreusableprocaddr(pd));
  1338. end;
  1339. procedure ttai_typedconstbuilder.emit_ord_const(value: int64; def: tdef);
  1340. begin
  1341. case def.size of
  1342. 1:
  1343. emit_tai(Tai_const.Create_8bit(byte(value)),def);
  1344. 2:
  1345. emit_tai(Tai_const.Create_16bit(word(value)),def);
  1346. 4:
  1347. emit_tai(Tai_const.Create_32bit(longint(value)),def);
  1348. 8:
  1349. emit_tai(Tai_const.Create_64bit(value),def);
  1350. else
  1351. internalerror(2014100501);
  1352. end;
  1353. end;
  1354. procedure ttai_typedconstbuilder.maybe_begin_aggregate(def: tdef);
  1355. begin
  1356. begin_aggregate_internal(def,false);
  1357. end;
  1358. procedure ttai_typedconstbuilder.maybe_end_aggregate(def: tdef);
  1359. begin
  1360. end_aggregate_internal(def,false);
  1361. end;
  1362. function ttai_typedconstbuilder.begin_anonymous_record(const optionalname: string; packrecords, recordalign, recordalignmin, maxcrecordalign: shortint): trecorddef;
  1363. var
  1364. anonrecorddef: trecorddef;
  1365. typesym: ttypesym;
  1366. begin
  1367. { if the name is specified, we create a typesym with that name in order
  1368. to ensure we can find it again later with that name -> reuse here as
  1369. well if possible (and that also avoids duplicate type name issues) }
  1370. if optionalname<>'' then
  1371. begin
  1372. typesym:=try_search_current_module_type(optionalname);
  1373. if assigned(typesym) then
  1374. begin
  1375. if typesym.typedef.typ<>recorddef then
  1376. internalerror(2015071401);
  1377. result:=trecorddef(typesym.typedef);
  1378. maybe_begin_aggregate(result);
  1379. exit;
  1380. end;
  1381. end;
  1382. { create skeleton def }
  1383. anonrecorddef:=crecorddef.create_global_internal(optionalname,packrecords,recordalignmin,maxcrecordalign);
  1384. trecordsymtable(anonrecorddef.symtable).recordalignment:=recordalign;
  1385. { generic aggregate housekeeping }
  1386. begin_aggregate_internal(anonrecorddef,true);
  1387. { mark as anonymous record }
  1388. curagginfo.anonrecord:=true;
  1389. { in case a descendent wants to do something with the anonrecorddef too }
  1390. result:=anonrecorddef;
  1391. end;
  1392. function ttai_typedconstbuilder.end_anonymous_record: trecorddef;
  1393. var
  1394. info: taggregateinformation;
  1395. anonrecord: boolean;
  1396. begin
  1397. info:=curagginfo;
  1398. if not assigned(info) or
  1399. (info.def.typ<>recorddef) then
  1400. internalerror(2014080201);
  1401. result:=trecorddef(info.def);
  1402. { make a copy, as we need it after info has been freed by
  1403. maybe_end_aggregate(result) }
  1404. anonrecord:=info.anonrecord;
  1405. { finalise the record skeleton (all fields have been added already by
  1406. emit_tai()) -- anonrecord may not be set in case we reused an earlier
  1407. constructed def }
  1408. if anonrecord then
  1409. trecordsymtable(result.symtable).addalignmentpadding;
  1410. end_aggregate_internal(result,true);
  1411. if anonrecord and
  1412. assigned(curagginfo) and
  1413. (curagginfo.def.typ=recorddef) then
  1414. insert_marked_aggregate_alignment(result);
  1415. end;
  1416. procedure ttai_typedconstbuilder.check_add_placeholder(def: tdef);
  1417. begin
  1418. { it only makes sense to add a placeholder inside an aggregate
  1419. (otherwise there can be but one element)
  1420. we cannot add a placeholder in the middle of a queued expression
  1421. either
  1422. the placeholder cannot be an aggregate }
  1423. if not assigned(curagginfo) or
  1424. queue_is_active or
  1425. (aggregate_kind(def)<>tck_simple) then
  1426. internalerror(2015091001);
  1427. end;
  1428. procedure ttai_typedconstbuilder.queue_init(todef: tdef);
  1429. var
  1430. info: taggregateinformation;
  1431. begin
  1432. { nested call to init? }
  1433. if fqueue_offset<>low(fqueue_offset) then
  1434. internalerror(2014062101);
  1435. { insert padding bytes before starting the queue, so that the first
  1436. padding byte won't be interpreted as the emitted value for this queue }
  1437. info:=curagginfo;
  1438. if assigned(info) then
  1439. begin
  1440. if ((info.def.typ=recorddef) or
  1441. is_object(info.def)) and
  1442. { may add support for these later }
  1443. not is_packed_record_or_object(info.def) then
  1444. pad_next_field(todef);
  1445. end;
  1446. fqueue_offset:=0;
  1447. fqueued_def:=todef;
  1448. end;
  1449. procedure ttai_typedconstbuilder.queue_vecn(def: tdef; const index: tconstexprint);
  1450. var
  1451. elelen,
  1452. vecbase: asizeint;
  1453. v: tconstexprint;
  1454. begin
  1455. elelen:=1;
  1456. vecbase:=0;
  1457. case def.typ of
  1458. stringdef :
  1459. ;
  1460. arraydef :
  1461. begin
  1462. if not is_packed_array(def) then
  1463. begin
  1464. elelen:=tarraydef(def).elesize;
  1465. vecbase:=tarraydef(def).lowrange;
  1466. end
  1467. else
  1468. Message(parser_e_packed_dynamic_open_array);
  1469. end;
  1470. else
  1471. Message(parser_e_illegal_expression);
  1472. end;
  1473. { Prevent overflow }
  1474. v:=index-vecbase;
  1475. if (v<int64(low(fqueue_offset))) or (v>int64(high(fqueue_offset))) then
  1476. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(fqueue_offset)),tostr(high(fqueue_offset)));
  1477. if high(fqueue_offset)-fqueue_offset div elelen>v then
  1478. inc(fqueue_offset,elelen*v.svalue)
  1479. else
  1480. message3(type_e_range_check_error_bounds,tostr(index),tostr(vecbase),tostr(high(fqueue_offset)-fqueue_offset div elelen+vecbase))
  1481. end;
  1482. procedure ttai_typedconstbuilder.queue_subscriptn(def: tabstractrecorddef; vs: tfieldvarsym);
  1483. begin
  1484. inc(fqueue_offset,vs.fieldoffset);
  1485. end;
  1486. function ttai_typedconstbuilder.queue_subscriptn_multiple_by_name(def: tabstractrecorddef; const fields: array of TIDString): tdef;
  1487. var
  1488. syms,
  1489. parentdefs: tfplist;
  1490. sym: tsym;
  1491. curdef: tdef;
  1492. i: longint;
  1493. begin
  1494. result:=nil;
  1495. if length(fields)=0 then
  1496. internalerror(2015071601);
  1497. syms:=tfplist.Create;
  1498. syms.count:=length(fields);
  1499. parentdefs:=tfplist.create;
  1500. parentdefs.Count:=length(fields);
  1501. curdef:=def;
  1502. for i:=low(fields) to high(fields) do
  1503. begin
  1504. sym:=search_struct_member_no_helper(tabstractrecorddef(curdef),fields[i]);
  1505. if not assigned(sym) or
  1506. (sym.typ<>fieldvarsym) or
  1507. ((i<>high(fields)) and
  1508. not(tfieldvarsym(sym).vardef.typ in [objectdef,recorddef])) then
  1509. internalerror(2015071505);
  1510. syms[i]:=sym;
  1511. parentdefs[i]:=curdef;
  1512. curdef:=tfieldvarsym(sym).vardef;
  1513. result:=curdef;
  1514. end;
  1515. for i:=high(fields) downto low(fields) do
  1516. queue_subscriptn(tabstractrecorddef(parentdefs[i]),tfieldvarsym(syms[i]));
  1517. syms.free;
  1518. parentdefs.free;
  1519. end;
  1520. procedure ttai_typedconstbuilder.queue_typeconvn(fromdef, todef: tdef);
  1521. begin
  1522. { do nothing }
  1523. end;
  1524. procedure ttai_typedconstbuilder.queue_emit_proc(pd: tprocdef);
  1525. begin
  1526. if fqueue_offset<>0 then
  1527. internalerror(2014092101);
  1528. emit_procdef_const(pd);
  1529. fqueue_offset:=low(fqueue_offset);
  1530. end;
  1531. procedure ttai_typedconstbuilder.queue_emit_staticvar(vs: tstaticvarsym);
  1532. begin
  1533. { pointerdef because we are emitting a pointer to the staticvarsym
  1534. data, not the data itself }
  1535. emit_tai(Tai_const.Createname(vs.mangledname,fqueue_offset),cpointerdef.getreusable(vs.vardef));
  1536. fqueue_offset:=low(fqueue_offset);
  1537. end;
  1538. procedure ttai_typedconstbuilder.queue_emit_label(l: tlabelsym);
  1539. begin
  1540. emit_tai(Tai_const.Createname(l.mangledname,fqueue_offset),voidcodepointertype);
  1541. fqueue_offset:=low(fqueue_offset);
  1542. end;
  1543. procedure ttai_typedconstbuilder.queue_emit_const(cs: tconstsym);
  1544. var
  1545. resourcestrrec: trecorddef;
  1546. begin
  1547. if cs.consttyp<>constresourcestring then
  1548. internalerror(2014062102);
  1549. if fqueue_offset<>0 then
  1550. internalerror(2014062103);
  1551. { warning: update if/when the type of resource strings changes }
  1552. case cs.consttyp of
  1553. constresourcestring:
  1554. begin
  1555. resourcestrrec:=trecorddef(search_system_type('TRESOURCESTRINGRECORD').typedef);
  1556. queue_subscriptn_multiple_by_name(resourcestrrec,['CURRENTVALUE']);
  1557. queue_emit_asmsym(current_asmdata.RefAsmSymbol(
  1558. make_mangledname('RESSTR',cs.owner,cs.name),AT_DATA),resourcestrrec
  1559. );
  1560. end;
  1561. { can these occur? }
  1562. constord,
  1563. conststring,constreal,
  1564. constset,constpointer,constnil,
  1565. constwstring,constguid:
  1566. internalerror(2015090903);
  1567. else
  1568. internalerror(2015090904);
  1569. end;
  1570. fqueue_offset:=low(fqueue_offset);
  1571. end;
  1572. procedure ttai_typedconstbuilder.queue_emit_asmsym(sym: tasmsymbol; def: tdef);
  1573. begin
  1574. { pointerdef, because "sym" represents the address of whatever the
  1575. data is }
  1576. def:=cpointerdef.getreusable(def);
  1577. emit_tai(Tai_const.Create_sym_offset(sym,fqueue_offset),def);
  1578. fqueue_offset:=low(fqueue_offset);
  1579. end;
  1580. procedure ttai_typedconstbuilder.queue_emit_ordconst(value: int64; def: tdef);
  1581. begin
  1582. emit_ord_const(value,def);
  1583. fqueue_offset:=low(fqueue_offset);
  1584. end;
  1585. function ttai_typedconstbuilder.queue_is_active: boolean;
  1586. begin
  1587. result:=fqueue_offset<>low(fqueue_offset)
  1588. end;
  1589. {****************************************************************************
  1590. tlowleveltypedconstplaceholder
  1591. ****************************************************************************}
  1592. constructor tlowleveltypedconstplaceholder.create(l: tasmlist; pos: tai; d: tdef);
  1593. begin
  1594. inherited create(d);
  1595. list:=l;
  1596. insertpos:=pos;
  1597. end;
  1598. procedure tlowleveltypedconstplaceholder.replace(ai: tai; d: tdef);
  1599. begin
  1600. if d<>def then
  1601. internalerror(2015091001);
  1602. list.insertafter(ai,insertpos);
  1603. list.remove(insertpos);
  1604. insertpos.free;
  1605. end;
  1606. {****************************************************************************
  1607. tai_abstracttypedconst
  1608. ****************************************************************************}
  1609. class constructor ttai_lowleveltypedconstbuilder.classcreate;
  1610. begin
  1611. caggregateinformation:=tlowlevelaggregateinformation;
  1612. end;
  1613. function ttai_lowleveltypedconstbuilder.emit_placeholder(def: tdef): ttypedconstplaceholder;
  1614. var
  1615. p: tai;
  1616. begin
  1617. check_add_placeholder(def);
  1618. p:=tai_marker.Create(mark_position);
  1619. emit_tai(p,def);
  1620. result:=tlowleveltypedconstplaceholder.create(fasmlist,p,def);
  1621. end;
  1622. procedure ttai_lowleveltypedconstbuilder.mark_anon_aggregate_alignment;
  1623. var
  1624. marker: tai_marker;
  1625. begin
  1626. marker:=tai_marker.Create(mark_position);
  1627. fasmlist.concat(marker);
  1628. tlowlevelaggregateinformation(curagginfo).anonrecmarker:=marker;
  1629. end;
  1630. procedure ttai_lowleveltypedconstbuilder.insert_marked_aggregate_alignment(def: tdef);
  1631. var
  1632. info: tlowlevelaggregateinformation;
  1633. fillbytes: asizeint;
  1634. begin
  1635. info:=tlowlevelaggregateinformation(curagginfo);
  1636. if not assigned(info.anonrecmarker) then
  1637. internalerror(2014091401);
  1638. fillbytes:=info.prepare_next_field(def);
  1639. while fillbytes>0 do
  1640. begin
  1641. fasmlist.insertafter(tai_const.create_8bit(0),info.anonrecmarker);
  1642. dec(fillbytes);
  1643. end;
  1644. fasmlist.remove(info.anonrecmarker);
  1645. info.anonrecmarker.free;
  1646. info.anonrecmarker:=nil;
  1647. end;
  1648. procedure ttai_lowleveltypedconstbuilder.finalize_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions);
  1649. begin
  1650. inherited;
  1651. { The darwin/ppc64 assembler or linker seems to have trouble }
  1652. { if a section ends with a global label without any data after it. }
  1653. { So for safety, just put a dummy value here. }
  1654. { Further, the regular linker also kills this symbol when turning }
  1655. { on smart linking in case no value appears after it, so put the }
  1656. { dummy byte there always }
  1657. { Update: the Mac OS X 10.6 linker orders data that needs to be }
  1658. { relocated before all other data, so make this data relocatable, }
  1659. { otherwise the end label won't be moved with the rest }
  1660. if (tcalo_vectorized_dead_strip_end in options) and
  1661. (target_info.system in (systems_darwin+systems_aix)) then
  1662. fasmlist.concat(Tai_const.create_sym(sym));
  1663. end;
  1664. begin
  1665. ctai_typedconstbuilder:=ttai_lowleveltypedconstbuilder;
  1666. end.