asmutils.pas 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. function emit_ansistring_const(list:TAsmList;data:PChar;len:LongInt;encoding:tstringencoding;NewSection:Boolean=True):TAsmLabel;
  25. function emit_unicodestring_const(list:TAsmList;data:Pointer;encoding:tstringencoding;Winlike:Boolean):TAsmLabel;
  26. implementation
  27. uses
  28. globals,
  29. systems,
  30. verbose,
  31. aasmtai,
  32. widestr,
  33. symdef;
  34. function emit_ansistring_const(list:TAsmList;data:PChar;len:LongInt;encoding:tstringencoding;NewSection:Boolean): TAsmLabel;
  35. var
  36. referencelab: TAsmLabel;
  37. s: PChar;
  38. begin
  39. current_asmdata.getdatalabel(result);
  40. if NewSection then
  41. new_section(list,sec_rodata,result.name,const_align(sizeof(pint)));
  42. referencelab := nil;
  43. if target_info.system in systems_darwin then
  44. begin
  45. current_asmdata.getdatalabel(referencelab);
  46. list.concat(tai_label.create(referencelab));
  47. end;
  48. if (encoding=0) then
  49. encoding:=CP_NONE;
  50. if (encoding=CP_NONE) and (m_systemcodepage in current_settings.modeswitches) then
  51. encoding:=current_settings.sourcecodepage;
  52. list.concat(tai_const.create_16bit(encoding));
  53. list.concat(tai_const.create_16bit(1));
  54. {$ifdef cpu64bitaddr}
  55. { dummy for alignment }
  56. list.concat(tai_const.create_32bit(0));
  57. {$endif cpu64bitaddr}
  58. list.concat(tai_const.create_pint(-1));
  59. list.concat(tai_const.create_pint(len));
  60. { make sure the string doesn't get dead stripped if the header is referenced }
  61. if target_info.system in systems_darwin then
  62. list.concat(tai_directive.create(asd_reference,result.name));
  63. list.concat(tai_label.create(result));
  64. { and vice versa }
  65. if target_info.system in systems_darwin then
  66. list.concat(tai_directive.create(asd_reference,referencelab.name));
  67. getmem(s,len+1);
  68. move(data^,s^,len);
  69. s[len]:=#0;
  70. list.concat(tai_string.create_pchar(s,len+1)); { terminating zero included }
  71. end;
  72. function emit_unicodestring_const(list:TAsmList;data:Pointer;encoding:tstringencoding;Winlike:Boolean):TAsmLabel;
  73. var
  74. referencelab: TAsmLabel;
  75. i, strlength: SizeInt;
  76. begin
  77. current_asmdata.getdatalabel(result);
  78. new_section(list,sec_rodata,result.name,const_align(sizeof(pint)));
  79. referencelab := nil;
  80. if target_info.system in systems_darwin then
  81. begin
  82. current_asmdata.getdatalabel(referencelab);
  83. list.concat(tai_label.create(referencelab));
  84. end;
  85. strlength := getlengthwidestring(pcompilerwidestring(data));
  86. if Winlike then
  87. list.concat(Tai_const.Create_32bit(strlength*cwidechartype.size))
  88. else
  89. begin
  90. list.concat(tai_const.create_16bit(encoding));
  91. list.concat(tai_const.create_16bit(2));
  92. {$ifdef cpu64bitaddr}
  93. { dummy for alignment }
  94. list.concat(Tai_const.Create_32bit(0));
  95. {$endif cpu64bitaddr}
  96. list.concat(Tai_const.Create_pint(-1));
  97. list.concat(Tai_const.Create_pint(strlength));
  98. end;
  99. { make sure the string doesn't get dead stripped if the header is referenced }
  100. if (target_info.system in systems_darwin) then
  101. list.concat(tai_directive.create(asd_reference,result.name));
  102. list.concat(Tai_label.Create(result));
  103. { ... and vice versa }
  104. if (target_info.system in systems_darwin) then
  105. list.concat(tai_directive.create(asd_reference,referencelab.name));
  106. if cwidechartype.size = 2 then
  107. begin
  108. for i:=0 to strlength-1 do
  109. list.concat(Tai_const.Create_16bit(pcompilerwidestring(data)^.data[i]));
  110. { ending #0 }
  111. list.concat(Tai_const.Create_16bit(0));
  112. end
  113. else
  114. InternalError(200904271); { codegeneration for other sizes must be written }
  115. end;
  116. end.