nllvminl.pas 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. {
  2. Copyright (c) 2014 by Jonas Maebe
  3. Generate LLVM bytecode for inline nodes
  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. unit nllvminl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,
  22. ncginl;
  23. type
  24. tllvminlinenode = class(tcginlinenode)
  25. protected
  26. function first_get_frame: tnode; override;
  27. public
  28. procedure second_length; override;
  29. end;
  30. implementation
  31. uses
  32. verbose,globtype,constexp,
  33. aasmbase, aasmdata,
  34. symtype,symdef,defutil,
  35. ncal,ncon,ninl,
  36. pass_2,
  37. cgbase,cgutils,tgobj,hlcgobj,
  38. cpubase;
  39. function tllvminlinenode.first_get_frame: tnode;
  40. begin
  41. result:=ccallnode.createintern('llvm_frameaddress',
  42. ccallparanode.create(genintconstnode(0),nil));
  43. end;
  44. procedure tllvminlinenode.second_length;
  45. var
  46. lengthlab, nillab: tasmlabel;
  47. hregister: tregister;
  48. href, tempref: treference;
  49. lendef: tdef;
  50. begin
  51. secondpass(left);
  52. if is_shortstring(left.resultdef) then
  53. begin
  54. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  55. internalerror(2014080806);
  56. { typecast the shortstring reference into a length byte reference }
  57. location_reset_ref(location,left.location.loc,def_cgsize(resultdef),left.location.reference.alignment);
  58. hregister:=hlcg.getaddressregister(current_asmdata.CurrAsmList,cpointerdef.getreusable(resultdef));
  59. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.resultdef,cpointerdef.getreusable(resultdef),left.location.reference,hregister);
  60. hlcg.reference_reset_base(location.reference,cpointerdef.getreusable(resultdef),hregister,0,left.location.reference.alignment);
  61. end
  62. else
  63. begin
  64. tg.gethltemp(current_asmdata.CurrAsmList,resultdef,resultdef.size,tt_normal,tempref);
  65. { length in ansi/wide strings and high in dynamic arrays is at offset
  66. -sizeof(sizeint), for widestrings it's at -4 }
  67. if is_widestring(left.resultdef) then
  68. lendef:=u32inttype
  69. else
  70. lendef:=ossinttype;
  71. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,
  72. left.resultdef,cpointerdef.getreusable(lendef),true);
  73. current_asmdata.getjumplabel(nillab);
  74. current_asmdata.getjumplabel(lengthlab);
  75. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,cpointerdef.getreusable(lendef),OC_EQ,0,left.location.register,nillab);
  76. hlcg.reference_reset_base(href,cpointerdef.getreusable(lendef),left.location.register,-lendef.size,lendef.alignment);
  77. hregister:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  78. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,lendef,resultdef,href,hregister);
  79. if is_widestring(left.resultdef) then
  80. hlcg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHR,resultdef,1,hregister);
  81. { Dynamic arrays do not have their length attached but their maximum index }
  82. if is_dynamic_array(left.resultdef) then
  83. hlcg.a_op_const_reg(current_asmdata.CurrAsmList,OP_ADD,resultdef,1,hregister);
  84. hlcg.a_load_reg_ref(current_asmdata.CurrAsmList,resultdef,resultdef,hregister,tempref);
  85. hlcg.a_jmp_always(current_asmdata.CurrAsmList,lengthlab);
  86. hlcg.a_label(current_asmdata.CurrAsmList,nillab);
  87. hlcg.a_load_const_ref(current_asmdata.CurrAsmList,resultdef,0,tempref);
  88. hlcg.a_label(current_asmdata.CurrAsmList,lengthlab);
  89. hregister:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  90. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,resultdef,resultdef,tempref,hregister);
  91. tg.ungettemp(current_asmdata.CurrAsmList,tempref);
  92. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  93. location.register:=hregister;
  94. end;
  95. end;
  96. begin
  97. cinlinenode:=tllvminlinenode;
  98. end.