aasmcnst.pas 56 KB

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