aasmcnst.pas 46 KB

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