1# Copyright (C) 2011 Apple Inc. All rights reserved.
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions
5# are met:
6# 1. Redistributions of source code must retain the above copyright
7#    notice, this list of conditions and the following disclaimer.
8# 2. Redistributions in binary form must reproduce the above copyright
9#    notice, this list of conditions and the following disclaimer in the
10#    documentation and/or other materials provided with the distribution.
11#
12# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
13# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
14# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
15# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
16# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
17# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
18# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
19# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
20# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
21# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
22# THE POSSIBILITY OF SUCH DAMAGE.
23
24require "config"
25require "arm"
26require "arm64"
27require "ast"
28require "x86"
29require "mips"
30require "sh4"
31require "cloop"
32
33BACKENDS =
34    [
35     "X86",
36     "X86_WIN",
37     "X86_64",
38     "X86_64_WIN",
39     "ARM",
40     "ARMv7",
41     "ARMv7_TRADITIONAL",
42     "ARM64",
43     "MIPS",
44     "SH4",
45     "C_LOOP"
46    ]
47
48# Keep the set of working backends separate from the set of backends that might be
49# supported. This is great because the BACKENDS list is almost like a reserved
50# words list, in that it causes settings resolution to treat those words specially.
51# Hence this lets us set aside the name of a backend we might want to support in
52# the future while not actually supporting the backend yet.
53WORKING_BACKENDS =
54    [
55     "X86",
56     "X86_WIN",
57     "X86_64",
58     "X86_64_WIN",
59     "ARM",
60     "ARMv7",
61     "ARMv7_TRADITIONAL",
62     "ARM64",
63     "MIPS",
64     "SH4",
65     "C_LOOP"
66    ]
67
68BACKEND_PATTERN = Regexp.new('\\A(' + BACKENDS.join(')|(') + ')\\Z')
69
70class Node
71    def lower(name)
72        begin
73            $activeBackend = name
74            send("lower" + name)
75        rescue => e
76            e.message << "At #{codeOriginString}"
77            raise e
78        end
79    end
80end
81
82# Overrides for lower() for those nodes that are backend-agnostic
83
84class Label
85    def lower(name)
86        $asm.putsLabel(self.name[1..-1], @global)
87    end
88end
89
90class LocalLabel
91    def lower(name)
92        $asm.putsLocalLabel "_offlineasm_#{self.name[1..-1]}"
93    end
94end
95
96class LabelReference
97    def asmLabel
98        if extern?
99            Assembler.externLabelReference(name[1..-1])
100        else
101            Assembler.labelReference(name[1..-1])
102        end
103    end
104
105    def cLabel
106        Assembler.cLabelReference(name[1..-1])
107    end
108end
109
110class LocalLabelReference
111    def asmLabel
112        Assembler.localLabelReference("_offlineasm_"+name[1..-1])
113    end
114
115    def cLabel
116        Assembler.cLocalLabelReference("_offlineasm_"+name[1..-1])
117    end
118end
119
120class Skip
121    def lower(name)
122    end
123end
124
125class Sequence
126    def lower(name)
127        $activeBackend = name
128        if respond_to? "getModifiedList#{name}"
129            newList = send("getModifiedList#{name}")
130            newList.each {
131                | node |
132                node.lower(name)
133            }
134        elsif respond_to? "lower#{name}"
135            send("lower#{name}")
136        else
137            @list.each {
138                | node |
139                node.lower(name)
140            }
141        end
142    end
143end
144
145