asmutils.pas 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. {
  2. Copyright (c) 1998-2006 by Florian Klaempfl
  3. This unit contains utility functions for assembler output
  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 asmutils;
  18. interface
  19. {$i fpcdefs.inc}
  20. uses
  21. globtype,
  22. aasmbase,
  23. aasmdata,
  24. symconst;
  25. type
  26. tasmlabofs = record
  27. lab: tasmlabel;
  28. ofs: pint;
  29. end;
  30. function emit_ansistring_const(list:TAsmList;data:PChar;len:LongInt;encoding:tstringencoding;NewSection:Boolean=True):tasmlabofs;
  31. function emit_unicodestring_const(list:TAsmList;data:Pointer;encoding:tstringencoding;Winlike:Boolean):tasmlabofs;
  32. function get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint;
  33. implementation
  34. uses
  35. globals,
  36. systems,
  37. verbose,
  38. aasmtai,
  39. widestr,
  40. symdef;
  41. function emit_ansistring_const(list:TAsmList;data:PChar;len:LongInt;encoding:tstringencoding;NewSection:Boolean): tasmlabofs;
  42. var
  43. s: PChar;
  44. begin
  45. current_asmdata.getdatalabel(result.lab);
  46. result.ofs:=0;
  47. if NewSection then
  48. begin
  49. maybe_new_object_file(list);
  50. new_section(list,sec_rodata,result.lab.name,const_align(sizeof(pint)));
  51. end;
  52. { put label before header on Darwin, because there the linker considers
  53. a global symbol to be the start of a new subsection }
  54. if target_info.system in systems_darwin then
  55. list.concat(tai_label.create(result.lab));
  56. list.concat(tai_const.create_16bit(encoding));
  57. inc(result.ofs,2);
  58. list.concat(tai_const.create_16bit(1));
  59. inc(result.ofs,2);
  60. {$ifdef cpu64bitaddr}
  61. { dummy for alignment }
  62. list.concat(tai_const.create_32bit(0));
  63. inc(result.ofs,4);
  64. {$endif cpu64bitaddr}
  65. list.concat(tai_const.create_pint(-1));
  66. inc(result.ofs,sizeof(pint));
  67. list.concat(tai_const.create_pint(len));
  68. inc(result.ofs,sizeof(pint));
  69. if not(target_info.system in systems_darwin) then
  70. begin
  71. { results in slightly more efficient code }
  72. list.concat(tai_label.create(result.lab));
  73. result.ofs:=0;
  74. end;
  75. { sanity check }
  76. if result.ofs<>get_string_symofs(st_ansistring,false) then
  77. internalerror(2012051701);
  78. getmem(s,len+1);
  79. move(data^,s^,len);
  80. s[len]:=#0;
  81. list.concat(tai_string.create_pchar(s,len+1)); { terminating zero included }
  82. end;
  83. function emit_unicodestring_const(list:TAsmList;data:Pointer;encoding:tstringencoding;Winlike:Boolean):tasmlabofs;
  84. var
  85. i, strlength: SizeInt;
  86. begin
  87. current_asmdata.getdatalabel(result.lab);
  88. result.ofs:=0;
  89. maybe_new_object_file(list);
  90. new_section(list,sec_rodata,result.lab.name,const_align(sizeof(pint)));
  91. strlength := getlengthwidestring(pcompilerwidestring(data));
  92. if Winlike then
  93. begin
  94. list.concat(Tai_const.Create_32bit(strlength*cwidechartype.size));
  95. { don't increase result.ofs, this is how Windows widestrings are
  96. defined by the OS: a pointer 4 bytes past the length of the
  97. string }
  98. list.concat(Tai_label.Create(result.lab));
  99. end
  100. else
  101. begin
  102. { put label before header on Darwin, because there the linker considers
  103. a global symbol to be the start of a new subsection }
  104. if target_info.system in systems_darwin then
  105. list.concat(Tai_label.Create(result.lab));
  106. list.concat(tai_const.create_16bit(encoding));
  107. inc(result.ofs,2);
  108. list.concat(tai_const.create_16bit(2));
  109. inc(result.ofs,2);
  110. {$ifdef cpu64bitaddr}
  111. { dummy for alignment }
  112. list.concat(Tai_const.Create_32bit(0));
  113. inc(result.ofs,4);
  114. {$endif cpu64bitaddr}
  115. list.concat(Tai_const.Create_pint(-1));
  116. inc(result.ofs,sizeof(pint));
  117. list.concat(Tai_const.Create_pint(strlength));
  118. inc(result.ofs,sizeof(pint));
  119. if not(target_info.system in systems_darwin) then
  120. begin
  121. { results in slightly more efficient code }
  122. list.concat(tai_label.create(result.lab));
  123. result.ofs:=0;
  124. end;
  125. { sanity check }
  126. if result.ofs<>get_string_symofs(st_unicodestring,false) then
  127. internalerror(2012051702);
  128. end;
  129. if cwidechartype.size = 2 then
  130. begin
  131. for i:=0 to strlength-1 do
  132. list.concat(Tai_const.Create_16bit(pcompilerwidestring(data)^.data[i]));
  133. { ending #0 }
  134. list.concat(Tai_const.Create_16bit(0));
  135. end
  136. else
  137. InternalError(200904271); { codegeneration for other sizes must be written }
  138. end;
  139. function get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint;
  140. const
  141. ansistring_header_size =
  142. { encoding }
  143. 2 +
  144. { elesize }
  145. 2 +
  146. {$ifdef cpu64bitaddr}
  147. { alignment }
  148. 4 +
  149. {$endif cpu64bitaddr}
  150. { reference count }
  151. sizeof(pint) +
  152. { length }
  153. sizeof(pint);
  154. unicodestring_header_size = ansistring_header_size;
  155. begin
  156. if not(target_info.system in systems_darwin) then
  157. result:=0
  158. else case typ of
  159. st_ansistring:
  160. result:=ansistring_header_size;
  161. st_unicodestring:
  162. result:=unicodestring_header_size;
  163. st_widestring:
  164. if winlikewidestring then
  165. result:=0
  166. else
  167. result:=unicodestring_header_size;
  168. else
  169. result:=0;
  170. end;
  171. end;
  172. end.