asmutils.pas 4.8 KB

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