fpc/compiler/asmutils.pas
Jonas Maebe 8afffdb176 * moved get_string_symofs() to a virtual class method of
ttai_typedconstbuilder so that it can be overridden by other platforms
  * override get_string_symofs() for LLVM and always return 0, as you can't
    have labels in the middle of a declaration on that platform

git-svn-id: branches/hlcgllvm@28140 -
2014-07-03 22:28:21 +00:00

158 lines
5.5 KiB
ObjectPascal

{
Copyright (c) 1998-2006 by Florian Klaempfl
This unit contains utility functions for assembler output
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
****************************************************************************
}
unit asmutils;
interface
{$i fpcdefs.inc}
uses
globtype,
aasmbase,
aasmdata,
symconst;
type
tasmlabofs = record
lab: tasmlabel;
ofs: pint;
end;
function emit_ansistring_const(list:TAsmList;data:PChar;len:LongInt;encoding:tstringencoding;NewSection:Boolean=True):tasmlabofs;
function emit_unicodestring_const(list:TAsmList;data:Pointer;encoding:tstringencoding;Winlike:Boolean):tasmlabofs;
implementation
uses
globals,
systems,
verbose,
aasmtai,aasmcnst,
widestr,
symdef;
function emit_ansistring_const(list:TAsmList;data:PChar;len:LongInt;encoding:tstringencoding;NewSection:Boolean): tasmlabofs;
var
s: PChar;
begin
current_asmdata.getdatalabel(result.lab);
result.ofs:=0;
if NewSection then
begin
maybe_new_object_file(list);
new_section(list,sec_rodata_norel,result.lab.name,const_align(sizeof(pint)));
end;
{ put label before header on Darwin, because there the linker considers
a global symbol to be the start of a new subsection }
if target_info.system in systems_darwin then
list.concat(tai_label.create(result.lab));
list.concat(tai_const.create_16bit(encoding));
inc(result.ofs,2);
list.concat(tai_const.create_16bit(1));
inc(result.ofs,2);
{$ifdef cpu64bitaddr}
{ dummy for alignment }
list.concat(tai_const.create_32bit(0));
inc(result.ofs,4);
{$endif cpu64bitaddr}
list.concat(tai_const.create_pint(-1));
inc(result.ofs,sizeof(pint));
list.concat(tai_const.create_pint(len));
inc(result.ofs,sizeof(pint));
if not(target_info.system in systems_darwin) then
begin
{ results in slightly more efficient code }
list.concat(tai_label.create(result.lab));
result.ofs:=0;
end;
{ sanity check }
if result.ofs<>ctai_typedconstbuilder.get_string_symofs(st_ansistring,false) then
internalerror(2012051701);
getmem(s,len+1);
move(data^,s^,len);
s[len]:=#0;
list.concat(tai_string.create_pchar(s,len+1)); { terminating zero included }
end;
function emit_unicodestring_const(list:TAsmList;data:Pointer;encoding:tstringencoding;Winlike:Boolean):tasmlabofs;
var
i, strlength: SizeInt;
begin
current_asmdata.getdatalabel(result.lab);
result.ofs:=0;
maybe_new_object_file(list);
new_section(list,sec_rodata_norel,result.lab.name,const_align(sizeof(pint)));
strlength := getlengthwidestring(pcompilerwidestring(data));
if Winlike then
begin
list.concat(Tai_const.Create_32bit(strlength*cwidechartype.size));
{ don't increase result.ofs, this is how Windows widestrings are
defined by the OS: a pointer 4 bytes past the length of the
string }
list.concat(Tai_label.Create(result.lab));
end
else
begin
{ put label before header on Darwin, because there the linker considers
a global symbol to be the start of a new subsection }
if target_info.system in systems_darwin then
list.concat(Tai_label.Create(result.lab));
list.concat(tai_const.create_16bit(encoding));
inc(result.ofs,2);
list.concat(tai_const.create_16bit(2));
inc(result.ofs,2);
{$ifdef cpu64bitaddr}
{ dummy for alignment }
list.concat(Tai_const.Create_32bit(0));
inc(result.ofs,4);
{$endif cpu64bitaddr}
list.concat(Tai_const.Create_pint(-1));
inc(result.ofs,sizeof(pint));
list.concat(Tai_const.Create_pint(strlength));
inc(result.ofs,sizeof(pint));
if not(target_info.system in systems_darwin) then
begin
{ results in slightly more efficient code }
list.concat(tai_label.create(result.lab));
result.ofs:=0;
end;
{ sanity check }
if result.ofs<>ctai_typedconstbuilder.get_string_symofs(st_unicodestring,false) then
internalerror(2012051702);
end;
if cwidechartype.size = 2 then
begin
for i:=0 to strlength-1 do
list.concat(Tai_const.Create_16bit(pcompilerwidestring(data)^.data[i]));
{ ending #0 }
list.concat(Tai_const.Create_16bit(0));
end
else
InternalError(200904271); { codegeneration for other sizes must be written }
end;
end.