1 /*
2  *  A utility for building various tables and specializations for the
3  *  KJS Frostbyte bytecode
4  *
5  *  Copyright (C) 2007, 2008 Maks Orlovich (maksim@kde.org)
6  *
7  *  This library is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU Library General Public
9  *  License as published by the Free Software Foundation; either
10  *  version 2 of the License, or (at your option) any later version.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Library General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Library General Public License
18  *  along with this library; see the file COPYING.LIB.  If not, write to
19  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  *  Boston, MA 02110-1301, USA.
21  *
22  */
23 
24 #ifndef TYPES_H
25 #define TYPES_H
26 
27 #include <iostream>
28 #include <vector>
29 #include <map>
30 
31 #include "codeprinter.h"
32 
33 using std::ostream;
34 using std::vector;
35 using std::map;
36 
37 enum TypeFlags {
38     Type_HaveImm = 1,
39     Type_HaveReg = 2,
40     Type_Align8  = 4
41 };
42 
43 enum ConvFlags {
44     Conv_NoFlags,
45     Conv_HaveReg  = 2,
46     Conv_Checked  = 4,
47     Conv_MayThrow = 8
48 };
49 
50 struct Type {
51     string name;
52     string nativeName;
53 
54     unsigned flags;
55 
56     // may not be the same as Type_Align8 in the feature..
alignTo8Type57     bool alignTo8() const
58     {
59         return (flags & Type_Align8) == Type_Align8;
60     }
61 
hasRegType62     bool hasReg() const
63     {
64         return (flags & Type_HaveReg) == Type_HaveReg;
65     }
66 
hasImmType67     bool hasImm() const
68     {
69         return (flags & Type_HaveImm) == Type_HaveImm;
70     }
71 
72     // field in store cells to access for this type
fieldType73     string field() const
74     {
75         return ((flags & Type_Align8) ? "wide" : "narrow") +
76                std::string(".") + name + "Val";
77     }
78 
79     bool operator==(const Type &other) const
80     {
81         return name == other.name;
82     }
83 };
84 
85 struct ConversionInfo {
86     string name;
87     string impl;
88     int  cost;   // for w/in tile for immediate, for external for reg
89     unsigned flags;
90     Type from;
91     Type to;
92     int codeLine;
93 
ConversionInfoConversionInfo94     ConversionInfo(): cost(0), flags(Conv_NoFlags)
95     {} //Can be called for trivial conversion
96 };
97 
98 class TableBuilder;
99 
100 // This class is responsible for managing types & conversions, and generating
101 // conversion-selection routines.
102 class TypeTable
103 {
104 public:
105     TypeTable(TableBuilder *instrBuilder, CodePrinter &out);
106 
107     void generateCode();
108 
109     void handleType(const string &type, const string &nativeName, unsigned flags);
110     void handleConversion(const string &runtimeRoutine, int codeLine,
111                           unsigned flags, const string &from, const string &to,
112                           int tileCost, int registerCost);
113 
114     // issues error if there is a problem..
115     vector<Type> resolveSignature(const StringList &in);
116     Type         resolveType(const string &type);
117 
118     ConversionInfo immConv(const Type &from, const Type &to);
119 private:
120     TableBuilder *instrBuilder;
121     CodePrinter  &out;
122     void printConversionInfo(Array &array, map<string, map<string, ConversionInfo> > &table, bool reg);
123 
124     void printConversionRoutine(const ConversionInfo &conversion);
125 
126     map<string, Type> types;
127     StringList        typeNames;
128 
129     StringList conversionNames;
130     vector<ConversionInfo> imConversionList;
131     vector<ConversionInfo> rgConversionList;
132 
133     map<string, map<string, ConversionInfo> > imConversions;
134     map<string, map<string, ConversionInfo> > rgConversions;
135 };
136 
137 #endif
138