aasmcnst.pas 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526
  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,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. procedure finish;
  81. destructor destroy; override;
  82. end;
  83. tasmlabofs = record
  84. lab: tasmlabel;
  85. ofs: asizeint;
  86. end;
  87. { flags for the finalisation of the typed const builder asmlist }
  88. ttcasmlistoption = (
  89. { the tasmsymbol is a tasmlabel }
  90. tcalo_is_lab,
  91. { start a new section (e.g., because we don't know the current section
  92. type) }
  93. tcalo_new_section,
  94. { this symbol is the start of a block of data that should be
  95. dead-stripable/smartlinkable; may imply starting a new section, but
  96. not necessarily (depends on what the platform requirements are) }
  97. tcalo_make_dead_strippable
  98. );
  99. ttcasmlistoptions = set of ttcasmlistoption;
  100. { information about aggregates we are parsing }
  101. taggregateinformation = class
  102. private
  103. function getcuroffset: asizeint;
  104. function getfieldoffset(l: longint): asizeint;
  105. protected
  106. { type of the aggregate }
  107. fdef: tdef;
  108. { type of the aggregate }
  109. ftyp: ttypedconstkind;
  110. { symtable entry of the previously emitted field in case of a
  111. record/object (nil if none emitted yet), used to insert alignment bytes
  112. if necessary for variant records and objects }
  113. fcurfield,
  114. { field corresponding to the data that will be emitted next in case of a
  115. record/object (nil if not set), used to handle variant records and
  116. objects }
  117. fnextfield: tfieldvarsym;
  118. { similar as the fcurfield/fnextfield above, but instead of fieldvarsyms
  119. these are indices in the symlist of a recorddef that correspond to
  120. fieldvarsyms. These are used only for non-variant records, simply
  121. traversing the fields in order. We could use the above method here as
  122. well, but to find the next field we'd always have to use
  123. symlist.indexof(fcurfield), which would be quite slow. These have -1 as
  124. value if they're not set }
  125. fcurindex,
  126. fnextindex: longint;
  127. { anonymous record that is being built as we add constant data }
  128. fanonrecord: boolean;
  129. property curindex: longint read fcurindex write fcurindex;
  130. property nextindex: longint read fnextindex write fnextindex;
  131. public
  132. constructor create(_def: tdef; _typ: ttypedconstkind); virtual;
  133. { calculated padding bytes for alignment if needed, and add the def of the
  134. next field in case we are constructing an anonymous record }
  135. function prepare_next_field(nextfielddef: tdef): asizeint;
  136. property def: tdef read fdef;
  137. property typ: ttypedconstkind read ftyp;
  138. property curfield: tfieldvarsym read fcurfield write fcurfield;
  139. property nextfield: tfieldvarsym read fnextfield write fnextfield;
  140. property fieldoffset[l: longint]: asizeint read getfieldoffset;
  141. property curoffset: asizeint read getcuroffset;
  142. property anonrecord: boolean read fanonrecord write fanonrecord;
  143. end;
  144. taggregateinformationclass = class of taggregateinformation;
  145. { Warning: never directly create a ttai_typedconstbuilder instance,
  146. instead create a cai_typedconstbuilder (this class can be overridden) }
  147. ttai_typedconstbuilder = class abstract
  148. { class type to use when creating new aggregate information instances }
  149. protected class var
  150. caggregateinformation: taggregateinformationclass;
  151. private
  152. function getcurragginfo: taggregateinformation;
  153. procedure set_next_field(AValue: tfieldvarsym);
  154. protected
  155. { temporary list in which all data is collected }
  156. fasmlist: tasmlist;
  157. { options for the final asmlist }
  158. foptions: ttcasmlistoptions;
  159. { while queueing elements of a compound expression, this is the current
  160. offset in the top-level array/record }
  161. fqueue_offset: asizeint;
  162. { array of caggregateinformation instances }
  163. faggregateinformation: tfpobjectlist;
  164. { Support for generating data that is only referenced from the typed
  165. constant data that we are currently generated. Such data can all be put
  166. in the same dead-strippable unit, as it's either all included or none of
  167. it is included. This data can be spread over multiple kinds of sections
  168. though (e.g. rodata and rodata_no_rel), so per section keep track whether
  169. we already started a dead-strippable unit and if so, what the section
  170. name was (so that on platforms that perform the dead stripping based on
  171. sections, we put all data for one typed constant into a single section
  172. with the same name) }
  173. protected type
  174. tinternal_data_section_info = record
  175. secname: TSymStr;
  176. sectype: TAsmSectiontype;
  177. end;
  178. protected var
  179. { all internally generated data must be stored in the same list, as it must
  180. be consecutive (if it's spread over multiple lists, we don't know in
  181. which order they'll be concatenated) -> keep track of this list }
  182. finternal_data_asmlist: tasmlist;
  183. { kind of the last section we started in the finternal_data_asmlist, to
  184. avoid creating unnecessary section statements }
  185. finternal_data_current_section: TAsmSectiontype;
  186. { info about in which kinds of sections we have already emitted internal
  187. data, and what their names were }
  188. finternal_data_section_info: array of tinternal_data_section_info;
  189. { ensure that finalize_asmlist is called only once }
  190. fasmlist_finalized: boolean;
  191. { returns whether def must be handled as an aggregate on the current
  192. platform }
  193. function aggregate_kind(def: tdef): ttypedconstkind; virtual;
  194. { finalize the asmlist: add the necessary symbols etc }
  195. procedure finalize_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions); virtual;
  196. { called by the public emit_tai() routines to actually add the typed
  197. constant data; the public ones also take care of adding extra padding
  198. bytes etc (by calling this one) }
  199. procedure do_emit_tai(p: tai; def: tdef); virtual;
  200. { calls prepare_next_field() and adds the padding bytes in the current
  201. location }
  202. procedure pad_next_field(nextfielddef: tdef);
  203. { returns the index in finternal_data_section_info of the info for the
  204. section of type typ. Returns -1 if there is no such info yet }
  205. function get_internal_data_section_index(typ: TAsmSectiontype): longint;
  206. { get a start label for an internal data section (at the start of a
  207. potentially dead-strippable part) }
  208. function get_internal_data_section_start_label: tasmlabel; virtual;
  209. { get a label in the middle of an internal data section (no dead
  210. stripping) }
  211. function get_internal_data_section_internal_label: tasmlabel; virtual;
  212. { easy access to the top level aggregate information instance }
  213. property curagginfo: taggregateinformation read getcurragginfo;
  214. public
  215. constructor create(const options: ttcasmlistoptions); virtual;
  216. destructor destroy; override;
  217. public
  218. { returns a builder for generating data that is only referrenced by the
  219. typed constant date we are currently generating (e.g. string data for a
  220. pchar constant). Also returns the label that will be placed at the start
  221. of that data. list is the tasmlist to which the data will be added.
  222. secname can be empty to use a default }
  223. procedure start_internal_data_builder(list: tasmlist; sectype: TAsmSectiontype; const secname: TSymStr; out tcb: ttai_typedconstbuilder; out l: tasmlabel);
  224. { finish a previously started internal data builder, including
  225. concatenating all generated data to the provided list and freeing the
  226. builder }
  227. procedure finish_internal_data_builder(var tcb: ttai_typedconstbuilder; l: tasmlabel; def: tdef; alignment: longint);
  228. { add a simple constant data element (p) to the typed constant.
  229. def is the type of the added value }
  230. procedure emit_tai(p: tai; def: tdef); virtual;
  231. { same as above, for a special case: when the def is a procvardef and we
  232. want to use it explicitly as a procdef (i.e., not as a record with a
  233. code and data pointer in case of a complex procvardef) }
  234. procedure emit_tai_procvar2procdef(p: tai; pvdef: tprocvardef); virtual;
  235. protected
  236. function emit_string_const_common(stringtype: tstringtype; len: asizeint; encoding: tstringencoding; var startlab: tasmlabel):tasmlabofs;
  237. procedure begin_aggregate_internal(def: tdef; anonymous: boolean); virtual;
  238. procedure end_aggregate_internal(def: tdef; anonymous: boolean); virtual;
  239. { when building an anonymous record, we cannot immediately insert the
  240. alignment before it in case it's nested, since we only know the required
  241. alignment once all fields have been inserted -> mark the location before
  242. the anonymous record, and insert the alignment once it's finished }
  243. procedure mark_anon_aggregate_alignment; virtual; abstract;
  244. procedure insert_marked_aggregate_alignment(def: tdef); virtual; abstract;
  245. public
  246. class function get_dynstring_rec_name(typ: tstringtype; winlike: boolean; len: asizeint): string;
  247. { the datalist parameter specifies where the data for the string constant
  248. will be emitted (via an internal data builder) }
  249. function emit_ansistring_const(datalist: TAsmList; data: pchar; len: asizeint; encoding: tstringencoding): tasmlabofs;
  250. function emit_unicodestring_const(datalist: TAsmList; data: pointer; encoding: tstringencoding; winlike: boolean):tasmlabofs;
  251. { emits a tasmlabofs as returned by emit_*string_const }
  252. procedure emit_string_offset(const ll: tasmlabofs; const strlength: longint; const st: tstringtype; const winlikewidestring: boolean; const charptrdef: tdef);virtual;
  253. { emit a shortstring constant, and return its def }
  254. function emit_shortstring_const(const str: shortstring): tdef;
  255. { emit a guid constant }
  256. procedure emit_guid_const(const guid: tguid);
  257. { emit a procdef constant }
  258. procedure emit_procdef_const(pd: tprocdef);
  259. { emit an ordinal constant }
  260. procedure emit_ord_const(value: int64; def: tdef);
  261. { begin a potential aggregate type. Must be called for any type
  262. that consists of multiple tai constant data entries, or that
  263. represents an aggregate at the Pascal level (a record, a non-dynamic
  264. array, ... }
  265. procedure maybe_begin_aggregate(def: tdef);
  266. { end a potential aggregate type. Must be paired with every
  267. maybe_begin_aggregate }
  268. procedure maybe_end_aggregate(def: tdef);
  269. { similar as above, but in case
  270. a) it's definitely a record
  271. b) the def of the record should be automatically constructed based on
  272. the types of the emitted fields
  273. }
  274. function begin_anonymous_record(const optionalname: string; packrecords, recordalignmin, maxcrecordalign: shortint): trecorddef; virtual;
  275. function end_anonymous_record: trecorddef; virtual;
  276. { The next group of routines are for constructing complex expressions.
  277. While parsing a typed constant these operators are encountered from
  278. outer to inner, so that is also the order in which they should be
  279. added to the queue. Only one queue can be active at a time. }
  280. { Init the queue. Gives an internalerror if a queue was already active }
  281. procedure queue_init(todef: tdef); virtual;
  282. { queue an array/string indexing operation (performs all range checking,
  283. so it doesn't have to be duplicated in all descendents). }
  284. procedure queue_vecn(def: tdef; const index: tconstexprint); virtual;
  285. { queue a subscripting operation }
  286. procedure queue_subscriptn(def: tabstractrecorddef; vs: tfieldvarsym); virtual;
  287. { queue a type conversion operation }
  288. procedure queue_typeconvn(fromdef, todef: tdef); virtual;
  289. { queue an address taking operation }
  290. procedure queue_addrn(fromdef, todef: tdef); virtual;
  291. { finalise the queue (so a new one can be created) and flush the
  292. previously queued operations, applying them in reverse order on a...}
  293. { ... procdef }
  294. procedure queue_emit_proc(pd: tprocdef); virtual;
  295. { ... staticvarsym }
  296. procedure queue_emit_staticvar(vs: tstaticvarsym); virtual;
  297. { ... labelsym }
  298. procedure queue_emit_label(l: tlabelsym); virtual;
  299. { ... constsym }
  300. procedure queue_emit_const(cs: tconstsym); virtual;
  301. { ... asmsym/asmlabel }
  302. procedure queue_emit_asmsym(sym: tasmsymbol; def: tdef); virtual;
  303. { ... an ordinal constant }
  304. procedure queue_emit_ordconst(value: int64; def: tdef); virtual;
  305. { finalize the internal asmlist (if necessary) and return it.
  306. This asmlist will be freed when the builder is destroyed, so add its
  307. contents to another list first. This property should only be accessed
  308. once all data has been added. }
  309. function get_final_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: longint): tasmlist;
  310. { returns the offset of the string data relative to ansi/unicode/widestring
  311. constant labels. On most platforms, this is 0 (with the header at a
  312. negative offset), but on some platforms such negative offsets are not
  313. supported this is equal to the header size }
  314. class function get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint; virtual;
  315. { set the fieldvarsym whose data we will emit next; needed
  316. in case of variant records, so we know which part of the variant gets
  317. initialised. Also in case of objects, because the fieldvarsyms are spread
  318. over the symtables of the entire inheritance tree }
  319. property next_field: tfieldvarsym write set_next_field;
  320. protected
  321. { this one always return the actual offset, called by the above (and
  322. overridden versions) }
  323. class function get_string_header_size(typ: tstringtype; winlikewidestring: boolean): pint;
  324. end;
  325. ttai_typedconstbuilderclass = class of ttai_typedconstbuilder;
  326. tlowlevelaggregateinformation = class(taggregateinformation)
  327. protected
  328. fanonrecmarker: tai;
  329. public
  330. property anonrecmarker: tai read fanonrecmarker write fanonrecmarker;
  331. end;
  332. ttai_lowleveltypedconstbuilder = class(ttai_typedconstbuilder)
  333. protected
  334. procedure mark_anon_aggregate_alignment; override;
  335. procedure insert_marked_aggregate_alignment(def: tdef); override;
  336. public
  337. { set the default value for caggregateinformation (= tlowlevelaggregateinformation) }
  338. class constructor classcreate;
  339. end;
  340. var
  341. ctai_typedconstbuilder: ttai_typedconstbuilderclass;
  342. implementation
  343. uses
  344. verbose,globals,systems,widestr,
  345. fmodule,
  346. symbase,symtable,defutil;
  347. {****************************************************************************
  348. taggregateinformation
  349. ****************************************************************************}
  350. function taggregateinformation.getcuroffset: asizeint;
  351. var
  352. field: tfieldvarsym;
  353. begin
  354. if assigned(curfield) then
  355. result:=curfield.fieldoffset+curfield.vardef.size
  356. else if curindex<>-1 then
  357. begin
  358. field:=tfieldvarsym(tabstractrecorddef(def).symtable.symlist[curindex]);
  359. result:=field.fieldoffset+field.vardef.size
  360. end
  361. else
  362. result:=0
  363. end;
  364. function taggregateinformation.getfieldoffset(l: longint): asizeint;
  365. var
  366. field: tfieldvarsym;
  367. begin
  368. field:=tfieldvarsym(tabstractrecorddef(def).symtable.symlist[l]);
  369. result:=field.fieldoffset;
  370. end;
  371. constructor taggregateinformation.create(_def: tdef; _typ: ttypedconstkind);
  372. begin
  373. fdef:=_def;
  374. ftyp:=_typ;
  375. fcurindex:=-1;
  376. fnextindex:=-1;
  377. end;
  378. function taggregateinformation.prepare_next_field(nextfielddef: tdef): asizeint;
  379. var
  380. currentoffset,nextoffset: asizeint;
  381. i: longint;
  382. begin
  383. { get the next field and its offset, and make that next field the current
  384. one }
  385. if assigned(nextfield) then
  386. begin
  387. nextoffset:=nextfield.fieldoffset;
  388. currentoffset:=curoffset;
  389. curfield:=nextfield;
  390. end
  391. else
  392. begin
  393. { must set nextfield for unions and objects, as we cannot
  394. automatically detect the "next" field in that case }
  395. if ((def.typ=recorddef) and
  396. trecorddef(def).isunion) or
  397. is_object(def) then
  398. internalerror(2014091202);
  399. { if we are constructing this record as data gets emitted, add a field
  400. for this data }
  401. if anonrecord then
  402. trecorddef(def).add_field_by_def(nextfielddef);
  403. { find next field }
  404. i:=curindex;
  405. repeat
  406. inc(i);
  407. until (tsym(tabstractrecorddef(def).symtable.symlist[i]).typ=fieldvarsym) and
  408. not(sp_static in tsym(tabstractrecorddef(def).symtable.symlist[i]).symoptions);
  409. nextoffset:=fieldoffset[i];
  410. currentoffset:=curoffset;
  411. curindex:=i;
  412. end;
  413. { need padding? }
  414. result:=nextoffset-currentoffset;
  415. end;
  416. {****************************************************************************
  417. tai_abstracttypedconst
  418. ****************************************************************************}
  419. procedure tai_abstracttypedconst.setdef(def: tdef);
  420. begin
  421. { should not be changed, rewrite the calling code if this happens }
  422. if assigned(fdef) then
  423. Internalerror(2014080203);
  424. fdef:=def;
  425. end;
  426. constructor tai_abstracttypedconst.create(_adetyp: ttypedconstkind; _def: tdef);
  427. begin
  428. inherited create;
  429. typ:=ait_typedconst;
  430. fadetyp:=_adetyp;
  431. fdef:=_def;
  432. end;
  433. {****************************************************************************
  434. tai_simpletypedconst
  435. ****************************************************************************}
  436. procedure tai_simpletypedconst.setval(AValue: tai);
  437. begin
  438. fval:=AValue;
  439. end;
  440. constructor tai_simpletypedconst.create(_adetyp: ttypedconstkind; _def: tdef; _val: tai);
  441. begin
  442. inherited create(_adetyp,_def);
  443. fval:=_val;
  444. end;
  445. {****************************************************************************
  446. tai_aggregatetypedconst.tadeenumerator
  447. ****************************************************************************}
  448. constructor tai_aggregatetypedconst.tadeenumerator.create(data: tai_aggregatetypedconst);
  449. begin
  450. fvalues:=data.fvalues;
  451. fvaluespos:=-1;
  452. end;
  453. function tai_aggregatetypedconst.tadeenumerator.getcurrent: tai_abstracttypedconst;
  454. begin
  455. result:=tai_abstracttypedconst(fvalues[fvaluespos]);
  456. end;
  457. function tai_aggregatetypedconst.tadeenumerator.movenext: boolean;
  458. begin
  459. if fvaluespos<pred(fvalues.count) then
  460. begin
  461. inc(fvaluespos);
  462. result:=true
  463. end
  464. else
  465. result:=false;
  466. end;
  467. procedure tai_aggregatetypedconst.tadeenumerator.reset;
  468. begin
  469. fvaluespos:=0
  470. end;
  471. {****************************************************************************
  472. tai_aggregatetypedconst
  473. ****************************************************************************}
  474. procedure tai_aggregatetypedconst.convert_to_string;
  475. var
  476. ai: tai_abstracttypedconst;
  477. newstr: tai_string;
  478. begin
  479. newstr:=tai_string.Create('');
  480. for ai in self do
  481. begin
  482. if ai.adetyp<>tck_simple then
  483. internalerror(2014070103);
  484. add_to_string(newstr,tai_simpletypedconst(ai).val);
  485. ai.free;
  486. end;
  487. fvalues.count:=0;
  488. { the "nil" def will be replaced with an array def of the appropriate
  489. size once we're finished adding data, so we don't create intermediate
  490. arraydefs all the time }
  491. fvalues.add(tai_simpletypedconst.create(tck_simple,nil,newstr));
  492. end;
  493. procedure tai_aggregatetypedconst.add_to_string(strtai: tai_string; othertai: tai);
  494. begin
  495. case othertai.typ of
  496. ait_string:
  497. begin
  498. strtai.str:=reallocmem(strtai.str,strtai.len+tai_string(othertai).len+1);
  499. { also copy null terminator }
  500. move(tai_string(othertai).str[0],strtai.str[strtai.len],tai_string(othertai).len+1);
  501. { the null terminator is not part of the string data }
  502. strtai.len:=strtai.len+tai_string(othertai).len;
  503. end;
  504. ait_const:
  505. begin
  506. if tai_const(othertai).size<>1 then
  507. internalerror(2014070101);
  508. strtai.str:=reallocmem(strtai.str,strtai.len+1);
  509. strtai.str[strtai.len]:=ansichar(tai_const(othertai).value);
  510. strtai.str[strtai.len+1]:=#0;
  511. inc(strtai.len);
  512. end;
  513. else
  514. internalerror(2014070102);
  515. end;
  516. end;
  517. constructor tai_aggregatetypedconst.create(_adetyp: ttypedconstkind; _fdef: tdef);
  518. begin
  519. inherited;
  520. fisstring:=false;
  521. fvalues:=tfplist.create;
  522. end;
  523. function tai_aggregatetypedconst.getenumerator: tadeenumerator;
  524. begin
  525. result:=tadeenumerator.create(self);
  526. end;
  527. procedure tai_aggregatetypedconst.addvalue(val: tai_abstracttypedconst);
  528. begin
  529. { merge string constants and ordinal constants added in an array of
  530. char, to unify the length and the string data }
  531. if fisstring or
  532. ((val.adetyp=tck_simple) and
  533. (tai_simpletypedconst(val).val.typ=ait_string)) then
  534. begin
  535. if not fisstring and
  536. (fvalues.count>0) then
  537. convert_to_string;
  538. fisstring:=true;
  539. case fvalues.count of
  540. 0: fvalues.add(val);
  541. 1:
  542. begin
  543. add_to_string(tai_string(tai_simpletypedconst(fvalues[0]).val),tai_simpletypedconst(val).val);
  544. val.free
  545. end
  546. else
  547. internalerror(2014070104);
  548. end;
  549. end
  550. else
  551. fvalues.add(val);
  552. end;
  553. function tai_aggregatetypedconst.valuecount: longint;
  554. begin
  555. result:=fvalues.count;
  556. end;
  557. procedure tai_aggregatetypedconst.insertvaluebeforepos(val: tai_abstracttypedconst; pos: longint);
  558. begin
  559. fvalues.insert(pos,val);
  560. end;
  561. procedure tai_aggregatetypedconst.finish;
  562. begin
  563. if fisstring then
  564. begin
  565. { set the def: an array of char with the same length as the string
  566. data }
  567. if fvalues.count<>1 then
  568. internalerror(2014070105);
  569. tai_simpletypedconst(fvalues[0]).fdef:=
  570. getarraydef(cansichartype,
  571. tai_string(tai_simpletypedconst(fvalues[0]).val).len);
  572. end;
  573. end;
  574. destructor tai_aggregatetypedconst.destroy;
  575. begin
  576. fvalues.free;
  577. inherited destroy;
  578. end;
  579. {*****************************************************************************
  580. ttai_typedconstbuilder
  581. *****************************************************************************}
  582. function ttai_typedconstbuilder.getcurragginfo: taggregateinformation;
  583. begin
  584. if assigned(faggregateinformation) and
  585. (faggregateinformation.count>0) then
  586. result:=taggregateinformation(faggregateinformation[faggregateinformation.count-1])
  587. else
  588. result:=nil;
  589. end;
  590. procedure ttai_typedconstbuilder.set_next_field(AValue: tfieldvarsym);
  591. var
  592. info: taggregateinformation;
  593. begin
  594. info:=curagginfo;
  595. if not assigned(info) then
  596. internalerror(2014091206);
  597. info.nextfield:=AValue;
  598. end;
  599. procedure ttai_typedconstbuilder.pad_next_field(nextfielddef: tdef);
  600. var
  601. fillbytes: asizeint;
  602. begin
  603. fillbytes:=curagginfo.prepare_next_field(nextfielddef);
  604. while fillbytes>0 do
  605. begin
  606. do_emit_tai(tai_const.create_8bit(0),u8inttype);
  607. dec(fillbytes);
  608. end;
  609. end;
  610. function ttai_typedconstbuilder.get_internal_data_section_index(typ: TAsmSectiontype): longint;
  611. begin
  612. { avoid wrong warning by -Oodfa }
  613. result:=-1;
  614. for result:=low(finternal_data_section_info) to high(finternal_data_section_info) do
  615. if finternal_data_section_info[result].sectype=typ then
  616. exit;
  617. result:=-1;
  618. end;
  619. function ttai_typedconstbuilder.get_internal_data_section_start_label: tasmlabel;
  620. begin
  621. { on Darwin, dead code/data stripping happens based on non-temporary
  622. labels (any label that doesn't start with "L" -- it doesn't have
  623. to be global) }
  624. if target_info.system in systems_darwin then
  625. current_asmdata.getstaticdatalabel(result)
  626. else if create_smartlink_library then
  627. current_asmdata.getglobaldatalabel(result)
  628. else
  629. current_asmdata.getlocaldatalabel(result);
  630. end;
  631. function ttai_typedconstbuilder.get_internal_data_section_internal_label: tasmlabel;
  632. begin
  633. if create_smartlink_library then
  634. { all labels need to be global in case they're in another object }
  635. current_asmdata.getglobaldatalabel(result)
  636. else
  637. { no special requirement for the label -> just get a local one }
  638. current_asmdata.getlocaldatalabel(result);
  639. end;
  640. function ttai_typedconstbuilder.aggregate_kind(def: tdef): ttypedconstkind;
  641. begin
  642. if (def.typ in [recorddef,filedef,variantdef]) or
  643. is_object(def) or
  644. ((def.typ=procvardef) and
  645. not tprocvardef(def).is_addressonly) then
  646. result:=tck_record
  647. else if ((def.typ=arraydef) and
  648. not is_dynamic_array(def)) or
  649. ((def.typ=setdef) and
  650. not is_smallset(def)) or
  651. is_shortstring(def) then
  652. result:=tck_array
  653. else
  654. result:=tck_simple;
  655. end;
  656. procedure ttai_typedconstbuilder.finalize_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions);
  657. var
  658. prelist: tasmlist;
  659. begin
  660. prelist:=tasmlist.create;
  661. { only now add items based on the symbolname, because it may be
  662. modified by the "section" specifier in case of a typed constant }
  663. if tcalo_make_dead_strippable in options then
  664. begin
  665. maybe_new_object_file(prelist);
  666. { we always need a new section here, since if we started a new
  667. object file then we have to say what the section is, and otherwise
  668. we need a new section because that's how the dead stripping works
  669. (except on Darwin, but that will be addressed in a future commit) }
  670. new_section(prelist,section,secname,const_align(alignment));
  671. end
  672. else if tcalo_new_section in options then
  673. new_section(prelist,section,secname,const_align(alignment))
  674. else
  675. prelist.concat(cai_align.Create(const_align(alignment)));
  676. if not(tcalo_is_lab in options) then
  677. if sym.bind=AB_GLOBAL then
  678. prelist.concat(tai_symbol.Create_Global(sym,0))
  679. else
  680. prelist.concat(tai_symbol.Create(sym,0))
  681. else
  682. prelist.concat(tai_label.Create(tasmlabel(sym)));
  683. { insert the symbol information before the data }
  684. fasmlist.insertlist(prelist);
  685. { end of the symbol }
  686. fasmlist.concat(tai_symbol_end.Createname(sym.name));
  687. { free the temporary list }
  688. prelist.free;
  689. end;
  690. procedure ttai_typedconstbuilder.do_emit_tai(p: tai; def: tdef);
  691. begin
  692. { by default we don't care about the type }
  693. fasmlist.concat(p);
  694. end;
  695. function ttai_typedconstbuilder.get_final_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: longint): tasmlist;
  696. begin
  697. if not fasmlist_finalized then
  698. begin
  699. finalize_asmlist(sym,def,section,secname,alignment,foptions);
  700. fasmlist_finalized:=true;
  701. end;
  702. result:=fasmlist;
  703. end;
  704. class function ttai_typedconstbuilder.get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint;
  705. begin
  706. { darwin's linker does not support negative offsets }
  707. if not(target_info.system in systems_darwin) then
  708. result:=0
  709. else
  710. result:=get_string_header_size(typ,winlikewidestring);
  711. end;
  712. class function ttai_typedconstbuilder.get_string_header_size(typ: tstringtype; winlikewidestring: boolean): pint;
  713. const
  714. ansistring_header_size =
  715. { encoding }
  716. 2 +
  717. { elesize }
  718. 2 +
  719. {$ifdef cpu64bitaddr}
  720. { alignment }
  721. 4 +
  722. {$endif cpu64bitaddr}
  723. { reference count }
  724. sizeof(pint) +
  725. { length }
  726. sizeof(pint);
  727. unicodestring_header_size = ansistring_header_size;
  728. begin
  729. case typ of
  730. st_ansistring:
  731. result:=ansistring_header_size;
  732. st_unicodestring:
  733. result:=unicodestring_header_size;
  734. st_widestring:
  735. if winlikewidestring then
  736. result:=0
  737. else
  738. result:=unicodestring_header_size;
  739. else
  740. result:=0;
  741. end;
  742. end;
  743. constructor ttai_typedconstbuilder.create(const options: ttcasmlistoptions);
  744. begin
  745. inherited create;
  746. fasmlist:=tasmlist.create;
  747. foptions:=options;
  748. { queue is empty }
  749. fqueue_offset:=low(fqueue_offset);
  750. finternal_data_current_section:=sec_none;
  751. end;
  752. destructor ttai_typedconstbuilder.destroy;
  753. begin
  754. { the queue should have been flushed if it was used }
  755. if fqueue_offset<>low(fqueue_offset) then
  756. internalerror(2014062901);
  757. faggregateinformation.free;
  758. fasmlist.free;
  759. inherited destroy;
  760. end;
  761. procedure ttai_typedconstbuilder.start_internal_data_builder(list: tasmlist; sectype: TAsmSectiontype; const secname: TSymStr; out tcb: ttai_typedconstbuilder; out l: tasmlabel);
  762. var
  763. options: ttcasmlistoptions;
  764. foundsec: longint;
  765. begin
  766. options:=[tcalo_is_lab];
  767. { Add a section header if the previous one was different. We'll use the
  768. same section name in case multiple items are added to the same kind of
  769. section (rodata, rodata_no_rel, ...), so that everything will still
  770. end up in the same section even if there are multiple section headers }
  771. if finternal_data_current_section<>sectype then
  772. include(options,tcalo_new_section);
  773. finternal_data_current_section:=sectype;
  774. l:=nil;
  775. { did we already create a section of this type for the internal data of
  776. this builder? }
  777. foundsec:=get_internal_data_section_index(sectype);
  778. if foundsec=-1 then
  779. begin
  780. { we only need to start a dead-strippable section of data at the
  781. start of the first subsection of this kind for this block.
  782. exception: if dead stripping happens based on objects/libraries,
  783. then we only have to create a new object file for the first
  784. internal data section of any kind (all the rest will simply be put
  785. in the same object file) }
  786. if create_smartlink then
  787. begin
  788. if not create_smartlink_library or
  789. (length(finternal_data_section_info)=0) then
  790. include(options,tcalo_make_dead_strippable);
  791. { on Darwin, dead code/data stripping happens based on non-
  792. temporary labels (any label that doesn't start with "L" -- it
  793. doesn't have to be global) -> add a non-temporary lobel at the
  794. start of every kind of subsection created in this builder }
  795. if target_info.system in systems_darwin then
  796. l:=get_internal_data_section_start_label;
  797. end;
  798. foundsec:=length(finternal_data_section_info);
  799. setlength(finternal_data_section_info,foundsec+1);
  800. finternal_data_section_info[foundsec].sectype:=sectype;
  801. end;
  802. if not assigned(finternal_data_asmlist) and
  803. (cs_create_smart in current_settings.moduleswitches) then
  804. begin
  805. l:=get_internal_data_section_start_label;
  806. { the internal data list should only be assigned by this routine,
  807. the first time that an internal data block is started }
  808. if not assigned(list) or
  809. assigned(finternal_data_asmlist) then
  810. internalerror(2015032101);
  811. finternal_data_asmlist:=list;
  812. end
  813. { all internal data for this tcb must go to the same list (otherwise all
  814. data we want to add to the dead-strippable block is not guaranteed to
  815. be sequential and e.g. in the same object file in case of library-based
  816. dead stripping) }
  817. else if (assigned(finternal_data_asmlist) and
  818. (list<>finternal_data_asmlist)) or
  819. not assigned(list) then
  820. internalerror(2015032101);
  821. finternal_data_asmlist:=list;
  822. if not assigned(l) then
  823. l:=get_internal_data_section_internal_label;
  824. { first section of this kind -> set name }
  825. if finternal_data_section_info[foundsec].secname='' then
  826. if secname='' then
  827. finternal_data_section_info[foundsec].secname:=l.Name
  828. else
  829. finternal_data_section_info[foundsec].secname:=secname
  830. { if the name is specified multiple times, it must match }
  831. else if (secname<>'') and
  832. (finternal_data_section_info[foundsec].secname<>secname) then
  833. internalerror(2015032401);
  834. tcb:=ttai_typedconstbuilderclass(classtype).create(options);
  835. end;
  836. procedure ttai_typedconstbuilder.finish_internal_data_builder(var tcb: ttai_typedconstbuilder; l: tasmlabel; def: tdef; alignment: longint);
  837. begin
  838. finternal_data_asmlist.concatList(tcb.get_final_asmlist(l,def,
  839. finternal_data_current_section,
  840. finternal_data_section_info[get_internal_data_section_index(finternal_data_current_section)].secname,
  841. alignment));
  842. tcb.free;
  843. tcb:=nil;
  844. end;
  845. procedure ttai_typedconstbuilder.emit_tai(p: tai; def: tdef);
  846. var
  847. kind: ttypedconstkind;
  848. info: taggregateinformation;
  849. begin
  850. { these elements can be aggregates themselves, e.g. a shortstring can
  851. be emitted as a series of bytes and char arrays }
  852. kind:=aggregate_kind(def);
  853. info:=curagginfo;
  854. if (kind<>tck_simple) and
  855. (not assigned(info) or
  856. (info.typ<>kind)) then
  857. internalerror(2014091001);
  858. { if we're emitting a record, handle the padding bytes, and in case of
  859. an anonymous record also add the next field }
  860. if assigned(info) then
  861. begin
  862. if ((info.def.typ=recorddef) or
  863. is_object(info.def)) and
  864. { may add support for these later }
  865. not is_packed_record_or_object(info.def) then
  866. pad_next_field(def);
  867. end;
  868. { emit the data }
  869. do_emit_tai(p,def);
  870. end;
  871. procedure ttai_typedconstbuilder.emit_tai_procvar2procdef(p: tai; pvdef: tprocvardef);
  872. begin
  873. { nothing special by default, since we don't care about the type }
  874. emit_tai(p,pvdef);
  875. end;
  876. function ttai_typedconstbuilder.emit_string_const_common(stringtype: tstringtype; len: asizeint; encoding: tstringencoding; var startlab: tasmlabel): tasmlabofs;
  877. var
  878. string_symofs: asizeint;
  879. charptrdef: tdef;
  880. elesize: word;
  881. begin
  882. result.lab:=startlab;
  883. result.ofs:=0;
  884. { pack the data, so that we don't add unnecessary null bytes after the
  885. constant string }
  886. begin_anonymous_record('$'+get_dynstring_rec_name(stringtype,false,len),1,1,1);
  887. string_symofs:=get_string_symofs(stringtype,false);
  888. { encoding }
  889. emit_tai(tai_const.create_16bit(encoding),u16inttype);
  890. inc(result.ofs,2);
  891. { element size }
  892. case stringtype of
  893. st_ansistring:
  894. begin
  895. elesize:=1;
  896. charptrdef:=charpointertype;
  897. end;
  898. st_unicodestring:
  899. begin
  900. elesize:=2;
  901. charptrdef:=widecharpointertype;
  902. end
  903. else
  904. internalerror(2014080401);
  905. end;
  906. emit_tai(tai_const.create_16bit(elesize),u16inttype);
  907. inc(result.ofs,2);
  908. {$ifdef cpu64bitaddr}
  909. { dummy for alignment }
  910. emit_tai(tai_const.create_32bit(0),u32inttype);
  911. inc(result.ofs,4);
  912. {$endif cpu64bitaddr}
  913. emit_tai(tai_const.create_pint(-1),ptrsinttype);
  914. inc(result.ofs,sizeof(pint));
  915. emit_tai(tai_const.create_pint(len),ptrsinttype);
  916. inc(result.ofs,sizeof(pint));
  917. if string_symofs=0 then
  918. begin
  919. { results in slightly more efficient code }
  920. emit_tai(tai_label.create(result.lab),charptrdef);
  921. result.ofs:=0;
  922. { create new label of the same kind (including whether or not the
  923. name starts with target_asm.labelprefix in case it's AB_LOCAL,
  924. so we keep the difference depending on whether the original was
  925. allocated via getstatic/getlocal/getglobal datalabel) }
  926. startlab:=tasmlabel.create(current_asmdata.AsmSymbolDict,startlab.name+'$strlab',startlab.bind,startlab.typ);
  927. end;
  928. { sanity check }
  929. if result.ofs<>string_symofs then
  930. internalerror(2012051701);
  931. end;
  932. procedure ttai_typedconstbuilder.begin_aggregate_internal(def: tdef; anonymous: boolean);
  933. var
  934. info: taggregateinformation;
  935. tck: ttypedconstkind;
  936. begin
  937. tck:=aggregate_kind(def);
  938. if tck=tck_simple then
  939. exit;
  940. if not assigned(faggregateinformation) then
  941. faggregateinformation:=tfpobjectlist.create
  942. { if we're starting an anonymous record, we can't align it yet because
  943. the alignment depends on the fields that will be added -> we'll do
  944. it at the end }
  945. else if not anonymous then
  946. begin
  947. { add padding if necessary, and update the current field/offset }
  948. info:=curagginfo;
  949. if is_record(curagginfo.def) or
  950. is_object(curagginfo.def) then
  951. pad_next_field(def);
  952. end
  953. { if this is the outer record, no padding is required; the alignment
  954. has to be specified explicitly in that case via get_final_asmlist() }
  955. else if assigned(curagginfo) and
  956. (curagginfo.def.typ=recorddef) then
  957. { mark where we'll have to insert the padding bytes at the end }
  958. mark_anon_aggregate_alignment;
  959. info:=caggregateinformation.create(def,aggregate_kind(def));
  960. faggregateinformation.add(info);
  961. end;
  962. procedure ttai_typedconstbuilder.end_aggregate_internal(def: tdef; anonymous: boolean);
  963. var
  964. info: taggregateinformation;
  965. fillbytes: asizeint;
  966. tck: ttypedconstkind;
  967. begin
  968. tck:=aggregate_kind(def);
  969. if tck=tck_simple then
  970. exit;
  971. info:=curagginfo;
  972. if not assigned(info) then
  973. internalerror(2014091002);
  974. if def<>info.def then
  975. internalerror(2014091205);
  976. { add tail padding if necessary }
  977. if (is_record(def) or
  978. is_object(def)) and
  979. not is_packed_record_or_object(def) then
  980. begin
  981. fillbytes:=def.size-info.curoffset;
  982. while fillbytes>0 do
  983. begin
  984. do_emit_tai(Tai_const.Create_8bit(0),u8inttype);
  985. dec(fillbytes)
  986. end;
  987. end;
  988. { pop and free the information }
  989. faggregateinformation.count:=faggregateinformation.count-1;
  990. info.free;
  991. end;
  992. class function ttai_typedconstbuilder.get_dynstring_rec_name(typ: tstringtype; winlike: boolean; len: asizeint): string;
  993. begin
  994. case typ of
  995. st_ansistring:
  996. result:='ansistrrec';
  997. st_unicodestring,
  998. st_widestring:
  999. if (typ=st_unicodestring) or
  1000. not winlike then
  1001. result:='unicodestrrec'
  1002. else
  1003. result:='widestrrec';
  1004. else
  1005. internalerror(2014080402);
  1006. end;
  1007. result:=result+tostr(len);
  1008. end;
  1009. function ttai_typedconstbuilder.emit_ansistring_const(datalist: TAsmList; data: pchar; len: asizeint; encoding: tstringencoding): tasmlabofs;
  1010. var
  1011. s: PChar;
  1012. startlab: tasmlabel;
  1013. ansistrrecdef: trecorddef;
  1014. datadef: tdef;
  1015. datatcb: ttai_typedconstbuilder;
  1016. begin
  1017. start_internal_data_builder(datalist,sec_rodata_norel,'',datatcb,startlab);
  1018. result:=datatcb.emit_string_const_common(st_ansistring,len,encoding,startlab);
  1019. getmem(s,len+1);
  1020. move(data^,s^,len);
  1021. s[len]:=#0;
  1022. { terminating zero included }
  1023. datadef:=getarraydef(cansichartype,len+1);
  1024. datatcb.maybe_begin_aggregate(datadef);
  1025. datatcb.emit_tai(tai_string.create_pchar(s,len+1),datadef);
  1026. datatcb.maybe_end_aggregate(datadef);
  1027. ansistrrecdef:=datatcb.end_anonymous_record;
  1028. finish_internal_data_builder(datatcb,startlab,ansistrrecdef,const_align(sizeof(pointer)));
  1029. end;
  1030. function ttai_typedconstbuilder.emit_unicodestring_const(datalist: TAsmList; data: pointer; encoding: tstringencoding; winlike: boolean):tasmlabofs;
  1031. var
  1032. i, strlength: longint;
  1033. string_symofs: asizeint;
  1034. startlab: tasmlabel;
  1035. datadef: tdef;
  1036. uniwidestrrecdef: trecorddef;
  1037. datatcb: ttai_typedconstbuilder;
  1038. begin
  1039. start_internal_data_builder(datalist,sec_rodata_norel,'',datatcb,startlab);
  1040. strlength:=getlengthwidestring(pcompilerwidestring(data));
  1041. if winlike then
  1042. begin
  1043. result.lab:=startlab;
  1044. datatcb.begin_anonymous_record('$'+get_dynstring_rec_name(st_widestring,true,strlength),
  1045. 4,
  1046. targetinfos[target_info.system]^.alignment.recordalignmin,
  1047. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1048. datatcb.emit_tai(Tai_const.Create_32bit(strlength*cwidechartype.size),s32inttype);
  1049. { can we optimise by placing the string constant label at the
  1050. required offset? }
  1051. string_symofs:=get_string_symofs(st_widestring,true);
  1052. if string_symofs=0 then
  1053. begin
  1054. { yes }
  1055. datatcb.emit_tai(Tai_label.Create(result.lab),widecharpointertype);
  1056. { allocate a separate label for the start of the data (see
  1057. emit_string_const_common() for explanation) }
  1058. startlab:=tasmlabel.create(current_asmdata.AsmSymbolDict,startlab.name+'$strlab',startlab.bind,startlab.typ);
  1059. end
  1060. else
  1061. internalerror(2015031502);
  1062. result.ofs:=string_symofs;
  1063. end
  1064. else
  1065. begin
  1066. result:=datatcb.emit_string_const_common(st_unicodestring,strlength,encoding,startlab);
  1067. end;
  1068. if cwidechartype.size = 2 then
  1069. begin
  1070. datadef:=getarraydef(cwidechartype,strlength+1);
  1071. datatcb.maybe_begin_aggregate(datadef);
  1072. for i:=0 to strlength-1 do
  1073. datatcb.emit_tai(Tai_const.Create_16bit(pcompilerwidestring(data)^.data[i]),cwidechartype);
  1074. { ending #0 }
  1075. datatcb.emit_tai(Tai_const.Create_16bit(0),cwidechartype);
  1076. datatcb.maybe_end_aggregate(datadef);
  1077. uniwidestrrecdef:=datatcb.end_anonymous_record;
  1078. end
  1079. else
  1080. { code generation for other sizes must be written }
  1081. internalerror(200904271);
  1082. finish_internal_data_builder(datatcb,startlab,datadef,const_align(sizeof(pint)));
  1083. end;
  1084. procedure ttai_typedconstbuilder.emit_string_offset(const ll: tasmlabofs; const strlength: longint; const st: tstringtype; const winlikewidestring: boolean; const charptrdef: tdef);
  1085. begin
  1086. emit_tai(Tai_const.Create_sym_offset(ll.lab,ll.ofs),charptrdef);
  1087. end;
  1088. function ttai_typedconstbuilder.emit_shortstring_const(const str: shortstring): tdef;
  1089. begin
  1090. { we use an arraydef instead of a shortstringdef, because we don't have
  1091. functionality in place yet to reuse shortstringdefs of the same length
  1092. and neither the lowlevel nor the llvm typedconst builder cares about
  1093. this difference }
  1094. result:=getarraydef(cansichartype,length(str)+1);
  1095. maybe_begin_aggregate(result);
  1096. emit_tai(Tai_const.Create_8bit(length(str)),u8inttype);
  1097. if str<>'' then
  1098. emit_tai(Tai_string.Create(str),getarraydef(cansichartype,length(str)));
  1099. maybe_end_aggregate(result);
  1100. end;
  1101. procedure ttai_typedconstbuilder.emit_guid_const(const guid: tguid);
  1102. var
  1103. i: longint;
  1104. begin
  1105. maybe_begin_aggregate(rec_tguid);
  1106. { variant record -> must specify which fields get initialised }
  1107. next_field:=tfieldvarsym(rec_tguid.symtable.symlist[0]);
  1108. emit_tai(Tai_const.Create_32bit(longint(guid.D1)),u32inttype);
  1109. next_field:=tfieldvarsym(rec_tguid.symtable.symlist[1]);
  1110. emit_tai(Tai_const.Create_16bit(guid.D2),u16inttype);
  1111. next_field:=tfieldvarsym(rec_tguid.symtable.symlist[2]);
  1112. emit_tai(Tai_const.Create_16bit(guid.D3),u16inttype);
  1113. next_field:=tfieldvarsym(rec_tguid.symtable.symlist[3]);
  1114. { the array }
  1115. maybe_begin_aggregate(tfieldvarsym(rec_tguid.symtable.symlist[3]).vardef);
  1116. for i:=Low(guid.D4) to High(guid.D4) do
  1117. emit_tai(Tai_const.Create_8bit(guid.D4[i]),u8inttype);
  1118. maybe_end_aggregate(tfieldvarsym(rec_tguid.symtable.symlist[3]).vardef);
  1119. maybe_end_aggregate(rec_tguid);
  1120. end;
  1121. procedure ttai_typedconstbuilder.emit_procdef_const(pd: tprocdef);
  1122. begin
  1123. emit_tai(Tai_const.Createname(pd.mangledname,AT_FUNCTION,0),getprocaddressprocvar(pd));
  1124. end;
  1125. procedure ttai_typedconstbuilder.emit_ord_const(value: int64; def: tdef);
  1126. begin
  1127. case def.size of
  1128. 1:
  1129. emit_tai(Tai_const.Create_8bit(byte(value)),def);
  1130. 2:
  1131. emit_tai(Tai_const.Create_16bit(word(value)),def);
  1132. 4:
  1133. emit_tai(Tai_const.Create_32bit(longint(value)),def);
  1134. 8:
  1135. emit_tai(Tai_const.Create_64bit(value),def);
  1136. else
  1137. internalerror(2014100501);
  1138. end;
  1139. end;
  1140. procedure ttai_typedconstbuilder.maybe_begin_aggregate(def: tdef);
  1141. begin
  1142. begin_aggregate_internal(def,false);
  1143. end;
  1144. procedure ttai_typedconstbuilder.maybe_end_aggregate(def: tdef);
  1145. begin
  1146. end_aggregate_internal(def,false);
  1147. end;
  1148. function ttai_typedconstbuilder.begin_anonymous_record(const optionalname: string; packrecords, recordalignmin, maxcrecordalign: shortint): trecorddef;
  1149. var
  1150. anonrecorddef: trecorddef;
  1151. srsym: tsym;
  1152. srsymtable: tsymtable;
  1153. found: boolean;
  1154. begin
  1155. { if the name is specified, we create a typesym with that name in order
  1156. to ensure we can find it again later with that name -> reuse here as
  1157. well if possible (and that also avoids duplicate type name issues) }
  1158. if optionalname<>'' then
  1159. begin
  1160. if optionalname[1]='$' then
  1161. found:=searchsym_in_module(current_module,copy(optionalname,2,length(optionalname)),srsym,srsymtable)
  1162. else
  1163. found:=searchsym_in_module(current_module,optionalname,srsym,srsymtable);
  1164. if found then
  1165. begin
  1166. if (srsym.typ<>typesym) or
  1167. (ttypesym(srsym).typedef.typ<>recorddef) then
  1168. internalerror(2014091207);
  1169. result:=trecorddef(ttypesym(srsym).typedef);
  1170. maybe_begin_aggregate(result);
  1171. exit;
  1172. end;
  1173. end;
  1174. { create skeleton def }
  1175. anonrecorddef:=crecorddef.create_global_internal(optionalname,packrecords,recordalignmin,maxcrecordalign);
  1176. { generic aggregate housekeeping }
  1177. begin_aggregate_internal(anonrecorddef,true);
  1178. { mark as anonymous record }
  1179. curagginfo.anonrecord:=true;
  1180. { in case a descendent wants to do something with the anonrecorddef too }
  1181. result:=anonrecorddef;
  1182. end;
  1183. function ttai_typedconstbuilder.end_anonymous_record: trecorddef;
  1184. var
  1185. info: taggregateinformation;
  1186. anonrecord: boolean;
  1187. begin
  1188. info:=curagginfo;
  1189. if not assigned(info) or
  1190. (info.def.typ<>recorddef) then
  1191. internalerror(2014080201);
  1192. result:=trecorddef(info.def);
  1193. { make a copy, as we need it after info has been freed by
  1194. maybe_end_aggregate(result) }
  1195. anonrecord:=info.anonrecord;
  1196. { finalise the record skeleton (all fields have been added already by
  1197. emit_tai()) -- anonrecord may not be set in case we reused an earlier
  1198. constructed def }
  1199. if anonrecord then
  1200. trecordsymtable(result.symtable).addalignmentpadding;
  1201. end_aggregate_internal(result,true);
  1202. if anonrecord and
  1203. assigned(curagginfo) and
  1204. (curagginfo.def.typ=recorddef) then
  1205. insert_marked_aggregate_alignment(result);
  1206. end;
  1207. procedure ttai_typedconstbuilder.queue_init(todef: tdef);
  1208. begin
  1209. { nested call to init? }
  1210. if fqueue_offset<>low(fqueue_offset) then
  1211. internalerror(2014062101);
  1212. fqueue_offset:=0;
  1213. end;
  1214. procedure ttai_typedconstbuilder.queue_vecn(def: tdef; const index: tconstexprint);
  1215. var
  1216. elelen,
  1217. vecbase: asizeint;
  1218. v: tconstexprint;
  1219. begin
  1220. elelen:=1;
  1221. vecbase:=0;
  1222. case def.typ of
  1223. stringdef :
  1224. ;
  1225. arraydef :
  1226. begin
  1227. if not is_packed_array(def) then
  1228. begin
  1229. elelen:=tarraydef(def).elesize;
  1230. vecbase:=tarraydef(def).lowrange;
  1231. end
  1232. else
  1233. Message(parser_e_packed_dynamic_open_array);
  1234. end;
  1235. else
  1236. Message(parser_e_illegal_expression);
  1237. end;
  1238. { Prevent overflow }
  1239. v:=index-vecbase;
  1240. if (v<int64(low(fqueue_offset))) or (v>int64(high(fqueue_offset))) then
  1241. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(fqueue_offset)),tostr(high(fqueue_offset)));
  1242. if high(fqueue_offset)-fqueue_offset div elelen>v then
  1243. inc(fqueue_offset,elelen*v.svalue)
  1244. else
  1245. message3(type_e_range_check_error_bounds,tostr(index),tostr(vecbase),tostr(high(fqueue_offset)-fqueue_offset div elelen+vecbase))
  1246. end;
  1247. procedure ttai_typedconstbuilder.queue_subscriptn(def: tabstractrecorddef; vs: tfieldvarsym);
  1248. begin
  1249. inc(fqueue_offset,vs.fieldoffset);
  1250. end;
  1251. procedure ttai_typedconstbuilder.queue_typeconvn(fromdef, todef: tdef);
  1252. begin
  1253. { do nothing }
  1254. end;
  1255. procedure ttai_typedconstbuilder.queue_addrn(fromdef, todef: tdef);
  1256. begin
  1257. { do nothing }
  1258. end;
  1259. procedure ttai_typedconstbuilder.queue_emit_proc(pd: tprocdef);
  1260. begin
  1261. if fqueue_offset<>0 then
  1262. internalerror(2014092101);
  1263. emit_procdef_const(pd);
  1264. fqueue_offset:=low(fqueue_offset);
  1265. end;
  1266. procedure ttai_typedconstbuilder.queue_emit_staticvar(vs: tstaticvarsym);
  1267. begin
  1268. { getpointerdef because we are emitting a pointer to the staticvarsym
  1269. data, not the data itself }
  1270. emit_tai(Tai_const.Createname(vs.mangledname,fqueue_offset),getpointerdef(vs.vardef));
  1271. fqueue_offset:=low(fqueue_offset);
  1272. end;
  1273. procedure ttai_typedconstbuilder.queue_emit_label(l: tlabelsym);
  1274. begin
  1275. emit_tai(Tai_const.Createname(l.mangledname,fqueue_offset),voidcodepointertype);
  1276. fqueue_offset:=low(fqueue_offset);
  1277. end;
  1278. procedure ttai_typedconstbuilder.queue_emit_const(cs: tconstsym);
  1279. begin
  1280. if cs.consttyp<>constresourcestring then
  1281. internalerror(2014062102);
  1282. if fqueue_offset<>0 then
  1283. internalerror(2014062103);
  1284. { warning: update if/when the type of resource strings changes }
  1285. emit_tai(Tai_const.Createname(make_mangledname('RESSTR',cs.owner,cs.name),AT_DATA,sizeof(pint)),cansistringtype);
  1286. fqueue_offset:=low(fqueue_offset);
  1287. end;
  1288. procedure ttai_typedconstbuilder.queue_emit_asmsym(sym: tasmsymbol; def: tdef);
  1289. begin
  1290. { getpointerdef, because "sym" represents the address of whatever the
  1291. data is }
  1292. def:=getpointerdef(def);
  1293. emit_tai(Tai_const.Create_sym_offset(sym,fqueue_offset),def);
  1294. fqueue_offset:=low(fqueue_offset);
  1295. end;
  1296. procedure ttai_typedconstbuilder.queue_emit_ordconst(value: int64; def: tdef);
  1297. begin
  1298. emit_ord_const(value,def);
  1299. fqueue_offset:=low(fqueue_offset);
  1300. end;
  1301. {****************************************************************************
  1302. tai_abstracttypedconst
  1303. ****************************************************************************}
  1304. class constructor ttai_lowleveltypedconstbuilder.classcreate;
  1305. begin
  1306. caggregateinformation:=tlowlevelaggregateinformation;
  1307. end;
  1308. procedure ttai_lowleveltypedconstbuilder.mark_anon_aggregate_alignment;
  1309. var
  1310. marker: tai_marker;
  1311. begin
  1312. marker:=tai_marker.Create(mark_position);
  1313. fasmlist.concat(marker);
  1314. tlowlevelaggregateinformation(curagginfo).anonrecmarker:=marker;
  1315. end;
  1316. procedure ttai_lowleveltypedconstbuilder.insert_marked_aggregate_alignment(def: tdef);
  1317. var
  1318. info: tlowlevelaggregateinformation;
  1319. fillbytes: asizeint;
  1320. begin
  1321. info:=tlowlevelaggregateinformation(curagginfo);
  1322. if not assigned(info.anonrecmarker) then
  1323. internalerror(2014091401);
  1324. fillbytes:=info.prepare_next_field(def);
  1325. while fillbytes>0 do
  1326. begin
  1327. fasmlist.insertafter(tai_const.create_8bit(0),info.anonrecmarker);
  1328. dec(fillbytes);
  1329. end;
  1330. fasmlist.remove(info.anonrecmarker);
  1331. info.anonrecmarker.free;
  1332. info.anonrecmarker:=nil;
  1333. end;
  1334. begin
  1335. ctai_typedconstbuilder:=ttai_lowleveltypedconstbuilder;
  1336. end.