aasmcnst.pas 46 KB

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