2
0

llvmdef.pas 42 KB

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