llvmdef.pas 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. {
  2. Copyright (c) 2013 by Jonas Maebe
  3. This unit implements some LLVM type helper routines.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {$i fpcdefs.inc}
  18. unit llvmdef;
  19. interface
  20. uses
  21. cclasses,globtype,
  22. aasmbase,
  23. parabase,
  24. symbase,symtype,symdef,
  25. llvmbase;
  26. type
  27. { there are three different circumstances in which procdefs are used:
  28. a) definition of a procdef that's implemented in the current module
  29. b) declaration of an external routine that's called in the current one
  30. c) alias declaration of a procdef implemented in the current module
  31. d) defining a procvar type
  32. The main differences between the contexts are:
  33. a) information about sign extension of result type, proc name, parameter names & sign-extension info & types
  34. b) information about sign extension of result type, proc name, no parameter names, with parameter sign-extension info & types
  35. c) no information about sign extension of result type, proc name, no parameter names, no information about sign extension of parameters, parameter types
  36. d) no information about sign extension of result type, no proc name, no parameter names, no information about sign extension of parameters, parameter types
  37. }
  38. tllvmprocdefdecltype = (lpd_def,lpd_decl,lpd_alias,lpd_procvar);
  39. { returns the identifier to use as typename for a def in llvm (llvm only
  40. allows naming struct types) -- only supported for defs with a typesym, and
  41. only for tabstractrecorddef descendantds and complex procvars }
  42. function llvmtypeidentifier(def: tdef): TSymStr;
  43. { encode a type into the internal format used by LLVM (for a type
  44. declaration) }
  45. function llvmencodetypedecl(def: tdef): TSymStr;
  46. { same as above, but use a type name if possible (for any use) }
  47. function llvmencodetypename(def: tdef): TSymStr;
  48. { encode a procdef/procvardef into the internal format used by LLVM }
  49. function llvmencodeproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype): TSymStr;
  50. { incremental version of the above }
  51. procedure llvmaddencodedproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype; var encodedstr: TSymStr);
  52. { function result types may have to be represented differently, e.g. a
  53. record consisting of 4 longints must be returned as a record consisting of
  54. two int64's on x86-64. This function is used to create (and reuse)
  55. temporary recorddefs for such purposes.}
  56. function llvmgettemprecorddef(fieldtypes: array of tdef; packrecords, recordalignmin, maxcrecordalign: shortint): trecorddef;
  57. { get the llvm type corresponding to a parameter, e.g. a record containing
  58. two integer int64 for an arbitrary record split over two individual int64
  59. parameters, or an int32 for an int16 parameter on a platform that requires
  60. such parameters to be zero/sign extended. The second parameter can be used
  61. to get the type before zero/sign extension, as e.g. required to generate
  62. function declarations. }
  63. function llvmgetcgparadef(const cgpara: tcgpara; beforevalueext: boolean): tdef;
  64. { can be used to extract the value extension info from acgpara. Pass in
  65. the def of the cgpara as first parameter and a local variable holding
  66. a copy of the def of the location (value extension only makes sense for
  67. ordinal parameters that are smaller than a single location). The routine
  68. will return the def of the location without sign extension (if applicable)
  69. and the kind of sign extension that was originally performed in the
  70. signext parameter }
  71. procedure llvmextractvalueextinfo(paradef: tdef; var paralocdef: tdef; out signext: tllvmvalueextension);
  72. { returns whether a paraloc should be translated into an llvm "byval"
  73. parameter. These are declared as pointers to a particular type, but
  74. usually turned into copies onto the stack. The exact behaviour for
  75. parameters that should be passed in registers is undefined and depends on
  76. the platform, and furthermore this modifier sometimes inhibits
  77. optimizations. As a result,we only use it for aggregate parameters of
  78. which we know that they should be passed on the stack }
  79. function llvmbyvalparaloc(paraloc: pcgparalocation): boolean;
  80. { returns whether a def is representated by an aggregate type in llvm
  81. (struct, array) }
  82. function llvmaggregatetype(def: tdef): boolean;
  83. function llvmconvop(var fromsize, tosize: tdef; inregs: boolean): tllvmop;
  84. { mangle a global identifier so that it's recognised by LLVM as a global
  85. (in the sense of module-global) label and so that it won't mangle the
  86. name further according to platform conventions (we already did that) }
  87. function llvmmangledname(const s: TSymStr): TSymStr;
  88. function llvmasmsymname(const sym: TAsmSymbol): TSymStr;
  89. implementation
  90. uses
  91. globals,cutils,constexp,
  92. verbose,systems,
  93. fmodule,
  94. symtable,symconst,symsym,
  95. llvmsym,hlcgobj,
  96. defutil,blockutl,cgbase,paramgr;
  97. {******************************************************************
  98. Type encoding
  99. *******************************************************************}
  100. function llvmtypeidentifier(def: tdef): TSymStr;
  101. begin
  102. if not assigned(def.typesym) then
  103. internalerror(2015041901);
  104. result:='%"typ.'+def.fullownerhierarchyname(false)+def.typesym.realname+'"'
  105. end;
  106. function llvmaggregatetype(def: tdef): boolean;
  107. begin
  108. result:=
  109. (def.typ in [recorddef,filedef,variantdef]) or
  110. ((def.typ=arraydef) and
  111. not is_dynamic_array(def)) or
  112. ((def.typ=setdef) and
  113. not is_smallset(def)) or
  114. is_shortstring(def) or
  115. is_object(def) or
  116. ((def.typ=procvardef) and
  117. not tprocvardef(def).is_addressonly)
  118. end;
  119. function llvmconvop(var fromsize, tosize: tdef; inregs: boolean): tllvmop;
  120. var
  121. fromregtyp,
  122. toregtyp: tregistertype;
  123. frombytesize,
  124. tobytesize: asizeint;
  125. begin
  126. fromregtyp:=chlcgobj.def2regtyp(fromsize);
  127. toregtyp:=chlcgobj.def2regtyp(tosize);
  128. { int to pointer or vice versa }
  129. if fromregtyp=R_ADDRESSREGISTER then
  130. begin
  131. case toregtyp of
  132. R_INTREGISTER:
  133. result:=la_ptrtoint;
  134. R_ADDRESSREGISTER:
  135. result:=la_bitcast;
  136. else
  137. result:=la_ptrtoint_to_x;
  138. end;
  139. end
  140. else if toregtyp=R_ADDRESSREGISTER then
  141. begin
  142. case fromregtyp of
  143. R_INTREGISTER:
  144. result:=la_inttoptr;
  145. R_ADDRESSREGISTER:
  146. result:=la_bitcast;
  147. else
  148. result:=la_x_to_inttoptr;
  149. end;
  150. end
  151. else
  152. begin
  153. { treat comp and currency as extended in registers (see comment at start
  154. of thlgcobj.a_loadfpu_ref_reg) }
  155. if inregs and
  156. (fromsize.typ=floatdef) then
  157. begin
  158. if tfloatdef(fromsize).floattype in [s64comp,s64currency] then
  159. fromsize:=sc80floattype;
  160. { at the value level, s80real and sc80real are the same }
  161. if tfloatdef(fromsize).floattype<>s80real then
  162. frombytesize:=fromsize.size
  163. else
  164. frombytesize:=sc80floattype.size;
  165. end
  166. else
  167. frombytesize:=fromsize.size;
  168. if inregs and
  169. (tosize.typ=floatdef) then
  170. begin
  171. if tfloatdef(tosize).floattype in [s64comp,s64currency] then
  172. tosize:=sc80floattype;
  173. if tfloatdef(tosize).floattype<>s80real then
  174. tobytesize:=tosize.size
  175. else
  176. tobytesize:=sc80floattype.size;
  177. end
  178. else
  179. tobytesize:=tosize.size;
  180. { need zero/sign extension, float truncation or plain bitcast? }
  181. if tobytesize<>frombytesize then
  182. begin
  183. case fromregtyp of
  184. R_FPUREGISTER,
  185. R_MMREGISTER:
  186. begin
  187. { todo: update once we support vectors }
  188. if not(toregtyp in [R_FPUREGISTER,R_MMREGISTER]) then
  189. internalerror(2014062203);
  190. if tobytesize<frombytesize then
  191. result:=la_fptrunc
  192. else
  193. result:=la_fpext
  194. end;
  195. else
  196. begin
  197. if tobytesize<frombytesize then
  198. result:=la_trunc
  199. else if is_signed(fromsize) then
  200. { fromsize is signed -> sign extension }
  201. result:=la_sext
  202. else
  203. result:=la_zext;
  204. end;
  205. end;
  206. end
  207. else if (fromsize=llvmbool1type) and
  208. (tosize<>llvmbool1type) then
  209. begin
  210. if is_cbool(tosize) then
  211. result:=la_sext
  212. else
  213. result:=la_zext
  214. end
  215. else if (tosize=llvmbool1type) and
  216. (fromsize<>llvmbool1type) then
  217. begin
  218. { would have to compare with 0, can't just take the lowest bit }
  219. if is_cbool(fromsize) then
  220. internalerror(2016052001)
  221. else
  222. result:=la_trunc
  223. end
  224. else
  225. result:=la_bitcast;
  226. end;
  227. end;
  228. function llvmmangledname(const s: TSymStr): TSymStr;
  229. begin
  230. if copy(s,1,length('llvm.'))<>'llvm.' then
  231. if s[1]<>'"' then
  232. result:='@"\01'+s+'"'
  233. else
  234. begin
  235. { already quoted -> insert \01 and prepend @ }
  236. result:='@'+s;
  237. insert('\01',result,3);
  238. end
  239. else
  240. result:='@'+s
  241. end;
  242. function llvmasmsymname(const sym: TAsmSymbol): TSymStr;
  243. begin
  244. { AT_ADDR and AT_LABEL represent labels in the code, which have
  245. a different type in llvm compared to (global) data labels }
  246. if sym.bind=AB_TEMP then
  247. result:='%'+sym.name
  248. else if not(sym.typ in [AT_LABEL,AT_ADDR]) then
  249. result:=llvmmangledname(sym.name)
  250. else
  251. result:='label %'+sym.name;
  252. end;
  253. function llvmbyvalparaloc(paraloc: pcgparalocation): boolean;
  254. begin
  255. { "byval" is broken for register paras on several platforms in llvm
  256. (search for "byval" in llvm's bug tracker). Additionally, it should only
  257. be used to pass aggregate parameters on the stack, because it reportedly
  258. inhibits llvm's midlevel optimizers.
  259. Exception (for now?): parameters that have special shifting
  260. requirements, because modelling those in llvm is not easy (and clang
  261. nor llvm-gcc seem to do so either) }
  262. result:=
  263. ((paraloc^.loc=LOC_REFERENCE) and
  264. llvmaggregatetype(paraloc^.def)) or
  265. ((paraloc^.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  266. (paraloc^.shiftval<>0))
  267. end;
  268. procedure llvmaddencodedabstractrecordtype(def: tabstractrecorddef; var encodedstr: TSymStr); forward;
  269. type
  270. tllvmencodeflag = (lef_inaggregate, lef_noimplicitderef, lef_typedecl);
  271. tllvmencodeflags = set of tllvmencodeflag;
  272. procedure llvmaddencodedtype_intern(def: tdef; const flags: tllvmencodeflags; var encodedstr: TSymStr);
  273. begin
  274. case def.typ of
  275. stringdef :
  276. begin
  277. case tstringdef(def).stringtype of
  278. st_widestring,
  279. st_unicodestring:
  280. { the variable does not point to the header, but to a
  281. null-terminated string/array with undefined bounds }
  282. encodedstr:=encodedstr+'i16*';
  283. st_ansistring:
  284. encodedstr:=encodedstr+'i8*';
  285. st_shortstring:
  286. { length byte followed by string bytes }
  287. if tstringdef(def).len>0 then
  288. encodedstr:=encodedstr+'['+tostr(tstringdef(def).len+1)+' x i8]'
  289. else
  290. encodedstr:=encodedstr+'[0 x i8]';
  291. else
  292. internalerror(2013100201);
  293. end;
  294. end;
  295. enumdef:
  296. begin
  297. encodedstr:=encodedstr+'i'+tostr(def.size*8);
  298. end;
  299. orddef :
  300. begin
  301. if is_void(def) then
  302. encodedstr:=encodedstr+'void'
  303. { mainly required because comparison operations return i1, and
  304. we need a way to represent the i1 type in Pascal. We don't
  305. reuse pasbool1type, because putting an i1 in a record or
  306. passing it as a parameter may result in unexpected behaviour }
  307. else if def=llvmbool1type then
  308. encodedstr:=encodedstr+'i1'
  309. else if torddef(def).ordtype<>customint then
  310. encodedstr:=encodedstr+'i'+tostr(def.size*8)
  311. else
  312. encodedstr:=encodedstr+'i'+tostr(def.packedbitsize);
  313. end;
  314. pointerdef :
  315. begin
  316. if is_voidpointer(def) then
  317. encodedstr:=encodedstr+'i8*'
  318. else
  319. begin
  320. llvmaddencodedtype_intern(tpointerdef(def).pointeddef,[],encodedstr);
  321. encodedstr:=encodedstr+'*';
  322. end;
  323. end;
  324. floatdef :
  325. begin
  326. case tfloatdef(def).floattype of
  327. s32real:
  328. encodedstr:=encodedstr+'float';
  329. s64real:
  330. encodedstr:=encodedstr+'double';
  331. { necessary to be able to force our own size/alignment }
  332. s80real:
  333. { prevent llvm from allocating the standard ABI size for
  334. extended }
  335. if lef_inaggregate in flags then
  336. encodedstr:=encodedstr+'[10 x i8]'
  337. else
  338. encodedstr:=encodedstr+'x86_fp80';
  339. sc80real:
  340. encodedstr:=encodedstr+'x86_fp80';
  341. s64comp,
  342. s64currency:
  343. encodedstr:=encodedstr+'i64';
  344. s128real:
  345. {$if defined(powerpc) or defined(powerpc128)}
  346. encodedstr:=encodedstr+'ppc_fp128';
  347. {$else}
  348. encodedstr:=encodedstr+'fp128';
  349. {$endif}
  350. else
  351. internalerror(2013100202);
  352. end;
  353. end;
  354. filedef :
  355. begin
  356. case tfiledef(def).filetyp of
  357. ft_text :
  358. llvmaddencodedtype_intern(search_system_type('TEXTREC').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  359. ft_typed :
  360. begin
  361. { in case of ISO-like I/O, the typed file def includes a
  362. get/put buffer of the size of the file's elements }
  363. if (m_isolike_io in current_settings.modeswitches) and
  364. not is_void(tfiledef(def).typedfiledef) then
  365. encodedstr:=encodedstr+'<{';
  366. llvmaddencodedtype_intern(search_system_type('FILEREC').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  367. if (m_isolike_io in current_settings.modeswitches) and
  368. not is_void(tfiledef(def).typedfiledef) then
  369. begin
  370. encodedstr:=encodedstr+',[';
  371. encodedstr:=encodedstr+tostr(tfiledef(def).typedfiledef.size);
  372. encodedstr:=encodedstr+' x i8]}>'
  373. end;
  374. end;
  375. ft_untyped :
  376. llvmaddencodedtype_intern(search_system_type('FILEREC').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  377. else
  378. internalerror(2013100203);
  379. end;
  380. end;
  381. recorddef :
  382. begin
  383. { avoid endlessly recursive definitions }
  384. if assigned(def.typesym) and
  385. ((lef_inaggregate in flags) or
  386. not(lef_typedecl in flags)) then
  387. encodedstr:=encodedstr+llvmtypeidentifier(def)
  388. else
  389. llvmaddencodedabstractrecordtype(trecorddef(def),encodedstr);
  390. end;
  391. variantdef :
  392. begin
  393. llvmaddencodedtype_intern(search_system_type('TVARDATA').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  394. end;
  395. classrefdef :
  396. begin
  397. if is_class(tclassrefdef(def).pointeddef) then
  398. begin
  399. llvmaddencodedtype_intern(tobjectdef(tclassrefdef(def).pointeddef).vmt_def,flags,encodedstr);
  400. encodedstr:=encodedstr+'*';
  401. end
  402. else if is_objcclass(tclassrefdef(def).pointeddef) then
  403. llvmaddencodedtype_intern(objc_idtype,flags,encodedstr)
  404. else
  405. encodedstr:=encodedstr+'i8*'
  406. end;
  407. setdef :
  408. begin
  409. { just an array as far as llvm is concerned; don't use a "packed
  410. array of i1" or so, this requires special support in backends
  411. and guarantees nothing about the internal format }
  412. if is_smallset(def) then
  413. llvmaddencodedtype_intern(cgsize_orddef(def_cgsize(def)),[lef_inaggregate],encodedstr)
  414. else
  415. encodedstr:=encodedstr+'['+tostr(tsetdef(def).size)+' x i8]';
  416. end;
  417. formaldef :
  418. begin
  419. { var/const/out x (always treated as "pass by reference" -> don't
  420. add extra "*" here) }
  421. encodedstr:=encodedstr+'i8';
  422. end;
  423. arraydef :
  424. begin
  425. if is_array_of_const(def) then
  426. begin
  427. encodedstr:=encodedstr+'[0 x ';
  428. llvmaddencodedtype_intern(search_system_type('TVARREC').typedef,[lef_inaggregate],encodedstr);
  429. encodedstr:=encodedstr+']';
  430. end
  431. else if is_open_array(def) then
  432. begin
  433. encodedstr:=encodedstr+'[0 x ';
  434. llvmaddencodedtype_intern(tarraydef(def).elementdef,[lef_inaggregate],encodedstr);
  435. encodedstr:=encodedstr+']';
  436. end
  437. else if is_dynamic_array(def) then
  438. begin
  439. llvmaddencodedtype_intern(tarraydef(def).elementdef,[lef_inaggregate],encodedstr);
  440. encodedstr:=encodedstr+'*';
  441. end
  442. else if is_packed_array(def) and
  443. (tarraydef(def).elementdef.typ in [enumdef,orddef]) then
  444. begin
  445. { encode as an array of bytes rather than as an array of
  446. packedbitsloadsize(elesize), because even if the load size
  447. is e.g. 2 bytes, the array may only be 1 or 3 bytes long
  448. (and if this array is inside a record, it must not be
  449. encoded as a type that is too long) }
  450. encodedstr:=encodedstr+'['+tostr(tarraydef(def).size)+' x ';
  451. llvmaddencodedtype_intern(u8inttype,[lef_inaggregate],encodedstr);
  452. encodedstr:=encodedstr+']';
  453. end
  454. else
  455. begin
  456. encodedstr:=encodedstr+'['+tostr(tarraydef(def).elecount)+' x ';
  457. llvmaddencodedtype_intern(tarraydef(def).elementdef,[lef_inaggregate],encodedstr);
  458. encodedstr:=encodedstr+']';
  459. end;
  460. end;
  461. procdef,
  462. procvardef :
  463. begin
  464. if (def.typ=procdef) or
  465. tprocvardef(def).is_addressonly then
  466. begin
  467. llvmaddencodedproctype(tabstractprocdef(def),'',lpd_procvar,encodedstr);
  468. if def.typ=procvardef then
  469. encodedstr:=encodedstr+'*';
  470. end
  471. else if ((lef_inaggregate in flags) or
  472. not(lef_typedecl in flags)) and
  473. assigned(tprocvardef(def).typesym) then
  474. begin
  475. { in case the procvardef recursively references itself, e.g.
  476. via a pointer }
  477. encodedstr:=encodedstr+llvmtypeidentifier(def);
  478. { blocks are implicit pointers }
  479. if is_block(def) then
  480. encodedstr:=encodedstr+'*'
  481. end
  482. else if is_block(def) then
  483. begin
  484. llvmaddencodedtype_intern(get_block_literal_type_for_proc(tabstractprocdef(def)),flags,encodedstr);
  485. end
  486. else
  487. begin
  488. encodedstr:=encodedstr+'<{';
  489. { code pointer }
  490. llvmaddencodedproctype(tabstractprocdef(def),'',lpd_procvar,encodedstr);
  491. { data pointer (maybe todo: generate actual layout if
  492. available) }
  493. encodedstr:=encodedstr+'*, i8*}>';
  494. end;
  495. end;
  496. objectdef :
  497. case tobjectdef(def).objecttype of
  498. odt_class,
  499. odt_objcclass,
  500. odt_object,
  501. odt_cppclass:
  502. begin
  503. if not(lef_typedecl in flags) and
  504. assigned(def.typesym) then
  505. encodedstr:=encodedstr+llvmtypeidentifier(def)
  506. else
  507. llvmaddencodedabstractrecordtype(tabstractrecorddef(def),encodedstr);
  508. if ([lef_typedecl,lef_noimplicitderef]*flags=[]) and
  509. is_implicit_pointer_object_type(def) then
  510. encodedstr:=encodedstr+'*'
  511. end;
  512. odt_interfacecom,
  513. odt_interfacecorba,
  514. odt_dispinterface:
  515. begin
  516. { type is a pointer to a pointer to the vmt }
  517. llvmaddencodedtype_intern(tobjectdef(def).vmt_def,flags,encodedstr);
  518. if ([lef_typedecl,lef_noimplicitderef]*flags=[]) then
  519. encodedstr:=encodedstr+'**';
  520. end;
  521. odt_interfacecom_function,
  522. odt_interfacecom_property,
  523. odt_objcprotocol:
  524. begin
  525. { opaque for now }
  526. encodedstr:=encodedstr+'i8*'
  527. end;
  528. odt_helper:
  529. llvmaddencodedtype_intern(tobjectdef(def).extendeddef,flags,encodedstr);
  530. else
  531. internalerror(2013100601);
  532. end;
  533. undefineddef,
  534. errordef :
  535. internalerror(2013100604);
  536. else
  537. internalerror(2013100603);
  538. end;
  539. end;
  540. function llvmencodetypename(def: tdef): TSymStr;
  541. begin
  542. result:='';
  543. llvmaddencodedtype_intern(def,[],result);
  544. end;
  545. procedure llvmaddencodedtype(def: tdef; inaggregate: boolean; var encodedstr: TSymStr);
  546. var
  547. flags: tllvmencodeflags;
  548. begin
  549. if inaggregate then
  550. flags:=[lef_inaggregate]
  551. else
  552. flags:=[];
  553. llvmaddencodedtype_intern(def,flags,encodedstr);
  554. end;
  555. procedure llvmaddencodedabstractrecordtype(def: tabstractrecorddef; var encodedstr: TSymStr);
  556. var
  557. st: tllvmshadowsymtable;
  558. symdeflist: tfpobjectlist;
  559. i: longint;
  560. nopacked: boolean;
  561. begin
  562. st:=tabstractrecordsymtable(def.symtable).llvmst;
  563. symdeflist:=st.symdeflist;
  564. nopacked:=df_llvm_no_struct_packing in def.defoptions;
  565. if nopacked then
  566. encodedstr:=encodedstr+'{ '
  567. else
  568. encodedstr:=encodedstr+'<{ ';
  569. if symdeflist.count>0 then
  570. begin
  571. i:=0;
  572. if (def.typ=objectdef) and
  573. assigned(tobjectdef(def).childof) and
  574. is_class_or_interface_or_dispinterface(tllvmshadowsymtableentry(symdeflist[0]).def) then
  575. begin
  576. { insert the struct for the class rather than a pointer to the struct }
  577. if (tllvmshadowsymtableentry(symdeflist[0]).def.typ<>objectdef) then
  578. internalerror(2008070601);
  579. llvmaddencodedtype_intern(tllvmshadowsymtableentry(symdeflist[0]).def,[lef_inaggregate,lef_noimplicitderef],encodedstr);
  580. inc(i);
  581. end;
  582. while i<symdeflist.count do
  583. begin
  584. if i<>0 then
  585. encodedstr:=encodedstr+', ';
  586. llvmaddencodedtype_intern(tllvmshadowsymtableentry(symdeflist[i]).def,[lef_inaggregate],encodedstr);
  587. inc(i);
  588. end;
  589. end;
  590. if nopacked then
  591. encodedstr:=encodedstr+' }'
  592. else
  593. encodedstr:=encodedstr+' }>';
  594. end;
  595. procedure llvmextractvalueextinfo(paradef: tdef; var paralocdef: tdef; out signext: tllvmvalueextension);
  596. begin
  597. { implicit zero/sign extension for ABI compliance? (yes, if the size
  598. of a paraloc is larger than the size of the entire parameter) }
  599. if is_ordinal(paradef) and
  600. is_ordinal(paralocdef) and
  601. (paradef.size<paralocdef.size) then
  602. begin
  603. paralocdef:=paradef;
  604. if is_signed(paradef) then
  605. signext:=lve_signext
  606. else
  607. signext:=lve_zeroext
  608. end
  609. else
  610. signext:=lve_none;
  611. end;
  612. procedure llvmaddencodedparaloctype(hp: tparavarsym; proccalloption: tproccalloption; withparaname, withattributes: boolean; var first: boolean; var encodedstr: TSymStr);
  613. var
  614. paraloc: PCGParaLocation;
  615. signext: tllvmvalueextension;
  616. usedef: tdef;
  617. begin
  618. if (proccalloption in cdecl_pocalls) and
  619. is_array_of_const(hp.vardef) then
  620. begin
  621. if not first then
  622. encodedstr:=encodedstr+', '
  623. else
  624. first:=false;
  625. encodedstr:=encodedstr+'...';
  626. exit
  627. end;
  628. if withparaname then
  629. paraloc:=hp.paraloc[calleeside].location
  630. else
  631. paraloc:=hp.paraloc[callerside].location;
  632. repeat
  633. usedef:=paraloc^.def;
  634. llvmextractvalueextinfo(hp.vardef,usedef,signext);
  635. { implicit zero/sign extension for ABI compliance? }
  636. if not first then
  637. encodedstr:=encodedstr+', ';
  638. llvmaddencodedtype_intern(usedef,[],encodedstr);
  639. { in case signextstr<>'', there should be only one paraloc -> no need
  640. to clear (reason: it means that the paraloc is larger than the
  641. original parameter) }
  642. if withattributes then
  643. encodedstr:=encodedstr+llvmvalueextension2str[signext];
  644. { sret: hidden pointer for structured function result }
  645. if vo_is_funcret in hp.varoptions then
  646. begin
  647. { "sret" is only valid for the firstparameter, while in FPC this
  648. can sometimes be second one (self comes before). In general,
  649. this is not a problem: we can just leave out sret, which means
  650. the result will be a bit less well optimised), but it is for
  651. AArch64: there, the sret parameter must be passed in a different
  652. register (-> paranr_result is smaller than paranr_self for that
  653. platform in symconst) }
  654. {$ifdef aarch64}
  655. if not first then
  656. internalerror(2015101404);
  657. {$endif aarch64}
  658. if withattributes then
  659. if first then
  660. encodedstr:=encodedstr+' sret'
  661. else { we can add some other attributes to optimise things,}
  662. encodedstr:=encodedstr+' noalias nocapture';
  663. end
  664. else if not paramanager.push_addr_param(hp.varspez,hp.vardef,proccalloption) and
  665. llvmbyvalparaloc(paraloc) then
  666. begin
  667. if withattributes then
  668. encodedstr:=encodedstr+'* byval'
  669. else
  670. encodedstr:=encodedstr+'*';
  671. end
  672. else if withattributes and
  673. paramanager.push_addr_param(hp.varspez,hp.vardef,proccalloption) then
  674. begin
  675. { it's not valid to take the address of a parameter and store it for
  676. use past the end of the function call (since the address can always
  677. be on the stack and become invalid later) }
  678. encodedstr:=encodedstr+' nocapture';
  679. { open array/array of const/variant array may be a valid pointer but empty }
  680. if not is_special_array(hp.vardef) and
  681. { e.g. empty records }
  682. (hp.vardef.size<>0) then
  683. begin
  684. case hp.varspez of
  685. vs_value,
  686. vs_const:
  687. begin
  688. encodedstr:=encodedstr+' nocapture dereferenceable('
  689. end;
  690. vs_var,
  691. vs_out,
  692. vs_constref:
  693. begin
  694. { while normally these are not nil, it is technically possible
  695. to pass nil via ptrtype(nil)^ }
  696. encodedstr:=encodedstr+' nocapture dereferenceable_or_null('
  697. end;
  698. else
  699. internalerror(2018120801);
  700. end;
  701. if hp.vardef.typ<>formaldef then
  702. encodedstr:=encodedstr+tostr(hp.vardef.size)+')'
  703. else
  704. encodedstr:=encodedstr+'1)';
  705. end;
  706. end;
  707. if withparaname then
  708. begin
  709. if paraloc^.llvmloc.loc<>LOC_REFERENCE then
  710. internalerror(2014010803);
  711. encodedstr:=encodedstr+' '+llvmasmsymname(paraloc^.llvmloc.sym);
  712. end;
  713. paraloc:=paraloc^.next;
  714. first:=false;
  715. until not assigned(paraloc);
  716. end;
  717. function llvmencodeproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype): TSymStr;
  718. begin
  719. result:='';
  720. llvmaddencodedproctype(def,customname,pddecltype,result);
  721. end;
  722. procedure llvmaddencodedproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype; var encodedstr: TSymStr);
  723. var
  724. usedef: tdef;
  725. paranr: longint;
  726. hp: tparavarsym;
  727. signext: tllvmvalueextension;
  728. useside: tcallercallee;
  729. first: boolean;
  730. begin
  731. { when writing a definition, we have to write the parameter names, and
  732. those are only available on the callee side. In all other cases,
  733. we are at the callerside }
  734. if pddecltype=lpd_def then
  735. useside:=calleeside
  736. else
  737. useside:=callerside;
  738. def.init_paraloc_info(useside);
  739. first:=true;
  740. { function result (return-by-ref is handled explicitly) }
  741. if not paramanager.ret_in_param(def.returndef,def) then
  742. begin
  743. usedef:=llvmgetcgparadef(def.funcretloc[useside],false);
  744. llvmextractvalueextinfo(def.returndef,usedef,signext);
  745. { specifying result sign extention information for an alias causes
  746. an error for some reason }
  747. if pddecltype in [lpd_decl,lpd_def] then
  748. encodedstr:=encodedstr+llvmvalueextension2str[signext];
  749. encodedstr:=encodedstr+' ';
  750. llvmaddencodedtype_intern(usedef,[],encodedstr);
  751. end
  752. else
  753. begin
  754. encodedstr:=encodedstr+' ';
  755. llvmaddencodedtype(voidtype,false,encodedstr);
  756. end;
  757. encodedstr:=encodedstr+' ';
  758. { add procname? }
  759. if (pddecltype in [lpd_decl,lpd_def]) and
  760. (def.typ=procdef) then
  761. if customname='' then
  762. encodedstr:=encodedstr+llvmmangledname(tprocdef(def).mangledname)
  763. else
  764. encodedstr:=encodedstr+llvmmangledname(customname);
  765. encodedstr:=encodedstr+'(';
  766. { parameters }
  767. first:=true;
  768. for paranr:=0 to def.paras.count-1 do
  769. begin
  770. hp:=tparavarsym(def.paras[paranr]);
  771. llvmaddencodedparaloctype(hp,def.proccalloption,pddecltype in [lpd_def],not(pddecltype in [lpd_procvar,lpd_alias]),first,encodedstr);
  772. end;
  773. if po_varargs in def.procoptions then
  774. begin
  775. if not first then
  776. encodedstr:=encodedstr+', ';
  777. encodedstr:=encodedstr+'...';
  778. end;
  779. encodedstr:=encodedstr+')'
  780. end;
  781. function llvmgettemprecorddef(fieldtypes: array of tdef; packrecords, recordalignmin, maxcrecordalign: shortint): trecorddef;
  782. var
  783. i: longint;
  784. res: PHashSetItem;
  785. oldsymtablestack: tsymtablestack;
  786. hrecst: trecordsymtable;
  787. hdef: tdef;
  788. hrecdef: trecorddef;
  789. sym: tfieldvarsym;
  790. typename: string;
  791. begin
  792. typename:=internaltypeprefixName[itp_llvmstruct];
  793. for i:=low(fieldtypes) to high(fieldtypes) do
  794. begin
  795. hdef:=fieldtypes[i];
  796. case hdef.typ of
  797. orddef:
  798. case torddef(hdef).ordtype of
  799. s8bit,
  800. u8bit,
  801. pasbool1,
  802. pasbool8:
  803. typename:=typename+'i8';
  804. s16bit,
  805. u16bit:
  806. typename:=typename+'i16';
  807. s32bit,
  808. u32bit:
  809. typename:=typename+'i32';
  810. s64bit,
  811. u64bit:
  812. typename:=typename+'i64';
  813. customint:
  814. typename:=typename+'i'+tostr(torddef(hdef).packedbitsize);
  815. else
  816. { other types should not appear currently, add as needed }
  817. internalerror(2014012001);
  818. end;
  819. floatdef:
  820. case tfloatdef(hdef).floattype of
  821. s32real:
  822. typename:=typename+'f32';
  823. s64real:
  824. typename:=typename+'f64';
  825. else
  826. { other types should not appear currently, add as needed }
  827. internalerror(2014012008);
  828. end;
  829. else
  830. typename:=typename+'d'+hdef.unique_id_str;
  831. end;
  832. end;
  833. if not assigned(current_module) then
  834. internalerror(2014012002);
  835. res:=current_module.llvmdefs.FindOrAdd(@typename[1],length(typename));
  836. if not assigned(res^.Data) then
  837. begin
  838. res^.Data:=crecorddef.create_global_internal(typename,packrecords,
  839. recordalignmin,maxcrecordalign);
  840. for i:=low(fieldtypes) to high(fieldtypes) do
  841. trecorddef(res^.Data).add_field_by_def('F'+tostr(i),fieldtypes[i]);
  842. end;
  843. trecordsymtable(trecorddef(res^.Data).symtable).addalignmentpadding;
  844. result:=trecorddef(res^.Data);
  845. end;
  846. function llvmgetcgparadef(const cgpara: tcgpara; beforevalueext: boolean): tdef;
  847. var
  848. retdeflist: array[0..9] of tdef;
  849. retloc: pcgparalocation;
  850. usedef: tdef;
  851. valueext: tllvmvalueextension;
  852. i: longint;
  853. sizeleft: asizeint;
  854. begin
  855. { single location }
  856. if not assigned(cgpara.location^.next) then
  857. begin
  858. { def of the location, except in case of zero/sign-extension and
  859. zero-sized records }
  860. if not is_special_array(cgpara.def) and
  861. (cgpara.def.size=0) then
  862. usedef:=cgpara.def
  863. else
  864. usedef:=cgpara.location^.def;
  865. if beforevalueext then
  866. llvmextractvalueextinfo(cgpara.def,usedef,valueext);
  867. { comp and currency are handled by the x87 in this case. They cannot
  868. be represented directly in llvm, and llvmdef translates them into
  869. i64 (since that's their storage size and internally they also are
  870. int64). Solve this by changing the type to s80real in the
  871. returndef/parameter declaration. }
  872. if (usedef.typ=floatdef) and
  873. (tfloatdef(usedef).floattype in [s64comp,s64currency]) then
  874. usedef:=s80floattype;
  875. result:=usedef;
  876. exit
  877. end;
  878. { multiple locations -> create temp record }
  879. retloc:=cgpara.location;
  880. i:=0;
  881. sizeleft:=cgpara.Def.size;
  882. repeat
  883. if i>high(retdeflist) then
  884. internalerror(2016121801);
  885. if assigned(retloc^.next) then
  886. begin
  887. retdeflist[i]:=retloc^.def;
  888. dec(sizeleft,retloc^.def.size);
  889. end
  890. else if retloc^.def.size<>sizeleft then
  891. begin
  892. case sizeleft of
  893. 1:
  894. retdeflist[i]:=u8inttype;
  895. 2:
  896. retdeflist[i]:=u16inttype;
  897. 3:
  898. retdeflist[i]:=u24inttype;
  899. 4:
  900. retdeflist[i]:=u32inttype;
  901. 5:
  902. retdeflist[i]:=u40inttype;
  903. 6:
  904. retdeflist[i]:=u48inttype;
  905. 7:
  906. retdeflist[i]:=u56inttype;
  907. else
  908. retdeflist[i]:=retloc^.def;
  909. end
  910. end
  911. else
  912. retdeflist[i]:=retloc^.def;
  913. inc(i);
  914. retloc:=retloc^.next;
  915. until not assigned(retloc);
  916. result:=llvmgettemprecorddef(slice(retdeflist,i),C_alignment,
  917. targetinfos[target_info.system]^.alignment.recordalignmin,
  918. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  919. include(result.defoptions,df_llvm_no_struct_packing);
  920. end;
  921. function llvmencodetypedecl(def: tdef): TSymStr;
  922. begin
  923. result:='';
  924. llvmaddencodedtype_intern(def,[lef_typedecl],result);
  925. end;
  926. end.