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.
5 @node Writing an application
6 @chapter Writing an application
7 @cindex Writing an application
9 This chapter contains information for those wishing to write their own
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
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.
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>)
38 @node File Generation Process
39 @section File Generation Process
41 This is an overview of cgen workflow.
45 @item cgen is started with list of files to generate and code generation
48 @item source code is loaded
51 @item application independent code is loaded if not compiled in
52 @item application specific code is loaded
54 Currently app-specific code is never compiled in.
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"
63 @item ultimately procedure `cpu-load' is called which is the main driver for
66 @item various data structures are initialized
68 @item data files are loaded
71 @item main <arch>.cpu file is loaded
73 There is a #include-like mechanism for loading other files so big
74 architectures can be broken up into several files.
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.
81 @item application specific data files are loaded
86 @item builtin elements are created
88 @item each requested file is generated by calling cgen-<file> generator
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)}.
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.
99 @item additional processing for some opcodes files
101 Several opcodes files are built from three sources.
106 @item section in <arch>.opc file
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).
114 It seems cleaner to put large amounts of non-machine-generated C
115 in separate files from code generator.
120 @node Coding Conventions
121 @section Coding Conventions
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>
135 @node Accessing Loaded Data
136 @section Accessing Loaded Data
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?}
144 Data for the currently selected architecture is obtained with several
149 - return symbol that is the name of the arch
150 - this is the name specified with `define-arch'
152 (current-arch-comment)
153 - return the comment specified with `define-arch'
155 (current-arch-atlist)
156 - return the attributes specified with `define-arch'
158 (current-arch-default-alignment)
159 - return a symbol indicated the default aligment
160 - one of aligned, unaligned, forced
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
166 (current-arch-mach-name-list)
167 - return a list of names (as symbols) of all machs in the architecture
169 (current-arch-isa-name-list)
170 - return a list of names (as symbols) of all isas in the architecture
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
181 (current-attr-lookup)
186 (current-enum-lookup)
206 (current-mach-lookup)
211 (current-model-lookup)
221 (current-ifld-lookup)
231 (current-insn-lookup)
236 (current-minsn-lookup)
239 - return list of computed <iformat> objects
242 - return list of computed <sformat> objects
244 [there are a few more to be documented, not sure they'll remain as is]
247 @node Name References
248 @section Name References
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.
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@@.
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
265 @node String Building
266 @section String Building
268 Output generation uses a combination of writing text out as it is computed
269 and building text for later writing out.
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.
279 The main procedures for building strings and writing them out are:
283 @item (string-write string-list-or-thunk1 string-list-or-thunk2 ...)
285 Loops over arguments writing them out in turn.
287 @item (string-write-map proc string-list-or-thunk-list)
289 Apply proc to each element in string-list-or-thunk-list and write out
292 @item (string-list arg1 arg2 ...)
294 Return list of arguments. This is identical to @code{list} except it
295 is intended to take string-lists as arguments.
297 @item (string-list-map proc arg-list)
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
303 @item (string-append string1 string2 ...)
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.
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.
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).
323 A complete list of user-visible functions is at the top of @file{cos.scm}.
325 Here is a list of the frequently used ones.
329 @item (class-make name parent-name-list element-list method-list)
331 Use @code{class-make} to define a class.
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)
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.
344 @item (new <class-name>)
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.
352 @item (define-getters class-name prefix element-list)
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)}.
359 This is a macro so don't quote anything.
361 @item (define-setters class-name prefix element-list)
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)}.
367 This is a macro so don't quote anything.
369 @item (elm-get object elm-name)
371 This can only be used in method definitions (blech, blah blah blah).
373 @item (elm-set! object elm-name new-value)
375 This can only be used in method definitions (blech, blah blah blah).
377 @item (send object method-name arg1 arg2)
379 Invoke method @code{method-name} on @code{object}.
381 The convention is to put this in a cover fn:
382 @code{(class-name-method-name object arg1 arg2)}.
384 @item (send-next object method-name arg1 arg2)
386 Same as @code{send} except only usable in methods and is used to invoke
387 the method in the parent class.
389 @item (make object . args)
391 One standard way to create a new object is with @code{make}.
392 It is a wrapper, defined as
395 (define (make object . args)
396 (apply send (cons (new object) (cons 'make! args)))
400 @item (vmake class . args)
402 The other standard way to create objects is with @code{vmake}.
404 @code{args} is a list of option names and arguments.
406 ??? Not completely implemented yet.
408 @item (method-make! class method-name lambda)
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.
413 @item (method-make-virtual! class method-name lambda)
415 Create virtual methods created with @code{method-make-virtual!}.
417 @item (method-make-forward! class elm-name methods) -> unspecified
419 Forwarding a method invocation on one object to another is extremely
420 useful so some utilities have been created to simplify creating forwarding
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.
426 @item (method-make-virtual-forward!)
428 Same as method-make-forward! except that it creates virtual methods.