asmutils.pas 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. implementation
  33. uses
  34. globals,
  35. systems,
  36. verbose,
  37. aasmtai,aasmcnst,
  38. widestr,
  39. symdef;
  40. function emit_ansistring_const(list:TAsmList;data:PChar;len:LongInt;encoding:tstringencoding;NewSection:Boolean): tasmlabofs;
  41. var
  42. s: PChar;
  43. begin
  44. current_asmdata.getdatalabel(result.lab);
  45. result.ofs:=0;
  46. if NewSection then
  47. begin
  48. maybe_new_object_file(list);
  49. new_section(list,sec_rodata_norel,result.lab.name,const_align(sizeof(pint)));
  50. end;
  51. { put label before header on Darwin, because there the linker considers
  52. a global symbol to be the start of a new subsection }
  53. if target_info.system in systems_darwin then
  54. list.concat(tai_label.create(result.lab));
  55. list.concat(tai_const.create_16bit(encoding));
  56. inc(result.ofs,2);
  57. list.concat(tai_const.create_16bit(1));
  58. inc(result.ofs,2);
  59. {$ifdef cpu64bitaddr}
  60. { dummy for alignment }
  61. list.concat(tai_const.create_32bit(0));
  62. inc(result.ofs,4);
  63. {$endif cpu64bitaddr}
  64. list.concat(tai_const.create_pint(-1));
  65. inc(result.ofs,sizeof(pint));
  66. list.concat(tai_const.create_pint(len));
  67. inc(result.ofs,sizeof(pint));
  68. if not(target_info.system in systems_darwin) then
  69. begin
  70. { results in slightly more efficient code }
  71. list.concat(tai_label.create(result.lab));
  72. result.ofs:=0;
  73. end;
  74. { sanity check }
  75. if result.ofs<>ctai_typedconstbuilder.get_string_symofs(st_ansistring,false) then
  76. internalerror(2012051701);
  77. getmem(s,len+1);
  78. move(data^,s^,len);
  79. s[len]:=#0;
  80. list.concat(tai_string.create_pchar(s,len+1)); { terminating zero included }
  81. end;
  82. function emit_unicodestring_const(list:TAsmList;data:Pointer;encoding:tstringencoding;Winlike:Boolean):tasmlabofs;
  83. var
  84. i, strlength: SizeInt;
  85. begin
  86. current_asmdata.getdatalabel(result.lab);
  87. result.ofs:=0;
  88. maybe_new_object_file(list);
  89. new_section(list,sec_rodata_norel,result.lab.name,const_align(sizeof(pint)));
  90. strlength := getlengthwidestring(pcompilerwidestring(data));
  91. if Winlike then
  92. begin
  93. list.concat(Tai_const.Create_32bit(strlength*cwidechartype.size));
  94. { don't increase result.ofs, this is how Windows widestrings are
  95. defined by the OS: a pointer 4 bytes past the length of the
  96. string }
  97. list.concat(Tai_label.Create(result.lab));
  98. end
  99. else
  100. begin
  101. { put label before header on Darwin, because there the linker considers
  102. a global symbol to be the start of a new subsection }
  103. if target_info.system in systems_darwin then
  104. list.concat(Tai_label.Create(result.lab));
  105. list.concat(tai_const.create_16bit(encoding));
  106. inc(result.ofs,2);
  107. list.concat(tai_const.create_16bit(2));
  108. inc(result.ofs,2);
  109. {$ifdef cpu64bitaddr}
  110. { dummy for alignment }
  111. list.concat(Tai_const.Create_32bit(0));
  112. inc(result.ofs,4);
  113. {$endif cpu64bitaddr}
  114. list.concat(Tai_const.Create_pint(-1));
  115. inc(result.ofs,sizeof(pint));
  116. list.concat(Tai_const.Create_pint(strlength));
  117. inc(result.ofs,sizeof(pint));
  118. if not(target_info.system in systems_darwin) then
  119. begin
  120. { results in slightly more efficient code }
  121. list.concat(tai_label.create(result.lab));
  122. result.ofs:=0;
  123. end;
  124. { sanity check }
  125. if result.ofs<>ctai_typedconstbuilder.get_string_symofs(st_unicodestring,false) then
  126. internalerror(2012051702);
  127. end;
  128. if cwidechartype.size = 2 then
  129. begin
  130. for i:=0 to strlength-1 do
  131. list.concat(Tai_const.Create_16bit(pcompilerwidestring(data)^.data[i]));
  132. { ending #0 }
  133. list.concat(Tai_const.Create_16bit(0));
  134. end
  135. else
  136. InternalError(200904271); { codegeneration for other sizes must be written }
  137. end;
  138. end.