summaryrefslogtreecommitdiff
path: root/compiler/rasm.pas
blob: a9d67762754ccbc8f483c4154055185000ec030c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
{
    Copyright (c) 1998-2003 by Peter Vreman, Florian Klaempfl and Carl Eric Codere

    Basic stuff for assembler readers

    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 rasm;

{$i fpcdefs.inc}

  interface

    uses
      cclasses,
      symsym,
      rabase,
      aasmbase,
      aasmdata,
      cpubase,
      cgbase;

    type
       tasmreader = class(tbaseasmreader)
         firsttoken     : boolean;
         _asmsorted     : boolean;
         curlist        : TAsmList;
         c              : char;
         actasmpattern  : string;
         actopcode      : tasmop;
         actasmregister : tregister;
         actcondition   : tasmcond;
         iasmops        : TFPHashList;
         locallabels    : TFPHashObjectList;
         constructor create;override;
         destructor destroy;override;
         function createlocallabel(const s: string; var hl: tasmlabel; emit: boolean): boolean;
         procedure checklocallabels;
       protected
         { allow subscripting a local if it is a:
            1) pointer to a records/object, or a class instance pointer, passed in a register to a pure assembler routine
            2) record located on the stack (passed as parameter, or local variable)
         }
         procedure checklocalsubscript(sym: tabstractnormalvarsym); virtual;
       end;

  implementation

    uses
      verbose,
      procinfo,
      symconst,symdef,
      paramgr;

    type
      TLocalLabel = class(TFPHashObject)
        emitted: boolean;
        lab: tasmlabel;
        function Gettasmlabel: tasmlabel;
      end;


    function TLocalLabel.Gettasmlabel:tasmlabel;
      begin
        if not assigned(lab) then
         begin
           current_asmdata.getjumplabel(lab);
           { this label is forced to be used so it's always written }
           lab.increfs;
         end;
        result:=lab;
      end;


    constructor tasmreader.create;
      begin
        inherited create;
        firsttoken:=true;
        locallabels:=TFPHashObjectList.create;
      end;


    destructor tasmreader.destroy;
      begin
        locallabels.Free;
        iasmops.Free;
        inherited destroy;
      end;


    function tasmreader.createlocallabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
      var
        lab: TLocalLabel;
      begin
        result:=true;
        { Check if it already is defined }
        lab:=TLocalLabel(locallabels.Find(s));
        if not assigned(lab) then
          lab:=TLocalLabel.Create(locallabels,s);
        { set emitted flag and check for dup syms }
        if emit then
          begin
            if lab.Emitted then
              begin
                Message1(asmr_e_dup_local_sym,lab.Name);
                result:=false;
              end;
            lab.Emitted:=true;
          end;
        hl:=lab.Gettasmlabel;
      end;


    procedure tasmreader.checklocallabels;
      var
        i: longint;
        lab: TLocalLabel;
      begin
        for i:=0 to locallabels.Count-1 do
          begin
            lab:=TLocalLabel(locallabels[i]);
            if not lab.emitted then
              Message1(asmr_e_unknown_label_identifier,lab.name);
          end;
        locallabels.Clear;
      end;


    procedure tasmreader.checklocalsubscript(sym: tabstractnormalvarsym);
      var
        isimplicitpointer: boolean;
      begin
        isimplicitpointer:=
          (sym.typ=paravarsym) and
          (is_implicit_pointer_object_type(sym.vardef) or
           paramanager.push_addr_param(sym.varspez,sym.vardef,current_procinfo.procdef.proccalloption));

        { sym.initiallloc/localloc is not yet initialised here }

        { pointer parameter to aggregate passed in register to pure assembler routine }
        if (po_assembler in current_procinfo.procdef.procoptions) and
           (sym.typ=paravarsym) and
           (tparavarsym(sym).paraloc[calleeside].location^.loc=LOC_REGISTER) and
           { ... however this is only possible if the pointer takes only one register location }
           not assigned(tparavarsym(sym).paraloc[calleeside].location^.Next) and
           isimplicitpointer then
          exit;

        { aggregate parameter passed on the stack to a pure assembler routine }
        if (po_assembler in current_procinfo.procdef.procoptions) and
           (sym.typ=paravarsym) and
           { sym.localloc is not yet initialised here for pure assembler routines }
           (tparavarsym(sym).paraloc[calleeside].location^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
           not isimplicitpointer then
          exit;

        { aggregate parameter located on the stack for a non-assembler routine
          (locals accessed from assembler code are never kept in registers) }
        if not(po_assembler in current_procinfo.procdef.procoptions) and
           (sym.typ=paravarsym) and
           not isimplicitpointer then
          exit;

        { local aggregate located on the stack (locals accessed from assembler
          code are never kept in registers) }
        if ((sym.typ=localvarsym) or
           { even if a parameter is passed by reference, it will be copied to
             a local if it's a value parameter to a non assembler routines }
            (not(po_assembler in current_procinfo.procdef.procoptions) and
             (sym.typ=paravarsym) and
             (sym.varspez=vs_value))) and
           not is_implicit_pointer_object_type(sym.vardef) then
          exit;

        Message(asmr_e_cannot_access_field_directly_for_parameters);
      end;

end.