llvmdef.pas 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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 or
  29. declaration of an external routine that's called in the current one
  30. b) alias declaration of a procdef implemented in the current module
  31. c) 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) no information about sign extension of result type, proc name, no parameter names, information about sign extension of parameters, parameter types
  35. c) no information about sign extension of result type, no proc name, no parameter names, no information about sign extension of parameters, parameter types
  36. }
  37. tllvmprocdefdecltype = (lpd_decl,lpd_alias,lpd_procvar);
  38. { returns the identifier to use as typename for a def in llvm (llvm only
  39. allows naming struct types) -- only supported for defs with a typesym, and
  40. only for tabstractrecorddef descendantds and complex procvars }
  41. function llvmtypeidentifier(def: tdef): TSymStr;
  42. { encode a type into the internal format used by LLVM (for a type
  43. declaration) }
  44. function llvmencodetypedecl(def: tdef): TSymStr;
  45. { same as above, but use a type name if possible (for any use) }
  46. function llvmencodetypename(def: tdef): TSymStr;
  47. { encode a procdef/procvardef into the internal format used by LLVM }
  48. function llvmencodeproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype): TSymStr;
  49. { incremental version of the above }
  50. procedure llvmaddencodedproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype; var encodedstr: TSymStr);
  51. { function result types may have to be represented differently, e.g. a
  52. record consisting of 4 longints must be returned as a record consisting of
  53. two int64's on x86-64. This function is used to create (and reuse)
  54. temporary recorddefs for such purposes.}
  55. function llvmgettemprecorddef(fieldtypes: tfplist; packrecords, recordalignmin, maxcrecordalign: shortint): trecorddef;
  56. { get the llvm type corresponding to a parameter, e.g. a record containing
  57. two integer int64 for an arbitrary record split over two individual int64
  58. parameters, or an int32 for an int16 parameter on a platform that requires
  59. such parameters to be zero/sign extended. The second parameter can be used
  60. to get the type before zero/sign extension, as e.g. required to generate
  61. function declarations. }
  62. function llvmgetcgparadef(const cgpara: tcgpara; beforevalueext: boolean): tdef;
  63. { can be used to extract the value extension info from acgpara. Pass in
  64. the def of the cgpara as first parameter and a local variable holding
  65. a copy of the def of the location (value extension only makes sense for
  66. ordinal parameters that are smaller than a single location). The routine
  67. will return the def of the location without sign extension (if applicable)
  68. and the kind of sign extension that was originally performed in the
  69. signext parameter }
  70. procedure llvmextractvalueextinfo(paradef: tdef; var paralocdef: tdef; out signext: tllvmvalueextension);
  71. { returns whether a paraloc should be translated into an llvm "byval"
  72. parameter. These are declared as pointers to a particular type, but
  73. usually turned into copies onto the stack. The exact behaviour for
  74. parameters that should be passed in registers is undefined and depends on
  75. the platform, and furthermore this modifier sometimes inhibits
  76. optimizations. As a result,we only use it for aggregate parameters of
  77. which we know that they should be passed on the stack }
  78. function llvmbyvalparaloc(paraloc: pcgparalocation): boolean;
  79. { returns whether a def is representated by an aggregate type in llvm
  80. (struct, array) }
  81. function llvmaggregatetype(def: tdef): boolean;
  82. function llvmconvop(fromsize, tosize: tdef): tllvmop;
  83. { mangle a global identifier so that it's recognised by LLVM as a global
  84. (in the sense of module-global) label and so that it won't mangle the
  85. name further according to platform conventions (we already did that) }
  86. function llvmmangledname(const s: TSymStr): TSymStr;
  87. function llvmasmsymname(const sym: TAsmSymbol): TSymStr;
  88. implementation
  89. uses
  90. cutils,constexp,
  91. verbose,systems,
  92. fmodule,
  93. symtable,symconst,symsym,
  94. llvmsym,hlcgobj,
  95. defutil,cgbase,paramgr;
  96. {******************************************************************
  97. Type encoding
  98. *******************************************************************}
  99. function llvmtypeidentifier(def: tdef): TSymStr;
  100. begin
  101. if not assigned(def.typesym) then
  102. internalerror(2015041901);
  103. result:='%"typ.'+def.fullownerhierarchyname+'.'+def.typesym.realname+'"'
  104. end;
  105. function llvmaggregatetype(def: tdef): boolean;
  106. begin
  107. result:=
  108. (def.typ in [recorddef,filedef,variantdef]) or
  109. ((def.typ=arraydef) and
  110. not is_dynamic_array(def)) or
  111. ((def.typ=setdef) and
  112. not is_smallset(def)) or
  113. is_shortstring(def) or
  114. is_object(def) or
  115. ((def.typ=procvardef) and
  116. not tprocvardef(def).is_addressonly)
  117. end;
  118. function llvmconvop(fromsize, tosize: tdef): tllvmop;
  119. var
  120. fromregtyp,
  121. toregtyp: tregistertype;
  122. frombytesize,
  123. tobytesize: asizeint;
  124. begin
  125. fromregtyp:=chlcgobj.def2regtyp(fromsize);
  126. toregtyp:=chlcgobj.def2regtyp(tosize);
  127. { int to pointer or vice versa }
  128. if fromregtyp=R_ADDRESSREGISTER then
  129. begin
  130. case toregtyp of
  131. R_INTREGISTER:
  132. result:=la_ptrtoint;
  133. R_ADDRESSREGISTER:
  134. result:=la_bitcast;
  135. else
  136. result:=la_ptrtoint_to_x;
  137. end;
  138. end
  139. else if toregtyp=R_ADDRESSREGISTER then
  140. begin
  141. case fromregtyp of
  142. R_INTREGISTER:
  143. result:=la_inttoptr;
  144. R_ADDRESSREGISTER:
  145. result:=la_bitcast;
  146. else
  147. result:=la_x_to_inttoptr;
  148. end;
  149. end
  150. else
  151. begin
  152. frombytesize:=fromsize.size;
  153. tobytesize:=tosize.size;
  154. { need zero/sign extension, float truncation or plain bitcast? }
  155. if tobytesize<>frombytesize then
  156. begin
  157. case fromregtyp of
  158. R_FPUREGISTER,
  159. R_MMREGISTER:
  160. begin
  161. { todo: update once we support vectors }
  162. if not(toregtyp in [R_FPUREGISTER,R_MMREGISTER]) then
  163. internalerror(2014062203);
  164. if tobytesize<frombytesize then
  165. result:=la_fptrunc
  166. else
  167. result:=la_fpext
  168. end;
  169. else
  170. begin
  171. if tobytesize<frombytesize then
  172. result:=la_trunc
  173. else if is_signed(fromsize) then
  174. { fromsize is signed -> sign extension }
  175. result:=la_sext
  176. else
  177. result:=la_zext;
  178. end;
  179. end;
  180. end
  181. else
  182. result:=la_bitcast;
  183. end;
  184. end;
  185. function llvmmangledname(const s: TSymStr): TSymStr;
  186. begin
  187. result:='@"\01'+s+'"';
  188. end;
  189. function llvmasmsymname(const sym: TAsmSymbol): TSymStr;
  190. begin
  191. { AT_ADDR and AT_LABEL represent labels in the code, which have
  192. a different type in llvm compared to (global) data labels }
  193. if sym.bind=AB_TEMP then
  194. result:='%'+sym.name
  195. else if not(sym.typ in [AT_LABEL,AT_ADDR]) then
  196. result:=llvmmangledname(sym.name)
  197. else
  198. result:='label %'+sym.name;
  199. end;
  200. function llvmbyvalparaloc(paraloc: pcgparalocation): boolean;
  201. begin
  202. { "byval" is broken for register paras on several platforms in llvm
  203. (search for "byval" in llvm's bug tracker). Additionally, it should only
  204. be used to pass aggregate parameters on the stack, because it reportedly
  205. inhibits llvm's midlevel optimizers.
  206. Exception (for now?): parameters that have special shifting
  207. requirements, because modelling those in llvm is not easy (and clang
  208. nor llvm-gcc seem to do so either) }
  209. result:=
  210. ((paraloc^.loc=LOC_REFERENCE) and
  211. llvmaggregatetype(paraloc^.def)) or
  212. (paraloc^.shiftval<>0)
  213. end;
  214. procedure llvmaddencodedabstractrecordtype(def: tabstractrecorddef; var encodedstr: TSymStr); forward;
  215. type
  216. tllvmencodeflag = (lef_inaggregate, lef_noimplicitderef, lef_typedecl);
  217. tllvmencodeflags = set of tllvmencodeflag;
  218. procedure llvmaddencodedtype_intern(def: tdef; const flags: tllvmencodeflags; var encodedstr: TSymStr);
  219. begin
  220. case def.typ of
  221. stringdef :
  222. begin
  223. case tstringdef(def).stringtype of
  224. st_widestring,
  225. st_unicodestring:
  226. { the variable does not point to the header, but to a
  227. null-terminated string/array with undefined bounds }
  228. encodedstr:=encodedstr+'i16*';
  229. st_ansistring:
  230. encodedstr:=encodedstr+'i8*';
  231. st_shortstring:
  232. { length byte followed by string bytes }
  233. if tstringdef(def).len>0 then
  234. encodedstr:=encodedstr+'['+tostr(tstringdef(def).len+1)+' x i8]'
  235. else
  236. encodedstr:=encodedstr+'[0 x i8]';
  237. else
  238. internalerror(2013100201);
  239. end;
  240. end;
  241. enumdef:
  242. begin
  243. encodedstr:=encodedstr+'i'+tostr(def.size*8);
  244. end;
  245. orddef :
  246. begin
  247. if is_void(def) then
  248. encodedstr:=encodedstr+'void'
  249. { mainly required because comparison operations return i1, and
  250. otherwise we always have to immediatel extend them to i8 for
  251. no good reason; besides, Pascal booleans can only contain 0
  252. or 1 in valid code anyway (famous last words...) }
  253. else if torddef(def).ordtype=pasbool8 then
  254. encodedstr:=encodedstr+'i1'
  255. else
  256. encodedstr:=encodedstr+'i'+tostr(def.size*8);
  257. end;
  258. pointerdef :
  259. begin
  260. if is_voidpointer(def) then
  261. encodedstr:=encodedstr+'i8*'
  262. else
  263. begin
  264. llvmaddencodedtype_intern(tpointerdef(def).pointeddef,[],encodedstr);
  265. encodedstr:=encodedstr+'*';
  266. end;
  267. end;
  268. floatdef :
  269. begin
  270. case tfloatdef(def).floattype of
  271. s32real:
  272. encodedstr:=encodedstr+'float';
  273. s64real:
  274. encodedstr:=encodedstr+'double';
  275. { necessary to be able to force our own size/alignment }
  276. s80real:
  277. { prevent llvm from allocating the standard ABI size for
  278. extended }
  279. if lef_inaggregate in flags then
  280. encodedstr:=encodedstr+'[10 x i8]'
  281. else
  282. encodedstr:=encodedstr+'x86_fp80';
  283. sc80real:
  284. encodedstr:=encodedstr+'x86_fp80';
  285. s64comp,
  286. s64currency:
  287. encodedstr:=encodedstr+'i64';
  288. s128real:
  289. {$if defined(powerpc) or defined(powerpc128)}
  290. encodedstr:=encodedstr+'ppc_fp128';
  291. {$else}
  292. encodedstr:=encodedstr+'fp128';
  293. {$endif}
  294. else
  295. internalerror(2013100202);
  296. end;
  297. end;
  298. filedef :
  299. begin
  300. case tfiledef(def).filetyp of
  301. ft_text :
  302. llvmaddencodedtype_intern(search_system_type('TEXTREC').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  303. ft_typed,
  304. ft_untyped :
  305. llvmaddencodedtype_intern(search_system_type('FILEREC').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  306. else
  307. internalerror(2013100203);
  308. end;
  309. end;
  310. recorddef :
  311. begin
  312. { avoid endlessly recursive definitions }
  313. if assigned(def.typesym) and
  314. ((lef_inaggregate in flags) or
  315. not(lef_typedecl in flags)) then
  316. encodedstr:=encodedstr+llvmtypeidentifier(def)
  317. else
  318. llvmaddencodedabstractrecordtype(trecorddef(def),encodedstr);
  319. end;
  320. variantdef :
  321. begin
  322. llvmaddencodedtype_intern(search_system_type('TVARDATA').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  323. end;
  324. classrefdef :
  325. begin
  326. { todo: define proper type for VMT and use that }
  327. encodedstr:=encodedstr+'i8*';
  328. end;
  329. setdef :
  330. begin
  331. { just an array as far as llvm is concerned; don't use a "packed
  332. array of i1" or so, this requires special support in backends
  333. and guarantees nothing about the internal format }
  334. if is_smallset(def) then
  335. llvmaddencodedtype_intern(cgsize_orddef(def_cgsize(def)),[lef_inaggregate],encodedstr)
  336. else
  337. encodedstr:=encodedstr+'['+tostr(tsetdef(def).size)+' x i8]';
  338. end;
  339. formaldef :
  340. begin
  341. { var/const/out x (always treated as "pass by reference" -> don't
  342. add extra "*" here) }
  343. encodedstr:=encodedstr+'i8';
  344. end;
  345. arraydef :
  346. begin
  347. if is_array_of_const(def) then
  348. begin
  349. encodedstr:=encodedstr+'[0 x ';
  350. llvmaddencodedtype_intern(search_system_type('TVARREC').typedef,[lef_inaggregate],encodedstr);
  351. encodedstr:=encodedstr+']';
  352. end
  353. else if is_open_array(def) then
  354. begin
  355. encodedstr:=encodedstr+'[0 x ';
  356. llvmaddencodedtype_intern(tarraydef(def).elementdef,[lef_inaggregate],encodedstr);
  357. encodedstr:=encodedstr+']';
  358. end
  359. else if is_dynamic_array(def) then
  360. begin
  361. llvmaddencodedtype_intern(tarraydef(def).elementdef,[],encodedstr);
  362. encodedstr:=encodedstr+'*';
  363. end
  364. else if is_packed_array(def) then
  365. begin
  366. encodedstr:=encodedstr+'['+tostr(tarraydef(def).size div tarraydef(def).elementdef.packedbitsize)+' x ';
  367. { encode as an array of integers with the size on which we
  368. perform the packedbits operations }
  369. llvmaddencodedtype_intern(cgsize_orddef(int_cgsize(packedbitsloadsize(tarraydef(def).elementdef.packedbitsize))),[lef_inaggregate],encodedstr);
  370. encodedstr:=encodedstr+']';
  371. end
  372. else
  373. begin
  374. encodedstr:=encodedstr+'['+tostr(tarraydef(def).elecount)+' x ';
  375. llvmaddencodedtype_intern(tarraydef(def).elementdef,[lef_inaggregate],encodedstr);
  376. encodedstr:=encodedstr+']';
  377. end;
  378. end;
  379. procdef,
  380. procvardef :
  381. begin
  382. if (def.typ=procdef) or
  383. tprocvardef(def).is_addressonly then
  384. begin
  385. llvmaddencodedproctype(tabstractprocdef(def),'',lpd_procvar,encodedstr);
  386. if def.typ=procvardef then
  387. encodedstr:=encodedstr+'*';
  388. end
  389. else if ((lef_inaggregate in flags) or
  390. not(lef_typedecl in flags)) and
  391. assigned(tprocvardef(def).typesym) then
  392. begin
  393. { in case the procvardef recursively references itself, e.g.
  394. via a pointer }
  395. encodedstr:=encodedstr+llvmtypeidentifier(def)
  396. end
  397. else
  398. begin
  399. encodedstr:=encodedstr+'{';
  400. { code pointer }
  401. llvmaddencodedproctype(tabstractprocdef(def),'',lpd_procvar,encodedstr);
  402. { data pointer (maybe todo: generate actual layout if
  403. available) }
  404. encodedstr:=encodedstr+'*, i8*}';
  405. end;
  406. end;
  407. objectdef :
  408. case tobjectdef(def).objecttype of
  409. odt_class,
  410. odt_objcclass,
  411. odt_object,
  412. odt_cppclass:
  413. begin
  414. if not(lef_typedecl in flags) and
  415. assigned(def.typesym) then
  416. encodedstr:=encodedstr+llvmtypeidentifier(def)
  417. else
  418. llvmaddencodedabstractrecordtype(tabstractrecorddef(def),encodedstr);
  419. if ([lef_typedecl,lef_noimplicitderef]*flags=[]) and
  420. is_implicit_pointer_object_type(def) then
  421. encodedstr:=encodedstr+'*'
  422. end;
  423. odt_interfacecom,
  424. odt_interfacecom_function,
  425. odt_interfacecom_property,
  426. odt_interfacecorba,
  427. odt_dispinterface,
  428. odt_objcprotocol:
  429. begin
  430. { opaque for now }
  431. encodedstr:=encodedstr+'i8*'
  432. end;
  433. else
  434. internalerror(2013100601);
  435. end;
  436. undefineddef,
  437. errordef :
  438. internalerror(2013100604);
  439. else
  440. internalerror(2013100603);
  441. end;
  442. end;
  443. function llvmencodetypename(def: tdef): TSymStr;
  444. begin
  445. result:='';
  446. llvmaddencodedtype_intern(def,[],result);
  447. end;
  448. procedure llvmaddencodedtype(def: tdef; inaggregate: boolean; var encodedstr: TSymStr);
  449. var
  450. flags: tllvmencodeflags;
  451. begin
  452. if inaggregate then
  453. flags:=[lef_inaggregate]
  454. else
  455. flags:=[];
  456. llvmaddencodedtype_intern(def,flags,encodedstr);
  457. end;
  458. procedure llvmaddencodedabstractrecordtype(def: tabstractrecorddef; var encodedstr: TSymStr);
  459. var
  460. st: tllvmshadowsymtable;
  461. symdeflist: tfpobjectlist;
  462. i: longint;
  463. begin
  464. st:=tabstractrecordsymtable(def.symtable).llvmst;
  465. symdeflist:=st.symdeflist;
  466. if tabstractrecordsymtable(def.symtable).usefieldalignment<>C_alignment then
  467. encodedstr:=encodedstr+'<';
  468. encodedstr:=encodedstr+'{ ';
  469. if symdeflist.count>0 then
  470. begin
  471. i:=0;
  472. if (def.typ=objectdef) and
  473. assigned(tobjectdef(def).childof) and
  474. is_class_or_interface_or_dispinterface(tllvmshadowsymtableentry(symdeflist[0]).def) then
  475. begin
  476. { insert the struct for the class rather than a pointer to the struct }
  477. if (tllvmshadowsymtableentry(symdeflist[0]).def.typ<>objectdef) then
  478. internalerror(2008070601);
  479. llvmaddencodedtype_intern(tllvmshadowsymtableentry(symdeflist[0]).def,[lef_inaggregate,lef_noimplicitderef],encodedstr);
  480. inc(i);
  481. end;
  482. while i<symdeflist.count do
  483. begin
  484. if i<>0 then
  485. encodedstr:=encodedstr+', ';
  486. llvmaddencodedtype_intern(tllvmshadowsymtableentry(symdeflist[i]).def,[lef_inaggregate],encodedstr);
  487. inc(i);
  488. end;
  489. end;
  490. encodedstr:=encodedstr+' }';
  491. if tabstractrecordsymtable(def.symtable).usefieldalignment<>C_alignment then
  492. encodedstr:=encodedstr+'>';
  493. end;
  494. procedure llvmextractvalueextinfo(paradef: tdef; var paralocdef: tdef; out signext: tllvmvalueextension);
  495. begin
  496. { implicit zero/sign extension for ABI compliance? (yes, if the size
  497. of a paraloc is larger than the size of the entire parameter) }
  498. if is_ordinal(paradef) and
  499. is_ordinal(paralocdef) and
  500. (paradef.size<paralocdef.size) then
  501. begin
  502. paralocdef:=paradef;
  503. if is_signed(paradef) then
  504. signext:=lve_signext
  505. else
  506. signext:=lve_zeroext
  507. end
  508. else
  509. signext:=lve_none;
  510. end;
  511. procedure llvmaddencodedparaloctype(hp: tparavarsym; proccalloption: tproccalloption; withparaname, withattributes: boolean; var first: boolean; var encodedstr: TSymStr);
  512. var
  513. paraloc: PCGParaLocation;
  514. signext: tllvmvalueextension;
  515. usedef: tdef;
  516. begin
  517. if (proccalloption in cdecl_pocalls) and
  518. is_array_of_const(hp.vardef) then
  519. begin
  520. if not first then
  521. encodedstr:=encodedstr+', '
  522. else
  523. first:=false;
  524. encodedstr:=encodedstr+'...';
  525. exit
  526. end;
  527. paraloc:=hp.paraloc[calleeside].location;
  528. repeat
  529. usedef:=paraloc^.def;
  530. llvmextractvalueextinfo(hp.vardef,usedef,signext);
  531. { implicit zero/sign extension for ABI compliance? }
  532. if not first then
  533. encodedstr:=encodedstr+', '
  534. else
  535. first:=false;
  536. llvmaddencodedtype_intern(usedef,[lef_inaggregate],encodedstr);
  537. { in case signextstr<>'', there should be only one paraloc -> no need
  538. to clear (reason: it means that the paraloc is larger than the
  539. original parameter) }
  540. if withattributes then
  541. encodedstr:=encodedstr+llvmvalueextension2str[signext];
  542. { sret: hidden pointer for structured function result }
  543. if vo_is_funcret in hp.varoptions then
  544. begin
  545. if withattributes then
  546. encodedstr:=encodedstr+' sret'
  547. end
  548. else if not paramanager.push_addr_param(hp.varspez,hp.vardef,proccalloption) and
  549. llvmbyvalparaloc(paraloc) then
  550. begin
  551. if withattributes then
  552. encodedstr:=encodedstr+'* byval'
  553. else
  554. encodedstr:=encodedstr+'*';
  555. end;
  556. if withparaname then
  557. begin
  558. if paraloc^.llvmloc.loc<>LOC_REFERENCE then
  559. internalerror(2014010803);
  560. encodedstr:=encodedstr+' '+llvmasmsymname(paraloc^.llvmloc.sym);
  561. end;
  562. paraloc:=paraloc^.next;
  563. until not assigned(paraloc);
  564. end;
  565. function llvmencodeproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype): TSymStr;
  566. begin
  567. result:='';
  568. llvmaddencodedproctype(def,customname,pddecltype,result);
  569. end;
  570. procedure llvmaddencodedproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype; var encodedstr: TSymStr);
  571. var
  572. usedef: tdef;
  573. paranr: longint;
  574. hp: tparavarsym;
  575. signext: tllvmvalueextension;
  576. first: boolean;
  577. begin
  578. def.init_paraloc_info(calleeside);
  579. first:=true;
  580. { function result (return-by-ref is handled explicitly) }
  581. if not paramanager.ret_in_param(def.returndef,def) then
  582. begin
  583. usedef:=llvmgetcgparadef(def.funcretloc[calleeside],false);
  584. llvmextractvalueextinfo(def.returndef,usedef,signext);
  585. { specifying result sign extention information for an alias causes
  586. an error for some reason }
  587. if pddecltype in [lpd_decl] then
  588. encodedstr:=encodedstr+llvmvalueextension2str[signext];
  589. encodedstr:=encodedstr+' ';
  590. llvmaddencodedtype_intern(usedef,[lef_inaggregate],encodedstr);
  591. end
  592. else
  593. begin
  594. encodedstr:=encodedstr+' ';
  595. llvmaddencodedtype(voidtype,false,encodedstr);
  596. end;
  597. encodedstr:=encodedstr+' ';
  598. { add procname? }
  599. if (pddecltype in [lpd_decl]) and
  600. (def.typ=procdef) then
  601. if customname='' then
  602. encodedstr:=encodedstr+llvmmangledname(tprocdef(def).mangledname)
  603. else
  604. encodedstr:=encodedstr+llvmmangledname(customname);
  605. encodedstr:=encodedstr+'(';
  606. { parameters }
  607. first:=true;
  608. for paranr:=0 to def.paras.count-1 do
  609. begin
  610. hp:=tparavarsym(def.paras[paranr]);
  611. llvmaddencodedparaloctype(hp,def.proccalloption,pddecltype in [lpd_decl],not(pddecltype in [lpd_procvar]),first,encodedstr);
  612. end;
  613. if po_varargs in def.procoptions then
  614. begin
  615. if not first then
  616. encodedstr:=encodedstr+', ';
  617. encodedstr:=encodedstr+'...';
  618. end;
  619. encodedstr:=encodedstr+')'
  620. end;
  621. function llvmgettemprecorddef(fieldtypes: tfplist; packrecords, recordalignmin, maxcrecordalign: shortint): trecorddef;
  622. var
  623. i: longint;
  624. res: PHashSetItem;
  625. oldsymtablestack: tsymtablestack;
  626. hrecst: trecordsymtable;
  627. hdef: tdef;
  628. hrecdef: trecorddef;
  629. sym: tfieldvarsym;
  630. typename: string;
  631. begin
  632. typename:='$llvmstruct_';
  633. for i:=0 to fieldtypes.count-1 do
  634. begin
  635. hdef:=tdef(fieldtypes[i]);
  636. case hdef.typ of
  637. orddef:
  638. case torddef(hdef).ordtype of
  639. s8bit,
  640. u8bit:
  641. typename:=typename+'i8';
  642. s16bit,
  643. u16bit:
  644. typename:=typename+'i16';
  645. s32bit,
  646. u32bit:
  647. typename:=typename+'i32';
  648. s64bit,
  649. u64bit:
  650. typename:=typename+'i64';
  651. else
  652. { other types should not appear currently, add as needed }
  653. internalerror(2014012001);
  654. end;
  655. floatdef:
  656. case tfloatdef(hdef).floattype of
  657. s32real:
  658. typename:=typename+'f32';
  659. s64real:
  660. typename:=typename+'f64';
  661. else
  662. { other types should not appear currently, add as needed }
  663. internalerror(2014012008);
  664. end;
  665. else
  666. { other types should not appear currently, add as needed }
  667. internalerror(2014012009);
  668. end;
  669. end;
  670. if not assigned(current_module) then
  671. internalerror(2014012002);
  672. res:=current_module.llvmdefs.FindOrAdd(@typename[1],length(typename));
  673. if not assigned(res^.Data) then
  674. begin
  675. res^.Data:=crecorddef.create_global_internal(typename,packrecords,
  676. recordalignmin,maxcrecordalign);
  677. trecorddef(res^.Data).add_fields_from_deflist(fieldtypes);
  678. end;
  679. result:=trecorddef(res^.Data);
  680. end;
  681. function llvmgetcgparadef(const cgpara: tcgpara; beforevalueext: boolean): tdef;
  682. var
  683. retdeflist: tfplist;
  684. retloc: pcgparalocation;
  685. usedef: tdef;
  686. valueext: tllvmvalueextension;
  687. begin
  688. { single location }
  689. if not assigned(cgpara.location^.next) then
  690. begin
  691. { def of the location, except in case of zero/sign-extension }
  692. usedef:=cgpara.location^.def;
  693. if beforevalueext then
  694. llvmextractvalueextinfo(cgpara.def,usedef,valueext);
  695. result:=usedef;
  696. exit
  697. end;
  698. { multiple locations -> create temp record }
  699. retdeflist:=tfplist.create;
  700. retloc:=cgpara.location;
  701. repeat
  702. retdeflist.add(retloc^.def);
  703. retloc:=retloc^.next;
  704. until not assigned(retloc);
  705. result:=llvmgettemprecorddef(retdeflist,C_alignment,
  706. targetinfos[target_info.system]^.alignment.recordalignmin,
  707. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  708. end;
  709. function llvmencodetypedecl(def: tdef): TSymStr;
  710. begin
  711. result:='';
  712. llvmaddencodedtype_intern(def,[lef_typedecl],result);
  713. end;
  714. end.