blob: 7c340087a4c1ed93744720bd207ad670dbe7b9cd [file] [log] [blame]
Shih-wei Liao19cc6972011-01-20 04:34:38 -08001<?xml version="1.0" encoding="utf-8" ?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6<meta name="generator" content="Docutils 0.6: http://docutils.sourceforge.net/" />
7<title>llvm-rs-cc: Compiler for ScriptC language</title>
8<style type="text/css">
9
10/*
11:Author: David Goodger (goodger@python.org)
12:Id: $Id: html4css1.css 5951 2009-05-18 18:03:10Z milde $
13:Copyright: This stylesheet has been placed in the public domain.
14
15Default cascading style sheet for the HTML output of Docutils.
16
17See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
18customize this style sheet.
19*/
20
21/* used to remove borders from tables and images */
22.borderless, table.borderless td, table.borderless th {
23 border: 0 }
24
25table.borderless td, table.borderless th {
26 /* Override padding for "table.docutils td" with "! important".
27 The right padding separates the table cells. */
28 padding: 0 0.5em 0 0 ! important }
29
30.first {
31 /* Override more specific margin styles with "! important". */
32 margin-top: 0 ! important }
33
34.last, .with-subtitle {
35 margin-bottom: 0 ! important }
36
37.hidden {
38 display: none }
39
40a.toc-backref {
41 text-decoration: none ;
42 color: black }
43
44blockquote.epigraph {
45 margin: 2em 5em ; }
46
47dl.docutils dd {
48 margin-bottom: 0.5em }
49
50/* Uncomment (and remove this text!) to get bold-faced definition list terms
51dl.docutils dt {
52 font-weight: bold }
53*/
54
55div.abstract {
56 margin: 2em 5em }
57
58div.abstract p.topic-title {
59 font-weight: bold ;
60 text-align: center }
61
62div.admonition, div.attention, div.caution, div.danger, div.error,
63div.hint, div.important, div.note, div.tip, div.warning {
64 margin: 2em ;
65 border: medium outset ;
66 padding: 1em }
67
68div.admonition p.admonition-title, div.hint p.admonition-title,
69div.important p.admonition-title, div.note p.admonition-title,
70div.tip p.admonition-title {
71 font-weight: bold ;
72 font-family: sans-serif }
73
74div.attention p.admonition-title, div.caution p.admonition-title,
75div.danger p.admonition-title, div.error p.admonition-title,
76div.warning p.admonition-title {
77 color: red ;
78 font-weight: bold ;
79 font-family: sans-serif }
80
81/* Uncomment (and remove this text!) to get reduced vertical space in
82 compound paragraphs.
83div.compound .compound-first, div.compound .compound-middle {
84 margin-bottom: 0.5em }
85
86div.compound .compound-last, div.compound .compound-middle {
87 margin-top: 0.5em }
88*/
89
90div.dedication {
91 margin: 2em 5em ;
92 text-align: center ;
93 font-style: italic }
94
95div.dedication p.topic-title {
96 font-weight: bold ;
97 font-style: normal }
98
99div.figure {
100 margin-left: 2em ;
101 margin-right: 2em }
102
103div.footer, div.header {
104 clear: both;
105 font-size: smaller }
106
107div.line-block {
108 display: block ;
109 margin-top: 1em ;
110 margin-bottom: 1em }
111
112div.line-block div.line-block {
113 margin-top: 0 ;
114 margin-bottom: 0 ;
115 margin-left: 1.5em }
116
117div.sidebar {
118 margin: 0 0 0.5em 1em ;
119 border: medium outset ;
120 padding: 1em ;
121 background-color: #ffffee ;
122 width: 40% ;
123 float: right ;
124 clear: right }
125
126div.sidebar p.rubric {
127 font-family: sans-serif ;
128 font-size: medium }
129
130div.system-messages {
131 margin: 5em }
132
133div.system-messages h1 {
134 color: red }
135
136div.system-message {
137 border: medium outset ;
138 padding: 1em }
139
140div.system-message p.system-message-title {
141 color: red ;
142 font-weight: bold }
143
144div.topic {
145 margin: 2em }
146
147h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
148h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
149 margin-top: 0.4em }
150
151h1.title {
152 text-align: center }
153
154h2.subtitle {
155 text-align: center }
156
157hr.docutils {
158 width: 75% }
159
160img.align-left, .figure.align-left{
161 clear: left ;
162 float: left ;
163 margin-right: 1em }
164
165img.align-right, .figure.align-right {
166 clear: right ;
167 float: right ;
168 margin-left: 1em }
169
170.align-left {
171 text-align: left }
172
173.align-center {
174 clear: both ;
175 text-align: center }
176
177.align-right {
178 text-align: right }
179
180/* reset inner alignment in figures */
181div.align-right {
182 text-align: left }
183
184/* div.align-center * { */
185/* text-align: left } */
186
187ol.simple, ul.simple {
188 margin-bottom: 1em }
189
190ol.arabic {
191 list-style: decimal }
192
193ol.loweralpha {
194 list-style: lower-alpha }
195
196ol.upperalpha {
197 list-style: upper-alpha }
198
199ol.lowerroman {
200 list-style: lower-roman }
201
202ol.upperroman {
203 list-style: upper-roman }
204
205p.attribution {
206 text-align: right ;
207 margin-left: 50% }
208
209p.caption {
210 font-style: italic }
211
212p.credits {
213 font-style: italic ;
214 font-size: smaller }
215
216p.label {
217 white-space: nowrap }
218
219p.rubric {
220 font-weight: bold ;
221 font-size: larger ;
222 color: maroon ;
223 text-align: center }
224
225p.sidebar-title {
226 font-family: sans-serif ;
227 font-weight: bold ;
228 font-size: larger }
229
230p.sidebar-subtitle {
231 font-family: sans-serif ;
232 font-weight: bold }
233
234p.topic-title {
235 font-weight: bold }
236
237pre.address {
238 margin-bottom: 0 ;
239 margin-top: 0 ;
240 font: inherit }
241
242pre.literal-block, pre.doctest-block {
243 margin-left: 2em ;
244 margin-right: 2em }
245
246span.classifier {
247 font-family: sans-serif ;
248 font-style: oblique }
249
250span.classifier-delimiter {
251 font-family: sans-serif ;
252 font-weight: bold }
253
254span.interpreted {
255 font-family: sans-serif }
256
257span.option {
258 white-space: nowrap }
259
260span.pre {
261 white-space: pre }
262
263span.problematic {
264 color: red }
265
266span.section-subtitle {
267 /* font-size relative to parent (h1..h6 element) */
268 font-size: 80% }
269
270table.citation {
271 border-left: solid 1px gray;
272 margin-left: 1px }
273
274table.docinfo {
275 margin: 2em 4em }
276
277table.docutils {
278 margin-top: 0.5em ;
279 margin-bottom: 0.5em }
280
281table.footnote {
282 border-left: solid 1px black;
283 margin-left: 1px }
284
285table.docutils td, table.docutils th,
286table.docinfo td, table.docinfo th {
287 padding-left: 0.5em ;
288 padding-right: 0.5em ;
289 vertical-align: top }
290
291table.docutils th.field-name, table.docinfo th.docinfo-name {
292 font-weight: bold ;
293 text-align: left ;
294 white-space: nowrap ;
295 padding-left: 0 }
296
297h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
298h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
299 font-size: 100% }
300
301ul.auto-toc {
302 list-style-type: none }
303
304</style>
305</head>
306<body>
307<div class="document" id="llvm-rs-cc-compiler-for-scriptc-language">
308<h1 class="title">llvm-rs-cc: Compiler for ScriptC language</h1>
309
310<div class="section" id="introduction">
311<h1>Introduction</h1>
312<p>llvm-rs-cc compiles a program in the ScriptC language to generate the
313following files.</p>
314<ul class="simple">
315<li>Bitcode file. Note that the bitcode here denotes the LLVM (Low-Level
316Virtual Machine) bitcode representation, which will be consumed on
317an Android device by libbcc (in
318platform/frameworks/compile/libbcc.git) to generate device-specific
319executables.</li>
320<li>Reflected APIs for Java. As a result, Android's Java developers can
321invoke those APIs from their code.</li>
322</ul>
323<p>Note that although ScriptC is C99-like, we enhance it with several
324distinct, effective features for Android programming. We will use
325example usage below to illustrate these features.</p>
326<p>llvm-rs-cc is being run on a host and is highly-optimizing. As a
327result, libbcc on the device can be lightweight and focus on
328machine-dependent code generation given some bitcode.</p>
329<p>llvm-rs-cc is a driver on top of libslang. The archictecture of
330libslang and libbcc is depicted in the following figure:</p>
331<pre class="literal-block">
332libslang libbcc
333 | \ |
334 | \ |
335 clang llvm
336</pre>
337</div>
338<div class="section" id="usage">
339<h1>Usage</h1>
340<ul>
341<li><p class="first"><em>-o $(PRIVATE_RS_OUTPUT_DIR)/res/raw</em></p>
342<p>This option specifies the directory for outputting a .bc file.</p>
343</li>
344<li><p class="first"><em>-p $(PRIVATE_RS_OUTPUT_DIR)/src</em></p>
345<p>The option <em>-p</em> denotes the directory for outputting the reflected Java files.</p>
346</li>
347<li><p class="first"><em>-d $(PRIVATE_RS_OUTPUT_DIR)</em></p>
348<p>This option <em>-d</em> sets the directory for writing dependences.</p>
349</li>
350<li><p class="first"><em>-MD</em></p>
351<p>Note that <em>-MD</em> will tell llvm-rs-cc to output dependences.</p>
352</li>
Shih-wei Liao29c36732011-01-20 05:48:20 -0800353<li><p class="first"><em>-a</em></p>
354<p>Specifies additional dependence target.</p>
355</li>
Shih-wei Liao19cc6972011-01-20 04:34:38 -0800356</ul>
357</div>
358<div class="section" id="example-command">
359<h1>Example Command</h1>
360<p>First:</p>
361<pre class="literal-block">
362$ cd &lt;Android_Root_Directory&gt;
363</pre>
364<p>Using frameworks/base/libs/rs/java/Fountain as a simple app in both
365Java and ScriptC, we can find the following command line in the build
366log:</p>
367<pre class="literal-block">
368$ out/host/linux-x86/bin/llvm-rs-cc \
369 -o out/target/common/obj/APPS/Fountain_intermediates/src/renderscript/res/raw \
370 -p out/target/common/obj/APPS/Fountain_intermediates/src/renderscript/src \
371 -d out/target/common/obj/APPS/Fountain_intermediates/src/renderscript \
372 -a out/target/common/obj/APPS/Fountain_intermediates/src/RenderScript.stamp \
373 -MD \
374 -I frameworks/base/libs/rs/scriptc \
375 -I external/clang/lib/Headers \
376 frameworks/base/libs/rs/java/Fountain/src/com/android/fountain/fountain.rs
377</pre>
378<p>This command will generate:</p>
379<ul class="simple">
380<li><strong>fountain.bc</strong></li>
381<li><strong>ScriptC_fountain.java</strong></li>
382<li><strong>ScriptField_Point.java</strong></li>
383</ul>
384<p>The <strong>Script*.java</strong> files above will be documented below.</p>
385</div>
386<div class="section" id="example-program-fountain-rs">
387<h1>Example Program: fountain.rs</h1>
388<p>fountain.rs is in ScriptC language, which is based on the standard
389C99. However, llvm-rs-cc goes beyond &quot;clang -std=c99&quot; and provides the
390following important features:</p>
391</div>
392<div class="section" id="pragma">
393<h1>1. Pragma</h1>
394<ul>
395<li><p class="first"><em>#pragma rs java_package_name([PACKAGE_NAME])</em></p>
396<p>The ScriptC_[SCRIPT_NAME].java has to be packaged so that Java
397developers can invoke those APIs.</p>
398<p>To do that, a ScriptC programmer should specify the package name, so
399that llvm-rs-cc knows the package expression and hence the directory
400for outputting ScriptC_[SCRIPT_NAME].java.</p>
401<p>In fountain.rs, we have:</p>
402<pre class="literal-block">
403#pragma rs java_package_name(com.android.fountain)
404</pre>
405<p>In ScriptC.fountain.java, we have:</p>
406<pre class="literal-block">
407package com.android.fountain
408</pre>
409<p>Note that the ScriptC_fountain.java will be generated inside
410./com/android/fountain/.</p>
411</li>
412<li><p class="first">#pragma version(1)</p>
413<p>This pragma is for evolving the language. Currently we are at
414version 1 of the language.</p>
415</li>
416</ul>
417</div>
418<div class="section" id="basic-reflection-export-variables-and-functions">
419<h1>2. Basic Reflection: Export Variables and Functions</h1>
420<p>llvm-rs-cc automatically export the &quot;externalizable and defined&quot; functions and
421variables to Android's Java side. That is, scripts are accessible from
422Java.</p>
423<p>For instance, for:</p>
424<pre class="literal-block">
425int foo = 0;
426</pre>
427<p>In ScriptC_fountain.java, llvm-rs-cc will reflect it to:</p>
428<pre class="literal-block">
429void set_foo(int v)...
430
431int get_foo()...
432</pre>
433<p>This access takes the form of generated classes which provide access
434to the functions and global variables within a script. In summary,
435global variables and functions within a script that are not declared
436static will generate get, set, or invoke methods. This provides a way
437to set the data within a script and call to its functions.</p>
438<p>Take the addParticles function in fountain.rs as an example:</p>
439<pre class="literal-block">
440void addParticles(int rate, float x, float y, int index, bool newColor) {
441 ...
442}
443</pre>
444<p>llvm-rs-cc will genearte ScriptC_fountain.java as follows:</p>
445<pre class="literal-block">
446void invoke_addParticles(int rate, float x, float y,
447 int index, bool newColor) {
448 ...
449}
450</pre>
451</div>
452<div class="section" id="export-user-defined-structs">
453<h1>3. Export User-Defined Structs</h1>
454<p>In fountain.rs, we have:</p>
455<pre class="literal-block">
456typedef struct __attribute__((packed, aligned(4))) Point {
457 float2 delta;
458 float2 position;
459 uchar4 color;
460} Point_t;
461
462Point_t *point;
463</pre>
464<p>llvm-rs-cc generates one ScriptField*.java file for each user-defined
465struct. I.e., in this case llvm-rs-cc will reflect to two files,
466ScriptC_fountain.java and ScriptField_Point.java.</p>
467<p>Note that when the type of exportable variable is struct, ScriptC
468developers should avoid anonymous structs. This is because llvm-rs-cc
469uses the struct name to name the file, instead of the typedef name.</p>
470<p>For the generated Java files, using ScriptC_fountain.java as an
471example we have:</p>
472<pre class="literal-block">
473void bind_point(ScriptField_Point v)
474</pre>
475<p>This binds your object with the allocated memory.</p>
476<p>You can bind the struct(e.g., Point), using the setter and getter
477method in ScriptField_Point.java.</p>
478<p>After binding, you could get the object from this method:</p>
479<pre class="literal-block">
480ScriptField_Point get_point()
481</pre>
482<p>In ScriptField_Point_s.java:</p>
483<pre class="literal-block">
484...
485// Copying the Item, which is the object that stores every
486// fields of struct, to the *index*\-th entry of byte array.
487//
488// In general, this method would not be invoked directly
489// but is used to implement the setter.
490void copyToArray(Item i, int index)
491
492// The setter of Item array,
493// index: the index of the Item array
494// copyNow: If true, it will be copied to the *index*\-th entry
495// of byte array.
496void set(Item i, int index, boolean copyNow)
497
498// The getter of Item array, which gets the *index*-th element
499// of byte array.
500Item get(int index)
501
502set_delta(int index, Float2 v, boolean copyNow)
503
504// The following is the individual setters and getters of
505// each field of a struct.
506public void set_delta(int index, Float2 v, boolean copyNow)
507public void set_position(int index, Float2 v, boolean copyNow)
508public void set_color(int index, Short4 v, boolean copyNow)
509public Float2 get_delta(int index)
510public Float2 get_position(int index)
511public Short4 get_color(int index)
512
513// Copying all Item array to byte array (i.e., memory allocation).
514void copyAll()
515...
516</pre>
517</div>
518<div class="section" id="summarize-the-java-reflection-above">
519<h1>4. Summarize the Java Reflection above</h1>
520<p>Let us summarize the high-level design of reflection next.</p>
521<ul>
522<li><p class="first">In terms of script's global functions, they can be called from Java.
523These calls operate asynchronously and no assumptions should be made
524upon with a function called will actually complete operation. If it
525is necessary to wait for a function to complete the java application
526may call the runtime finish method which will wait for all the script
527threads to complete. Two special functions also exist:</p>
528<ul class="simple">
529<li>The function <strong>init</strong> present will be called once after the script
530is loaded. This is useful to initialize data or anything else the
531script may need before it can be used. The init may not depend on
532globals initialized from Java as it will be called before these
533can be initialized.</li>
534<li>The function <strong>root</strong> is a special function for graphics. Which a
535script must redraw its contents this function will be called. No
536assumptions should be made as to when this function will be
537called. It will only be called if the script is bound as root.
538Also calls to this will be synchronized with data updates and
539other invocations from Java. Thus the script will not change due
540to external influence during a run of <strong>root</strong>. The return value
541indicates to the runtime if the function should be called again to
542redraw in the future. A return value of 0 indicates that no
543redraw is necessary until something changes. Any positive integer
544indicates a time in ms that the runtime should wait before calling
545root again to render another frame.</li>
546</ul>
547</li>
548<li><p class="first">In terms of script's global data, global variables can be written
549from Java. The Java class will cache the value or object set and
550provide return methods to retrieve this value. If a script updates
551the value, this update will not propagate back to the Java class.
552Initializers if present will also initialize the cached Java value.
553This provides a convenient way to declare constants within a script and
554make them accessible from the java runtime. If the script declares a
555variable const, only the get methods will be generated.</p>
556<p>Globals within a script are considered local to the script. They
557cannot be accessed by other scripts and are in effect always 'static'
558in the traditional C sense. Static here is used to control if a
559accessor is generated. Static continues to mean <em>not
560externally visible</em> and thus prevents the generation of
561accessors. Globals are persistent across invocations to a script and
562thus may be used to hold data from run to run.</p>
563<p>Globals of two types may be reflected into the Java class. The first
564type is basic non-pointer types. Types defined in rs_types.rsh may be
565used. For the non-pointer class get and set methods are generated in
566Java. Globals of single pointer types behave differently. These may
567use more complex types. Simple structures composed of the types in
568rs_types.rsh may also be used. These globals generate bind points in
569java. If the type is a structure they also generate a <strong>Field</strong> class
570used to pack and unpack the contents of the structure. Binding an
571allocation to one of these bind points in Java effectively sets the
572pointer in the script. Bind points marked const indicate to the
573runtime that the script will not modify the contents of an allocation.
574This may allow the runtime to make more effective use of threads.</p>
575</li>
576</ul>
577</div>
578<div class="section" id="vector-types">
579<h1>5. Vector Types</h1>
580<p>Vector types such as float2, float4, and uint4 are included to support
581vector processing in environments where the processors provide vector
582instructions.</p>
583<p>On non-vector systems the same code will continue to run but without
584the performance advantage. Function overloading is also supported.
585This allows the runtime to support vector version of the basic math
586routines without the need for special naming. For instance,</p>
587<ul class="simple">
588<li><em>float sin(float);</em></li>
589<li><em>float2 sin(float2);</em></li>
590<li><em>float4 sin(float4);</em></li>
591</ul>
592</div>
593</div>
594</body>
595</html>