llvmdef.pas 41 KB

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