2
0

llvmdef.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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: tfplist; 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(fromsize, tosize: tdef): 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. cutils,constexp,
  92. verbose,systems,
  93. fmodule,
  94. symtable,symconst,symsym,
  95. llvmsym,hlcgobj,
  96. defutil,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+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(fromsize, tosize: tdef): 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. frombytesize:=fromsize.size;
  154. tobytesize:=tosize.size;
  155. { need zero/sign extension, float truncation or plain bitcast? }
  156. if tobytesize<>frombytesize then
  157. begin
  158. case fromregtyp of
  159. R_FPUREGISTER,
  160. R_MMREGISTER:
  161. begin
  162. { todo: update once we support vectors }
  163. if not(toregtyp in [R_FPUREGISTER,R_MMREGISTER]) then
  164. internalerror(2014062203);
  165. if tobytesize<frombytesize then
  166. result:=la_fptrunc
  167. else
  168. result:=la_fpext
  169. end;
  170. else
  171. begin
  172. if tobytesize<frombytesize then
  173. result:=la_trunc
  174. else if is_signed(fromsize) then
  175. { fromsize is signed -> sign extension }
  176. result:=la_sext
  177. else
  178. result:=la_zext;
  179. end;
  180. end;
  181. end
  182. else if is_pasbool(fromsize) and
  183. not is_pasbool(tosize) then
  184. begin
  185. if is_cbool(tosize) then
  186. result:=la_sext
  187. else
  188. result:=la_zext
  189. end
  190. else if is_pasbool(tosize) and
  191. not is_pasbool(fromsize) then
  192. result:=la_trunc
  193. else
  194. result:=la_bitcast;
  195. end;
  196. end;
  197. function llvmmangledname(const s: TSymStr): TSymStr;
  198. begin
  199. if copy(s,1,length('llvm.'))<>'llvm.' then
  200. result:='@"\01'+s+'"'
  201. else
  202. result:='@'+s
  203. end;
  204. function llvmasmsymname(const sym: TAsmSymbol): TSymStr;
  205. begin
  206. { AT_ADDR and AT_LABEL represent labels in the code, which have
  207. a different type in llvm compared to (global) data labels }
  208. if sym.bind=AB_TEMP then
  209. result:='%'+sym.name
  210. else if not(sym.typ in [AT_LABEL,AT_ADDR]) then
  211. result:=llvmmangledname(sym.name)
  212. else
  213. result:='label %'+sym.name;
  214. end;
  215. function llvmbyvalparaloc(paraloc: pcgparalocation): boolean;
  216. begin
  217. { "byval" is broken for register paras on several platforms in llvm
  218. (search for "byval" in llvm's bug tracker). Additionally, it should only
  219. be used to pass aggregate parameters on the stack, because it reportedly
  220. inhibits llvm's midlevel optimizers.
  221. Exception (for now?): parameters that have special shifting
  222. requirements, because modelling those in llvm is not easy (and clang
  223. nor llvm-gcc seem to do so either) }
  224. result:=
  225. ((paraloc^.loc=LOC_REFERENCE) and
  226. llvmaggregatetype(paraloc^.def)) or
  227. ((paraloc^.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  228. (paraloc^.shiftval<>0))
  229. end;
  230. procedure llvmaddencodedabstractrecordtype(def: tabstractrecorddef; var encodedstr: TSymStr); forward;
  231. type
  232. tllvmencodeflag = (lef_inaggregate, lef_noimplicitderef, lef_typedecl);
  233. tllvmencodeflags = set of tllvmencodeflag;
  234. procedure llvmaddencodedtype_intern(def: tdef; const flags: tllvmencodeflags; var encodedstr: TSymStr);
  235. var
  236. elesize: asizeint;
  237. begin
  238. case def.typ of
  239. stringdef :
  240. begin
  241. case tstringdef(def).stringtype of
  242. st_widestring,
  243. st_unicodestring:
  244. { the variable does not point to the header, but to a
  245. null-terminated string/array with undefined bounds }
  246. encodedstr:=encodedstr+'i16*';
  247. st_ansistring:
  248. encodedstr:=encodedstr+'i8*';
  249. st_shortstring:
  250. { length byte followed by string bytes }
  251. if tstringdef(def).len>0 then
  252. encodedstr:=encodedstr+'['+tostr(tstringdef(def).len+1)+' x i8]'
  253. else
  254. encodedstr:=encodedstr+'[0 x i8]';
  255. else
  256. internalerror(2013100201);
  257. end;
  258. end;
  259. enumdef:
  260. begin
  261. encodedstr:=encodedstr+'i'+tostr(def.size*8);
  262. end;
  263. orddef :
  264. begin
  265. if is_void(def) then
  266. encodedstr:=encodedstr+'void'
  267. { mainly required because comparison operations return i1, and
  268. otherwise we always have to immediatel extend them to i8 for
  269. no good reason; besides, Pascal booleans can only contain 0
  270. or 1 in valid code anyway (famous last words...) }
  271. else if torddef(def).ordtype=pasbool8 then
  272. encodedstr:=encodedstr+'i1'
  273. else
  274. encodedstr:=encodedstr+'i'+tostr(def.size*8);
  275. end;
  276. pointerdef :
  277. begin
  278. if is_voidpointer(def) then
  279. encodedstr:=encodedstr+'i8*'
  280. else
  281. begin
  282. llvmaddencodedtype_intern(tpointerdef(def).pointeddef,[],encodedstr);
  283. encodedstr:=encodedstr+'*';
  284. end;
  285. end;
  286. floatdef :
  287. begin
  288. case tfloatdef(def).floattype of
  289. s32real:
  290. encodedstr:=encodedstr+'float';
  291. s64real:
  292. encodedstr:=encodedstr+'double';
  293. { necessary to be able to force our own size/alignment }
  294. s80real:
  295. { prevent llvm from allocating the standard ABI size for
  296. extended }
  297. if lef_inaggregate in flags then
  298. encodedstr:=encodedstr+'[10 x i8]'
  299. else
  300. encodedstr:=encodedstr+'x86_fp80';
  301. sc80real:
  302. encodedstr:=encodedstr+'x86_fp80';
  303. s64comp,
  304. s64currency:
  305. encodedstr:=encodedstr+'i64';
  306. s128real:
  307. {$if defined(powerpc) or defined(powerpc128)}
  308. encodedstr:=encodedstr+'ppc_fp128';
  309. {$else}
  310. encodedstr:=encodedstr+'fp128';
  311. {$endif}
  312. else
  313. internalerror(2013100202);
  314. end;
  315. end;
  316. filedef :
  317. begin
  318. case tfiledef(def).filetyp of
  319. ft_text :
  320. llvmaddencodedtype_intern(search_system_type('TEXTREC').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  321. ft_typed,
  322. ft_untyped :
  323. llvmaddencodedtype_intern(search_system_type('FILEREC').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  324. else
  325. internalerror(2013100203);
  326. end;
  327. end;
  328. recorddef :
  329. begin
  330. { avoid endlessly recursive definitions }
  331. if assigned(def.typesym) and
  332. ((lef_inaggregate in flags) or
  333. not(lef_typedecl in flags)) then
  334. encodedstr:=encodedstr+llvmtypeidentifier(def)
  335. else
  336. llvmaddencodedabstractrecordtype(trecorddef(def),encodedstr);
  337. end;
  338. variantdef :
  339. begin
  340. llvmaddencodedtype_intern(search_system_type('TVARDATA').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  341. end;
  342. classrefdef :
  343. begin
  344. if is_class(tclassrefdef(def).pointeddef) then
  345. begin
  346. llvmaddencodedtype_intern(tobjectdef(tclassrefdef(def).pointeddef).vmt_def,flags,encodedstr);
  347. encodedstr:=encodedstr+'*';
  348. end
  349. else if is_objcclass(tclassrefdef(def).pointeddef) then
  350. llvmaddencodedtype_intern(objc_idtype,flags,encodedstr)
  351. else
  352. encodedstr:=encodedstr+'i8*'
  353. end;
  354. setdef :
  355. begin
  356. { just an array as far as llvm is concerned; don't use a "packed
  357. array of i1" or so, this requires special support in backends
  358. and guarantees nothing about the internal format }
  359. if is_smallset(def) then
  360. llvmaddencodedtype_intern(cgsize_orddef(def_cgsize(def)),[lef_inaggregate],encodedstr)
  361. else
  362. encodedstr:=encodedstr+'['+tostr(tsetdef(def).size)+' x i8]';
  363. end;
  364. formaldef :
  365. begin
  366. { var/const/out x (always treated as "pass by reference" -> don't
  367. add extra "*" here) }
  368. encodedstr:=encodedstr+'i8';
  369. end;
  370. arraydef :
  371. begin
  372. if is_array_of_const(def) then
  373. begin
  374. encodedstr:=encodedstr+'[0 x ';
  375. llvmaddencodedtype_intern(search_system_type('TVARREC').typedef,[lef_inaggregate],encodedstr);
  376. encodedstr:=encodedstr+']';
  377. end
  378. else if is_open_array(def) then
  379. begin
  380. encodedstr:=encodedstr+'[0 x ';
  381. llvmaddencodedtype_intern(tarraydef(def).elementdef,[lef_inaggregate],encodedstr);
  382. encodedstr:=encodedstr+']';
  383. end
  384. else if is_dynamic_array(def) then
  385. begin
  386. llvmaddencodedtype_intern(tarraydef(def).elementdef,[],encodedstr);
  387. encodedstr:=encodedstr+'*';
  388. end
  389. else if is_packed_array(def) and
  390. (tarraydef(def).elementdef.typ in [enumdef,orddef]) then
  391. begin
  392. elesize:=packedbitsloadsize(tarraydef(def).elementdef.packedbitsize);
  393. encodedstr:=encodedstr+'['+tostr(tarraydef(def).size div elesize)+' x ';
  394. { encode as an array of integers with the size on which we
  395. perform the packedbits operations }
  396. llvmaddencodedtype_intern(cgsize_orddef(int_cgsize(elesize)),[lef_inaggregate],encodedstr);
  397. encodedstr:=encodedstr+']';
  398. end
  399. else
  400. begin
  401. encodedstr:=encodedstr+'['+tostr(tarraydef(def).elecount)+' x ';
  402. llvmaddencodedtype_intern(tarraydef(def).elementdef,[lef_inaggregate],encodedstr);
  403. encodedstr:=encodedstr+']';
  404. end;
  405. end;
  406. procdef,
  407. procvardef :
  408. begin
  409. if (def.typ=procdef) or
  410. tprocvardef(def).is_addressonly then
  411. begin
  412. llvmaddencodedproctype(tabstractprocdef(def),'',lpd_procvar,encodedstr);
  413. if def.typ=procvardef then
  414. encodedstr:=encodedstr+'*';
  415. end
  416. else if ((lef_inaggregate in flags) or
  417. not(lef_typedecl in flags)) and
  418. assigned(tprocvardef(def).typesym) then
  419. begin
  420. { in case the procvardef recursively references itself, e.g.
  421. via a pointer }
  422. encodedstr:=encodedstr+llvmtypeidentifier(def)
  423. end
  424. else
  425. begin
  426. encodedstr:=encodedstr+'<{';
  427. { code pointer }
  428. llvmaddencodedproctype(tabstractprocdef(def),'',lpd_procvar,encodedstr);
  429. { data pointer (maybe todo: generate actual layout if
  430. available) }
  431. encodedstr:=encodedstr+'*, i8*}>';
  432. end;
  433. end;
  434. objectdef :
  435. case tobjectdef(def).objecttype of
  436. odt_class,
  437. odt_objcclass,
  438. odt_object,
  439. odt_cppclass:
  440. begin
  441. if not(lef_typedecl in flags) and
  442. assigned(def.typesym) then
  443. encodedstr:=encodedstr+llvmtypeidentifier(def)
  444. else
  445. llvmaddencodedabstractrecordtype(tabstractrecorddef(def),encodedstr);
  446. if ([lef_typedecl,lef_noimplicitderef]*flags=[]) and
  447. is_implicit_pointer_object_type(def) then
  448. encodedstr:=encodedstr+'*'
  449. end;
  450. odt_interfacecom,
  451. odt_interfacecorba,
  452. odt_dispinterface:
  453. begin
  454. { type is a pointer to a pointer to the vmt }
  455. llvmaddencodedtype_intern(tobjectdef(def).vmt_def,flags,encodedstr);
  456. if ([lef_typedecl,lef_noimplicitderef]*flags=[]) then
  457. encodedstr:=encodedstr+'**';
  458. end;
  459. odt_interfacecom_function,
  460. odt_interfacecom_property,
  461. odt_objcprotocol:
  462. begin
  463. { opaque for now }
  464. encodedstr:=encodedstr+'i8*'
  465. end;
  466. else
  467. internalerror(2013100601);
  468. end;
  469. undefineddef,
  470. errordef :
  471. internalerror(2013100604);
  472. else
  473. internalerror(2013100603);
  474. end;
  475. end;
  476. function llvmencodetypename(def: tdef): TSymStr;
  477. begin
  478. result:='';
  479. llvmaddencodedtype_intern(def,[],result);
  480. end;
  481. procedure llvmaddencodedtype(def: tdef; inaggregate: boolean; var encodedstr: TSymStr);
  482. var
  483. flags: tllvmencodeflags;
  484. begin
  485. if inaggregate then
  486. flags:=[lef_inaggregate]
  487. else
  488. flags:=[];
  489. llvmaddencodedtype_intern(def,flags,encodedstr);
  490. end;
  491. procedure llvmaddencodedabstractrecordtype(def: tabstractrecorddef; var encodedstr: TSymStr);
  492. var
  493. st: tllvmshadowsymtable;
  494. symdeflist: tfpobjectlist;
  495. i: longint;
  496. nopacked: boolean;
  497. begin
  498. st:=tabstractrecordsymtable(def.symtable).llvmst;
  499. symdeflist:=st.symdeflist;
  500. nopacked:=df_llvm_no_struct_packing in def.defoptions;
  501. if nopacked then
  502. encodedstr:=encodedstr+'{ '
  503. else
  504. encodedstr:=encodedstr+'<{ ';
  505. if symdeflist.count>0 then
  506. begin
  507. i:=0;
  508. if (def.typ=objectdef) and
  509. assigned(tobjectdef(def).childof) and
  510. is_class_or_interface_or_dispinterface(tllvmshadowsymtableentry(symdeflist[0]).def) then
  511. begin
  512. { insert the struct for the class rather than a pointer to the struct }
  513. if (tllvmshadowsymtableentry(symdeflist[0]).def.typ<>objectdef) then
  514. internalerror(2008070601);
  515. llvmaddencodedtype_intern(tllvmshadowsymtableentry(symdeflist[0]).def,[lef_inaggregate,lef_noimplicitderef],encodedstr);
  516. inc(i);
  517. end;
  518. while i<symdeflist.count do
  519. begin
  520. if i<>0 then
  521. encodedstr:=encodedstr+', ';
  522. llvmaddencodedtype_intern(tllvmshadowsymtableentry(symdeflist[i]).def,[lef_inaggregate],encodedstr);
  523. inc(i);
  524. end;
  525. end;
  526. if nopacked then
  527. encodedstr:=encodedstr+' }'
  528. else
  529. encodedstr:=encodedstr+' }>';
  530. end;
  531. procedure llvmextractvalueextinfo(paradef: tdef; var paralocdef: tdef; out signext: tllvmvalueextension);
  532. begin
  533. { implicit zero/sign extension for ABI compliance? (yes, if the size
  534. of a paraloc is larger than the size of the entire parameter) }
  535. if is_ordinal(paradef) and
  536. is_ordinal(paralocdef) and
  537. (paradef.size<paralocdef.size) then
  538. begin
  539. paralocdef:=paradef;
  540. if is_signed(paradef) then
  541. signext:=lve_signext
  542. else
  543. signext:=lve_zeroext
  544. end
  545. else
  546. signext:=lve_none;
  547. end;
  548. procedure llvmaddencodedparaloctype(hp: tparavarsym; proccalloption: tproccalloption; withparaname, withattributes: boolean; var first: boolean; var encodedstr: TSymStr);
  549. var
  550. paraloc: PCGParaLocation;
  551. signext: tllvmvalueextension;
  552. usedef: tdef;
  553. begin
  554. if (proccalloption in cdecl_pocalls) and
  555. is_array_of_const(hp.vardef) then
  556. begin
  557. if not first then
  558. encodedstr:=encodedstr+', '
  559. else
  560. first:=false;
  561. encodedstr:=encodedstr+'...';
  562. exit
  563. end;
  564. if withparaname then
  565. paraloc:=hp.paraloc[calleeside].location
  566. else
  567. paraloc:=hp.paraloc[callerside].location;
  568. repeat
  569. usedef:=paraloc^.def;
  570. llvmextractvalueextinfo(hp.vardef,usedef,signext);
  571. { implicit zero/sign extension for ABI compliance? }
  572. if not first then
  573. encodedstr:=encodedstr+', '
  574. else
  575. first:=false;
  576. llvmaddencodedtype_intern(usedef,[],encodedstr);
  577. { in case signextstr<>'', there should be only one paraloc -> no need
  578. to clear (reason: it means that the paraloc is larger than the
  579. original parameter) }
  580. if withattributes then
  581. encodedstr:=encodedstr+llvmvalueextension2str[signext];
  582. { sret: hidden pointer for structured function result }
  583. if vo_is_funcret in hp.varoptions then
  584. begin
  585. { "sret" is only valid for the firstparameter, while in FPC this
  586. can sometimes be second one (self comes before). In general,
  587. this is not a problem: we can just leave out sret, which means
  588. the result will be a bit less well optimised), but it is for
  589. AArch64: there, the sret parameter must be passed in a different
  590. register (-> paranr_result is smaller than paranr_self for that
  591. platform in symconst) }
  592. {$ifdef aarch64}
  593. if not first then
  594. internalerror(2015101404);
  595. {$endif aarch64}
  596. if withattributes then
  597. if first then
  598. encodedstr:=encodedstr+' sret'
  599. else { we can add some other attributes to optimise things,}
  600. encodedstr:=encodedstr+' noalias nocapture';
  601. end
  602. else if not paramanager.push_addr_param(hp.varspez,hp.vardef,proccalloption) and
  603. llvmbyvalparaloc(paraloc) then
  604. begin
  605. if withattributes then
  606. encodedstr:=encodedstr+'* byval'
  607. else
  608. encodedstr:=encodedstr+'*';
  609. end;
  610. if withparaname then
  611. begin
  612. if paraloc^.llvmloc.loc<>LOC_REFERENCE then
  613. internalerror(2014010803);
  614. encodedstr:=encodedstr+' '+llvmasmsymname(paraloc^.llvmloc.sym);
  615. end;
  616. paraloc:=paraloc^.next;
  617. until not assigned(paraloc);
  618. end;
  619. function llvmencodeproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype): TSymStr;
  620. begin
  621. result:='';
  622. llvmaddencodedproctype(def,customname,pddecltype,result);
  623. end;
  624. procedure llvmaddencodedproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype; var encodedstr: TSymStr);
  625. var
  626. usedef: tdef;
  627. paranr: longint;
  628. hp: tparavarsym;
  629. signext: tllvmvalueextension;
  630. useside: tcallercallee;
  631. first: boolean;
  632. begin
  633. { when writing a definition, we have to write the parameter names, and
  634. those are only available on the callee side. In all other cases,
  635. we are at the callerside }
  636. if pddecltype=lpd_def then
  637. useside:=calleeside
  638. else
  639. useside:=callerside;
  640. def.init_paraloc_info(useside);
  641. first:=true;
  642. { function result (return-by-ref is handled explicitly) }
  643. if not paramanager.ret_in_param(def.returndef,def) then
  644. begin
  645. usedef:=llvmgetcgparadef(def.funcretloc[useside],false);
  646. llvmextractvalueextinfo(def.returndef,usedef,signext);
  647. { specifying result sign extention information for an alias causes
  648. an error for some reason }
  649. if pddecltype in [lpd_decl,lpd_def] then
  650. encodedstr:=encodedstr+llvmvalueextension2str[signext];
  651. encodedstr:=encodedstr+' ';
  652. llvmaddencodedtype_intern(usedef,[],encodedstr);
  653. end
  654. else
  655. begin
  656. encodedstr:=encodedstr+' ';
  657. llvmaddencodedtype(voidtype,false,encodedstr);
  658. end;
  659. encodedstr:=encodedstr+' ';
  660. { add procname? }
  661. if (pddecltype in [lpd_decl,lpd_def]) and
  662. (def.typ=procdef) then
  663. if customname='' then
  664. encodedstr:=encodedstr+llvmmangledname(tprocdef(def).mangledname)
  665. else
  666. encodedstr:=encodedstr+llvmmangledname(customname);
  667. encodedstr:=encodedstr+'(';
  668. { parameters }
  669. first:=true;
  670. for paranr:=0 to def.paras.count-1 do
  671. begin
  672. hp:=tparavarsym(def.paras[paranr]);
  673. llvmaddencodedparaloctype(hp,def.proccalloption,pddecltype in [lpd_def],not(pddecltype in [lpd_procvar,lpd_alias]),first,encodedstr);
  674. end;
  675. if po_varargs in def.procoptions then
  676. begin
  677. if not first then
  678. encodedstr:=encodedstr+', ';
  679. encodedstr:=encodedstr+'...';
  680. end;
  681. encodedstr:=encodedstr+')'
  682. end;
  683. function llvmgettemprecorddef(fieldtypes: tfplist; packrecords, recordalignmin, maxcrecordalign: shortint): trecorddef;
  684. var
  685. i: longint;
  686. res: PHashSetItem;
  687. oldsymtablestack: tsymtablestack;
  688. hrecst: trecordsymtable;
  689. hdef: tdef;
  690. hrecdef: trecorddef;
  691. sym: tfieldvarsym;
  692. typename: string;
  693. begin
  694. typename:=internaltypeprefixName[itp_llvmstruct];
  695. for i:=0 to fieldtypes.count-1 do
  696. begin
  697. hdef:=tdef(fieldtypes[i]);
  698. case hdef.typ of
  699. orddef:
  700. case torddef(hdef).ordtype of
  701. s8bit,
  702. u8bit:
  703. typename:=typename+'i8';
  704. s16bit,
  705. u16bit:
  706. typename:=typename+'i16';
  707. s32bit,
  708. u32bit:
  709. typename:=typename+'i32';
  710. s64bit,
  711. u64bit:
  712. typename:=typename+'i64';
  713. else
  714. { other types should not appear currently, add as needed }
  715. internalerror(2014012001);
  716. end;
  717. floatdef:
  718. case tfloatdef(hdef).floattype of
  719. s32real:
  720. typename:=typename+'f32';
  721. s64real:
  722. typename:=typename+'f64';
  723. else
  724. { other types should not appear currently, add as needed }
  725. internalerror(2014012008);
  726. end;
  727. pointerdef:
  728. typename:='p'+tostr(hdef.deflist_index);
  729. else
  730. { other types should not appear currently, add as needed }
  731. internalerror(2014012009);
  732. end;
  733. end;
  734. if not assigned(current_module) then
  735. internalerror(2014012002);
  736. res:=current_module.llvmdefs.FindOrAdd(@typename[1],length(typename));
  737. if not assigned(res^.Data) then
  738. begin
  739. res^.Data:=crecorddef.create_global_internal(typename,packrecords,
  740. recordalignmin,maxcrecordalign);
  741. for i:=0 to fieldtypes.count-1 do
  742. trecorddef(res^.Data).add_field_by_def('F'+tostr(i),tdef(fieldtypes[i]));
  743. end;
  744. trecordsymtable(trecorddef(res^.Data).symtable).addalignmentpadding;
  745. result:=trecorddef(res^.Data);
  746. end;
  747. function llvmgetcgparadef(const cgpara: tcgpara; beforevalueext: boolean): tdef;
  748. var
  749. retdeflist: tfplist;
  750. retloc: pcgparalocation;
  751. usedef: tdef;
  752. valueext: tllvmvalueextension;
  753. begin
  754. { single location }
  755. if not assigned(cgpara.location^.next) then
  756. begin
  757. { def of the location, except in case of zero/sign-extension }
  758. usedef:=cgpara.location^.def;
  759. if beforevalueext then
  760. llvmextractvalueextinfo(cgpara.def,usedef,valueext);
  761. { comp and currency are handled by the x87 in this case. They cannot
  762. be represented directly in llvm, and llvmdef translates them into
  763. i64 (since that's their storage size and internally they also are
  764. int64). Solve this by changing the type to s80real in the
  765. returndef/parameter declaration. }
  766. if (usedef.typ=floatdef) and
  767. (tfloatdef(usedef).floattype in [s64comp,s64currency]) then
  768. usedef:=s80floattype;
  769. result:=usedef;
  770. exit
  771. end;
  772. { multiple locations -> create temp record }
  773. retdeflist:=tfplist.create;
  774. retloc:=cgpara.location;
  775. repeat
  776. retdeflist.add(retloc^.def);
  777. retloc:=retloc^.next;
  778. until not assigned(retloc);
  779. result:=llvmgettemprecorddef(retdeflist,C_alignment,
  780. targetinfos[target_info.system]^.alignment.recordalignmin,
  781. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  782. include(result.defoptions,df_llvm_no_struct_packing);
  783. end;
  784. function llvmencodetypedecl(def: tdef): TSymStr;
  785. begin
  786. result:='';
  787. llvmaddencodedtype_intern(def,[lef_typedecl],result);
  788. end;
  789. end.