tgcpu.pas 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. {
  2. Copyright (C) 2010 by Jonas Maebe
  3. This unit handles the temporary variables for the JVM
  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. {
  18. This unit handles the temporary variables for the JVM.
  19. }
  20. unit tgcpu;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. globtype,
  25. aasmdata,
  26. cgutils,
  27. symtype,tgobj;
  28. type
  29. { ttgjvm }
  30. ttgjvm = class(ttgobj)
  31. protected
  32. function getifspecialtemp(list: TAsmList; def: tdef; forcesize: aint; temptype: ttemptype; out ref: treference): boolean;
  33. function alloctemp(list: TAsmList; size, alignment: longint; temptype: ttemptype; def: tdef): longint; override;
  34. public
  35. procedure setfirsttemp(l : longint); override;
  36. procedure getlocal(list: TAsmList; size: longint; alignment: shortint; def: tdef; var ref: treference); override;
  37. procedure gethltemp(list: TAsmList; def: tdef; forcesize: aint; temptype: ttemptype; out ref: treference); override;
  38. end;
  39. implementation
  40. uses
  41. verbose,
  42. cgbase,
  43. symconst,defutil,
  44. hlcgobj,hlcgcpu,
  45. symdef;
  46. { ttgjvm }
  47. function ttgjvm.getifspecialtemp(list: TAsmList; def: tdef; forcesize: aint; temptype: ttemptype; out ref: treference): boolean;
  48. var
  49. eledef: tdef;
  50. ndim: longint;
  51. begin
  52. result:=false;
  53. case def.typ of
  54. arraydef:
  55. begin
  56. if not is_dynamic_array(def) then
  57. begin
  58. { allocate an array of the right size }
  59. gettemp(list,java_jlobject.size,java_jlobject.alignment,temptype,ref);
  60. ndim:=0;
  61. eledef:=def;
  62. repeat
  63. thlcgjvm(hlcg).a_load_const_stack(list,s32inttype,tarraydef(eledef).elecount,R_INTREGISTER);
  64. eledef:=tarraydef(eledef).elementdef;
  65. inc(ndim);
  66. until (eledef.typ<>arraydef) or
  67. is_dynamic_array(eledef);
  68. eledef:=tarraydef(def).elementdef;
  69. thlcgjvm(hlcg).g_newarray(list,def,ndim);
  70. thlcgjvm(hlcg).a_load_stack_ref(list,java_jlobject,ref,0);
  71. result:=true;
  72. end;
  73. end;
  74. end;
  75. end;
  76. function ttgjvm.alloctemp(list: TAsmList; size, alignment: longint; temptype: ttemptype; def: tdef): longint;
  77. begin
  78. { the JVM only supports 1 slot (= 4 bytes in FPC) and 2 slot (= 8 bytes in
  79. FPC) temps on the stack. double and int64 are 2 slots, the rest is one slot.
  80. There are no problems with reusing the same slot for a value of a different
  81. type. There are no alignment requirements either. }
  82. if size<4 then
  83. size:=4;
  84. if not(size in [4,8]) then
  85. internalerror(2010121401);
  86. { don't pass on "def", since we don't care if a slot is used again for a
  87. different type }
  88. result:=inherited alloctemp(list, size shr 2, 1, temptype, nil);
  89. end;
  90. procedure ttgjvm.setfirsttemp(l: longint);
  91. begin
  92. firsttemp:=l;
  93. lasttemp:=l;
  94. end;
  95. procedure ttgjvm.getlocal(list: TAsmList; size: longint; alignment: shortint; def: tdef; var ref: treference);
  96. begin
  97. if not getifspecialtemp(list,def,size,tt_persistent,ref) then
  98. inherited;
  99. end;
  100. procedure ttgjvm.gethltemp(list: TAsmList; def: tdef; forcesize: aint; temptype: ttemptype; out ref: treference);
  101. begin
  102. if not getifspecialtemp(list,def,forcesize,temptype,ref) then
  103. inherited;
  104. end;
  105. begin
  106. tgobjclass:=ttgjvm;
  107. end.