asmutils.pas 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. new_section(list,sec_rodata,result.lab.name,const_align(sizeof(pint)));
  49. { put label before header on Darwin, because there the linker considers
  50. a global symbol to be the start of a new subsection }
  51. if target_info.system in systems_darwin then
  52. list.concat(tai_label.create(result.lab));
  53. list.concat(tai_const.create_16bit(encoding));
  54. inc(result.ofs,2);
  55. list.concat(tai_const.create_16bit(1));
  56. inc(result.ofs,2);
  57. {$ifdef cpu64bitaddr}
  58. { dummy for alignment }
  59. list.concat(tai_const.create_32bit(0));
  60. inc(result.ofs,4);
  61. {$endif cpu64bitaddr}
  62. list.concat(tai_const.create_pint(-1));
  63. inc(result.ofs,sizeof(pint));
  64. list.concat(tai_const.create_pint(len));
  65. inc(result.ofs,sizeof(pint));
  66. if not(target_info.system in systems_darwin) then
  67. begin
  68. { results in slightly more efficient code }
  69. list.concat(tai_label.create(result.lab));
  70. result.ofs:=0;
  71. end;
  72. { sanity check }
  73. if result.ofs<>get_string_symofs(st_ansistring,false) then
  74. internalerror(2012051701);
  75. getmem(s,len+1);
  76. move(data^,s^,len);
  77. s[len]:=#0;
  78. list.concat(tai_string.create_pchar(s,len+1)); { terminating zero included }
  79. end;
  80. function emit_unicodestring_const(list:TAsmList;data:Pointer;encoding:tstringencoding;Winlike:Boolean):tasmlabofs;
  81. var
  82. i, strlength: SizeInt;
  83. begin
  84. current_asmdata.getdatalabel(result.lab);
  85. result.ofs:=0;
  86. new_section(list,sec_rodata,result.lab.name,const_align(sizeof(pint)));
  87. strlength := getlengthwidestring(pcompilerwidestring(data));
  88. if Winlike then
  89. begin
  90. list.concat(Tai_const.Create_32bit(strlength*cwidechartype.size));
  91. { don't increase result.ofs, this is how Windows widestrings are
  92. defined by the OS: a pointer 4 bytes past the length of the
  93. string }
  94. list.concat(Tai_label.Create(result.lab));
  95. end
  96. else
  97. begin
  98. { put label before header on Darwin, because there the linker considers
  99. a global symbol to be the start of a new subsection }
  100. if target_info.system in systems_darwin then
  101. list.concat(Tai_label.Create(result.lab));
  102. list.concat(tai_const.create_16bit(encoding));
  103. inc(result.ofs,2);
  104. list.concat(tai_const.create_16bit(2));
  105. inc(result.ofs,2);
  106. {$ifdef cpu64bitaddr}
  107. { dummy for alignment }
  108. list.concat(Tai_const.Create_32bit(0));
  109. inc(result.ofs,4);
  110. {$endif cpu64bitaddr}
  111. list.concat(Tai_const.Create_pint(-1));
  112. inc(result.ofs,sizeof(pint));
  113. list.concat(Tai_const.Create_pint(strlength));
  114. inc(result.ofs,sizeof(pint));
  115. if not(target_info.system in systems_darwin) then
  116. begin
  117. { results in slightly more efficient code }
  118. list.concat(tai_label.create(result.lab));
  119. result.ofs:=0;
  120. end;
  121. { sanity check }
  122. if result.ofs<>get_string_symofs(st_unicodestring,false) then
  123. internalerror(2012051702);
  124. end;
  125. if cwidechartype.size = 2 then
  126. begin
  127. for i:=0 to strlength-1 do
  128. list.concat(Tai_const.Create_16bit(pcompilerwidestring(data)^.data[i]));
  129. { ending #0 }
  130. list.concat(Tai_const.Create_16bit(0));
  131. end
  132. else
  133. InternalError(200904271); { codegeneration for other sizes must be written }
  134. end;
  135. function get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint;
  136. const
  137. ansistring_header_size =
  138. { encoding }
  139. 2 +
  140. { elesize }
  141. 2 +
  142. {$ifdef cpu64bitaddr}
  143. { alignment }
  144. 4 +
  145. {$endif cpu64bitaddr}
  146. { reference count }
  147. sizeof(pint) +
  148. { length }
  149. sizeof(pint);
  150. unicodestring_header_size = ansistring_header_size;
  151. begin
  152. if not(target_info.system in systems_darwin) then
  153. result:=0
  154. else case typ of
  155. st_ansistring:
  156. result:=ansistring_header_size;
  157. st_unicodestring:
  158. result:=unicodestring_header_size;
  159. st_widestring:
  160. if winlikewidestring then
  161. result:=0
  162. else
  163. result:=unicodestring_header_size;
  164. else
  165. result:=0;
  166. end;
  167. end;
  168. end.