OSDN Git Service

27142e32504c96f947e19e226bd27191c32366a6
[pf3gnuchains/pf3gnuchains3x.git] / cgen / doc / app.texi
1 @c Copyright (C) 2000 Red Hat, Inc.
2 @c This file is part of the CGEN manual.
3 @c For copying conditions, see the file cgen.texi.
4
5 @node Writing an application
6 @chapter Writing an application
7 @cindex Writing an application
8
9 This chapter contains information for those wishing to write their own
10 CGEN application.
11
12 @menu
13 * File Layout::                 Organization of source files
14 * File Generation Process::     Workflow in cgen
15 * Coding Conventions::          Coding conventions
16 * Accessing Loaded Data::       Reading data from loaded .cpu files
17 * Name References::             Architecture names in generated code
18 * String Building::             Building long strings and writing them out
19 * COS::                         Cgen's Object System
20 @end menu
21
22 @node File Layout
23 @section File Layout
24
25 Source files in cgen are organized in a very specific way.@footnote{As the
26 number of source files grows the entire layout may be changed, but until then
27 this is how things are.}  It makes it easy to find things.
28
29 @itemize @bullet
30 @item top level file is cgen-<app>.scm
31 The best way to create this file is to copy an existing application's file
32 (e.g. cgen-opc.scm) and modify to suit.
33 @item file <app>.scm contains general app-specific utilities
34 @item other files are <app>-foo.scm
35 @item add entry to dev.scm (load-<app>)
36 @end itemize
37
38 @node File Generation Process
39 @section File Generation Process
40
41 This is an overview of cgen workflow.
42
43 @itemize @bullet
44
45 @item cgen is started with list of files to generate and code generation
46 options
47
48 @item source code is loaded
49
50 @itemize @minus
51 @item application independent code is loaded if not compiled in
52 @item application specific code is loaded
53
54 Currently app-specific code is never compiled in.
55 @itemize @minus
56 @item doesn't affect speed as much as application independent stuff
57 @item subject to more frequent changes
58 @item makes it easier to do application development if changes to .scm
59 files are "ready to use"
60 @end itemize
61 @end itemize
62
63 @item ultimately procedure `cpu-load' is called which is the main driver for
64 loading .cpu files
65
66 @item various data structures are initialized
67
68 @item data files are loaded
69
70 @itemize @minus
71 @item main <arch>.cpu file is loaded
72
73 There is a #include-like mechanism for loading other files so big
74 architectures can be broken up into several files.
75
76 While the architecture description is being loaded, entries not requested
77 are discarded.  This happens, for example, when building a simulator:
78 there's no point in keeping instructions specific to a machine that is
79 not being generated.  What to keep is based on the MACH and ISA attributes.
80
81 @item application specific data files are loaded
82
83 e.g. <arch>.sim
84 @end itemize
85
86 @item builtin elements are created
87
88 @item each requested file is generated by calling cgen-<file> generator
89
90 The output is written to the output file with @code{with-output-to-file} so
91 the code must write to @code{(current-output-port)}.
92
93 Some files require heavy duty processing of the cpu description.
94 For example the simulator computes the instruction formats from the
95 instruction field lists of each instruction.  This computation is defered
96 to each cgen-<file> procedure that needs it and must be explicitly requested
97 by them.  The results are cached so this is only done once of course.
98
99 @item additional processing for some opcodes files
100
101 Several opcodes files are built from three sources.
102
103 @itemize @minus
104 @item generated code
105
106 @item section in <arch>.opc file
107
108 It's not appropriate to put large amounts of C (or perhaps any C) in
109 cgen description files, yet some things are best expressed in some
110 other language (e.g. assembler/disassembler operand parsing/printing).
111
112 @item foo.in file
113
114 It seems cleaner to put large amounts of non-machine-generated C
115 in separate files from code generator.
116 @end itemize
117
118 @end itemize
119
120 @node Coding Conventions
121 @section Coding Conventions
122
123 @itemize @bullet
124 @item unless definition occupies one line, final trailing parenthesis is on
125 a line by itself beginning in column one
126 @item definitions internal to a source file begin with '-'
127 @item global state variables are named *foo-bar*
128 [FIXME: current code needs updating]
129 @item avoid uppercase (except for ???)
130 @item procedures that return a boolean result end in '?'
131 @item procedures that modify something end in '!'
132 @item classes are named <name>
133 @end itemize
134
135 @node Accessing Loaded Data
136 @section Accessing Loaded Data
137
138 Each kind of description file entry (defined with `define-foo') is recorded
139 in an object of class <foo>.@footnote{not true for <arch> but will be RSN}
140 All the data is collected together in an object of class
141 <system>.@footnote{got a better name?}
142 @footnote{modes aren't recorded here, should they be?}
143
144 Data for the currently selected architecture is obtained with several
145 access functions.
146
147 @smallexample
148   (current-arch-name)
149   - return symbol that is the name of the arch
150   - this is the name specified with `define-arch'
151
152   (current-arch-comment)
153   - return the comment specified with `define-arch'
154
155   (current-arch-atlist)
156   - return the attributes specified with `define-arch'
157
158   (current-arch-default-alignment)
159   - return a symbol indicated the default aligment
160     - one of aligned, unaligned, forced
161
162   (current-arch-insn-lsb0?)
163   - return a #t if the least significant bit in a word is numbered 0
164   - return a #f if the most significant bit in a word is numbered 0
165
166   (current-arch-mach-name-list)
167   - return a list of names (as symbols) of all machs in the architecture
168
169   (current-arch-isa-name-list)
170   - return a list of names (as symbols) of all isas in the architecture
171
172   - for most of the remaining elements, there are three main accessors
173     [foo is sometimes abbreviated]
174     - current-foo-list - returns list of <foo> objects in the architecture
175     - current-foo-add! - add a <foo> object to the architecture
176     - current-foo-lookup - lookup the <foo> object based on its name
177
178   <atlist>
179   (current-attr-list)
180   (current-attr-add!)
181   (current-attr-lookup)
182
183   <enum>
184   (current-enum-list)
185   (current-enum-add!)
186   (current-enum-lookup)
187
188   <keyword>
189   (current-kw-list)
190   (current-kw-add!)
191   (current-kw-lookup)
192
193   <isa>
194   (current-isa-list)
195   (current-isa-add!)
196   (current-isa-lookup)
197
198   <cpu>
199   (current-cpu-list)
200   (current-cpu-add!)
201   (current-cpu-lookup)
202
203   <mach>
204   (current-mach-list)
205   (current-mach-add!)
206   (current-mach-lookup)
207
208   <model>
209   (current-model-list)
210   (current-model-add!)
211   (current-model-lookup)
212
213   <hardware>
214   (current-hw-list)
215   (current-hw-add!)
216   (current-hw-lookup)
217
218   <ifield>
219   (current-ifld-list)
220   (current-ifld-add!)
221   (current-ifld-lookup)
222
223   <operand>
224   (current-op-list)
225   (current-op-add!)
226   (current-op-lookup)
227
228   <insn>
229   (current-insn-list)
230   (current-insn-add!)
231   (current-insn-lookup)
232
233   <macro-insn>
234   (current-minsn-list)
235   (current-minsn-add!)
236   (current-minsn-lookup)
237
238   (current-ifmt-list)
239   - return list of computed <iformat> objects
240
241   (current-sfmt-list)
242   - return list of computed <sformat> objects
243
244   [there are a few more to be documented, not sure they'll remain as is]
245 @end smallexample
246
247 @node Name References
248 @section Name References
249
250 To simplify writing code generators, system names can be
251 specified with fixed strings rather than having to compute them.
252 The output is post-processed to convert the strings to the actual names.
253 Upper and lower case names are supported.
254
255 @itemize @bullet
256 @item For the architecture name use @@arch@@, @@ARCH@@.
257 @item For the cpu family name use @@cpu@@, @@CPU@@.
258 @item For the prefix use @@prefix@@, @@PREFIX@@.
259 @end itemize
260
261 The @samp{prefix} notion is to segregate different code for the same
262 cpu family.  For example, this is used to segregate the ARM ISA from the
263 Thumb ISA.
264
265 @node String Building
266 @section String Building
267
268 Output generation uses a combination of writing text out as it is computed
269 and building text for later writing out.
270
271 The top level file generator uses @code{string-write}.  It takes string-lists
272 and thunks as arguments and writes each argument in turn to stdout.
273 String-lists are lists of strings (nested arbitrarily deep).  It's cheaper
274 to @code{cons} long strings together than to use @code{string-append}.
275 Thunks return string-lists to write out, but isn't computed until all
276 preceeding arguments to `string-write' have been written out.  This allows
277 defering building up of large amounts of text until it needs to be.
278
279 The main procedures for building strings and writing them out are:
280
281 @itemize @bullet
282
283 @item (string-write string-list-or-thunk1 string-list-or-thunk2 ...)
284
285 Loops over arguments writing them out in turn.
286
287 @item (string-write-map proc string-list-or-thunk-list)
288
289 Apply proc to each element in string-list-or-thunk-list and write out
290 the result.
291
292 @item (string-list arg1 arg2 ...)
293
294 Return list of arguments.  This is identical to @code{list} except it
295 is intended to take string-lists as arguments.
296
297 @item (string-list-map proc arg-list)
298
299 Return list of @code{proc} applied to each element of @code{arg-list}.
300 This is identical to @code{map} except it is intended to take strings
301 as arguments.
302
303 @item (string-append string1 string2 ...)
304
305 For small arguments it's just as well to use @code{string-append}.
306 This is a standard Scheme procedure.  The output is also easier to read
307 when developing interactively.  And some subroutines are used in multiple
308 contexts including some where strings are required.
309
310 @end itemize
311
312 @node COS
313 @section COS
314
315 COS is Cgen's Object System.  It's a simple OO system for Guile that
316 was written to provide something useful until Guile had its own.
317 COS will be replaced with GOOPs if the Scheme implementation of cgen is kept.
318
319 The pure Scheme implementation of COS uses vectors to record objects and
320 classes.  The C implementation uses smobs (though classes are still
321 implemented with vectors).
322
323 A complete list of user-visible functions is at the top of @file{cos.scm}.
324
325 Here is a list of the frequently used ones.
326
327 @itemize @bullet
328
329 @item (class-make name parent-name-list element-list method-list)
330
331 Use @code{class-make} to define a class.
332
333 @smallexample
334 name: symbol, <name-of-class>
335 parent-name-list: list of symbols, names of each parent class
336 element-list: list of either symbols or (symbol . initial-value)
337 method-list: list of (symbol . lambda)
338 @end smallexample
339
340 The result is the class's definition.  It is usually assigned to a global
341 variable with same name as class's name.  Current cgen code always does
342 this.  It's not a requirement but it is convention.
343
344 @item (new <class-name>)
345
346 Create a new object with @code{new}.
347 @code{<class-name>} is typically the global variable that recorded
348 the results of @code{class-make}.  The result is a new object of the
349 requested class.  Class elements have either an "undefined" value
350 or an initial value if one was specified when the class was defined.
351
352 @item (define-getters class-name prefix element-list)
353
354 Elements (aka members) are read/written with "accessors".
355 Read accessors are defined with @code{define-getters}, which
356 creates one procedure for each element, each defined as
357 @code{(prefix-element-name object)}.
358
359 This is a macro so don't quote anything.
360
361 @item (define-setters class-name prefix element-list)
362
363 Write accessors are defined with @code{define-setters}, which
364 creates one procedure for each element, each defined as
365 @code{(prefix-set-element-name! object new-value)}.
366
367 This is a macro so don't quote anything.
368
369 @item (elm-get object elm-name)
370
371 This can only be used in method definitions (blech, blah blah blah).
372
373 @item (elm-set! object elm-name new-value)
374
375 This can only be used in method definitions (blech, blah blah blah).
376
377 @item (send object method-name arg1 arg2)
378
379 Invoke method @code{method-name} on @code{object}.
380
381 The convention is to put this in a cover fn:
382 @code{(class-name-method-name object arg1 arg2)}.
383
384 @item (send-next object method-name arg1 arg2)
385
386 Same as @code{send} except only usable in methods and is used to invoke
387 the method in the parent class.
388
389 @item (make object . args)
390
391 One standard way to create a new object is with @code{make}.
392 It is a wrapper, defined as
393
394 @smallexample
395 (define (make object . args)
396   (apply send (cons (new object) (cons 'make! args)))
397 )
398 @end smallexample
399
400 @item (vmake class . args)
401
402 The other standard way to create objects is with @code{vmake}.
403
404 @code{args} is a list of option names and arguments.
405
406 ??? Not completely implemented yet.
407
408 @item (method-make! class method-name lambda)
409
410 The normal way of creating methods is to use @code{method-make!}, not define
411 them with the class.  It's just easier to define them separately.
412
413 @item (method-make-virtual! class method-name lambda)
414
415 Create virtual methods created with @code{method-make-virtual!}.
416
417 @item (method-make-forward! class elm-name methods) -> unspecified
418
419 Forwarding a method invocation on one object to another is extremely
420 useful so some utilities have been created to simplify creating forwarding
421 methods.
422
423 @code{methods} is a list of method names.  A method is created for each one
424 that forwards the method onto the object contained in element ELM-NAME.
425
426 @item (method-make-virtual-forward!)
427
428 Same as method-make-forward! except that it creates virtual methods.
429
430 @end itemize