aasmcnst.pas 55 KB

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