blob: 47ab5c9f6bb70888d63166b0f88c1650b7f7d34e [file] [log] [blame]
David Li067eefe2011-03-22 10:06:00 -07001#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4#
5# Copyright 2011, The Android Open Source Project
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
19
20import os
21import sys
22
23def RemoveAnnotation(line):
24 if line.find(":") >= 0:
25 annotation = line[line.find(":"): line.find(" ", line.find(":"))]
26 return line.replace(annotation, "*")
27 else:
28 return line
29
30if __name__ == "__main__":
31 externs = []
32 lines = open("../../../frameworks/base/opengl/libs/GLES2_dbg/gl2_api_annotated.in").readlines()
33 output = open("src/com/android/glesv2debugger/MessageParser.java", "w")
34
35 i = 0
36 output.write("""\
37/*
38 ** Copyright 2011, The Android Open Source Project
39 **
40 ** Licensed under the Apache License, Version 2.0 (the "License");
41 ** you may not use this file except in compliance with the License.
42 ** You may obtain a copy of the License at
43 **
44 ** http://www.apache.org/licenses/LICENSE-2.0
45 **
46 ** Unless required by applicable law or agreed to in writing, software
47 ** distributed under the License is distributed on an "AS IS" BASIS,
48 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
49 ** See the License for the specific language governing permissions and
50 ** limitations under the License.
51 */
52
53// auto generated by generate_MessageParser_java.py,
54// which also prints skeleton code for MessageParserEx.java
55
56package com.android.glesv2debugger;
57
58import com.android.glesv2debugger.DebuggerMessage.Message;
59import com.android.glesv2debugger.DebuggerMessage.Message.Function;
60import com.google.protobuf.ByteString;
61
62import java.nio.ByteBuffer;
63
64public abstract class MessageParser {
65
66 String args;
67
68 String[] GetList()
69 {
70 assert args.charAt(0) == '[';
71 String arg = args;
72 args = args.substring(args.indexOf(']') + 1);
73 int comma = args.indexOf(',');
74 if (comma >= 0)
75 args = args.substring(comma + 1).trim();
76 else
77 args = null;
78 arg = arg.substring(1, arg.indexOf(']')).trim();
79 return arg.split(",");
80 }
81
82 ByteString ParseFloats(int count) {
83 ByteBuffer buffer = ByteBuffer.allocate(count * 4);
84 String [] arg = GetList();
85 for (int i = 0; i < count; i++)
86 buffer.putFloat(Float.parseFloat(arg[i].trim()));
87 return ByteString.copyFrom(buffer);
88 }
89
90 ByteString ParseInts(int count) {
91 ByteBuffer buffer = ByteBuffer.allocate(count * 4);
92 String [] arg = GetList();
93 for (int i = 0; i < count; i++)
94 buffer.putInt(Integer.parseInt(arg[i].trim()));
95 return ByteString.copyFrom(buffer);
96 }
97
98 ByteString ParseUInts(int count) {
99 ByteBuffer buffer = ByteBuffer.allocate(count * 4);
100 String [] arg = GetList();
101 for (int i = 0; i < count; i++)
102 buffer.putInt((int)(Long.parseLong(arg[i].trim()) & 0xffffffff));
103 return ByteString.copyFrom(buffer);
104 }
105
106 ByteString ParseMatrix(int columns, int count) {
107 return ParseFloats(columns * count);
108 }
109
110 ByteString ParseString() {
111 // TODO: escape sequence and proper string literal
112 String arg = args.substring(args.indexOf('"') + 1, args.lastIndexOf('"'));
113 args = args.substring(args.lastIndexOf('"'));
114 int comma = args.indexOf(',');
115 if (comma >= 0)
116 args = args.substring(comma + 1).trim();
117 else
118 args = null;
119 return ByteString.copyFromUtf8(arg);
120 }
121
122 String GetArgument()
123 {
124 int comma = args.indexOf(",");
125 String arg = null;
126 if (comma >= 0)
127 {
128 arg = args.substring(0, comma).trim();
129 args = args.substring(comma + 1).trim();
130 }
131 else
132 {
133 arg = args;
134 args = null;
135 }
136 if (arg.indexOf("=") >= 0)
137 arg = arg.substring(arg.indexOf("=") + 1);
138 return arg;
139 }
140
141 int ParseArgument()
142 {
143 String arg = GetArgument();
144 if (arg.startsWith("GL_"))
145 return GLEnum.valueOf(arg).value;
146 else if (arg.toLowerCase().startsWith("0x"))
147 return Integer.parseInt(arg.substring(2), 16);
148 else
149 return Integer.parseInt(arg);
150 }
151
152 int ParseFloat()
153 {
154 String arg = GetArgument();
155 return Float.floatToRawIntBits(Float.parseFloat(arg));
156 }
157
158 public void Parse(final Message.Builder builder, String string) {
159 int lparen = string.indexOf("("), rparen = string.lastIndexOf(")");
160 String s = string.substring(0, lparen).trim();
161 args = string.substring(lparen + 1, rparen);
162 String[] t = s.split(" ");
163 Function function = Function.valueOf(t[t.length - 1]);
164 builder.setFunction(function);
165 switch (function) {
166""")
167
168 abstractParsers = ""
169
170 for line in lines:
171 if line.find("API_ENTRY(") >= 0: # a function prototype
172 returnType = line[0: line.find(" API_ENTRY(")].replace("const ", "")
173 functionName = line[line.find("(") + 1: line.find(")")] #extract GL function name
174 parameterList = line[line.find(")(") + 2: line.find(") {")]
175
176 parameters = parameterList.split(',')
177 paramIndex = 0
178
179 #if returnType != "void":
180 #else:
181
182 if parameterList == "void":
183 parameters = []
184 inout = ""
185
186 paramNames = []
187 abstract = False
188 argumentSetters = ""
189 output.write("\
190 case %s:\n" % (functionName))
191
192 for parameter in parameters:
193 parameter = parameter.replace("const","")
194 parameter = parameter.strip()
195 paramType = parameter.split(' ')[0]
196 paramName = parameter.split(' ')[1]
197 annotation = ""
198
199 argumentParser = ""
200
201 if parameter.find(":") >= 0:
202 dataSetter = ""
203 assert inout == "" # only one parameter should be annotated
204 inout = paramType.split(":")[2]
205 annotation = paramType.split(":")[1]
206 paramType = paramType.split(":")[0]
207 count = 1
208 countArg = ""
209 if annotation.find("*") >= 0: # [1,n] * param
210 count = int(annotation.split("*")[0])
211 countArg = annotation.split("*")[1]
212 assert countArg in paramNames
213 elif annotation in paramNames:
214 count = 1
215 countArg = annotation
216 elif annotation == "GLstring":
217 annotation = annotation
218 else:
219 count = int(annotation)
220
221 if paramType == "GLfloat":
222 argumentParser = "ParseFloats"
223 elif paramType == "GLint":
224 argumentParser = "ParseInts"
225 elif paramType == "GLuint":
226 argumentParser = "ParseUInts"
227 elif annotation == "GLstring":
228 assert paramType == 'GLchar'
229 elif paramType.find("void") >= 0:
230 assert 1
231 else:
232 assert 0
233
234 if functionName.find('Matrix') >= 0:
235 columns = int(functionName[functionName.find("fv") - 1: functionName.find("fv")])
236 assert columns * columns == count
237 assert countArg != ""
238 assert paramType == "GLfloat"
239 dataSetter = "builder.setData(ParseMatrix(%d, %d * builder.getArg%d()));" % (
240 columns, count, paramNames.index(countArg))
241 elif annotation == "GLstring":
242 dataSetter = "builder.setData(ParseString());"
243 elif paramType.find("void") >= 0:
244 dataSetter = "// TODO"
245 abstract = True
246 elif countArg == "":
247 dataSetter = "builder.setData(%s(%d));" % (argumentParser, count)
248 else:
249 dataSetter = "builder.setData(%s(%d * builder.getArg%d()));" % (
250 argumentParser, count, paramNames.index(countArg))
251 argumentSetters += "\
252 %s // %s %s\n" % (dataSetter, paramType, paramName)
253 else:
254 if paramType == "GLfloat" or paramType == "GLclampf":
255 argumentSetters += "\
256 builder.setArg%d(ParseFloat()); // %s %s\n" % (
257 paramIndex, paramType, paramName)
258 elif paramType.find("*") >= 0:
259 argumentSetters += "\
260 // TODO: %s %s\n" % (paramType, paramName)
261 abstract = True
262 else:
263 argumentSetters += "\
264 builder.setArg%d(ParseArgument()); // %s %s\n" % (
265 paramIndex, paramType, paramName)
266 paramNames.append(paramName)
267 paramIndex += 1
268
269 if not abstract:
270 output.write("%s" % argumentSetters)
271 else:
272 output.write("\
273 Parse_%s(builder);\n" % functionName)
274 abstractParsers += "\
275 abstract void Parse_%s(Message.Builder builder);\n" % functionName
276 print """\
277 @Override
278 void Parse_%s(Message.Builder builder) {
279%s }
280""" % (functionName, argumentSetters) # print skeleton code for MessageParserE
281
282 output.write("\
283 break;\n")
284 output.write("""\
285 default:
286 assert false;
287 }
288 }
289""")
290 output.write(abstractParsers)
291 output.write("\
292}""")