Project

General

Profile

Template Description Language » History » Version 32

Andrei Tatarnikov, 05/20/2014 06:22 PM

1 5 Alexander Kamkin
h1. Template Description Language
2 4 Alexander Kamkin
3
_~By Artemiy Utekhin~_
4 1 Alexander Kamkin
5 21 Alexander Kamkin
*UNDER CONSTRUCTION*
6
7 6 Alexander Kamkin
{{toc}}
8
9 27 Andrei Tatarnikov
h2. Introduction
10
11 30 Andrei Tatarnikov
MicroTESK generates test programs on the basis of _*test templates*_ that provide an abstract description of scenarios to be reproduced by the generated programs. Test templates are created using the _*test template description language*_. It is a Ruby-based domain-specific language that provides facilities to describe tests in terms of the target microprocessor''s ISA and to manage the structure of the generated test programs. The language is implemented as a library that includes functionality for describing test templates and for processing these test templates to produce a test program. MicroTESK uses the JRuby interpreter to process test templates, which allows interaction between Ruby libraries and other parts of MicroTESK written in Java.
12 1 Alexander Kamkin
13 30 Andrei Tatarnikov
h3. Test Template Structure
14 1 Alexander Kamkin
15 3 Artemiy Utekhin
h2. The translation process
16 1 Alexander Kamkin
17 20 Alexander Kamkin
Ruby-TDL code describes a template of a test program which is then translated into TL code using the API of the MicroTESK CPU model parser and simulator (and, by extension, the constraint solver and other MicroTESK components). The process can be described as follows:
18 1 Alexander Kamkin
19 3 Artemiy Utekhin
# Receiving model metadata from the simulator;
20
# Template pre-processing;
21
# Constructing commands in the simulator;
22
# Executing commands in the simulator;
23
# Receiving the assembler code from the simulator (based on the CPU Sim-nML description);
24
# Writing the TL output to target files.
25 1 Alexander Kamkin
26 3 Artemiy Utekhin
Depending on the circumstances some of these steps may be done concurrently. 
27 1 Alexander Kamkin
28 3 Artemiy Utekhin
h2. Configuration
29 1 Alexander Kamkin
30 3 Artemiy Utekhin
_Pending..._
31 1 Alexander Kamkin
32 3 Artemiy Utekhin
h2. Execution
33 1 Alexander Kamkin
34 7 Artemiy Utekhin
Right now there is a simple execution script that requires the name of a package containing the CPU model (which is responsible for simulating the target processor and is generated by the [[Sim-nML Translator]]), a template file and an optional output file (if it''s not provided the library uses the standard output to print out the results). To run a template, execute the following command:
35 1 Alexander Kamkin
36 32 Andrei Tatarnikov
<pre>generate <model name> <template file.rb> [<output file.asm>]</pre>
37 7 Artemiy Utekhin
38
Example:
39
40 31 Andrei Tatarnikov
<pre>sh bin/generate.sh demo arch/demo/templates/demo_template.rb test.asm</pre>
41 1 Alexander Kamkin
42 3 Artemiy Utekhin
h2. Writing templates
43 1 Alexander Kamkin
44 3 Artemiy Utekhin
h3. Basic features
45 1 Alexander Kamkin
46 20 Alexander Kamkin
The two core abstractions used by MicroTESK parser/simulator and Ruby-TDL are an *instruction* and an *addressing mode*. An instruction is rather self-explanatory, it simply represents a target assembler instruction. Every argument of an instruction is a parametrized *addressing mode* that explains the meaning of the provided values to the simulator. The mode could point to the registers, for instance, or to a specific memory location. It can also denote an immediate value - e.g. a simple integer or a string. Thus, a basic template is effectively a sequence of instructions with parametrized addressing modes as their arguments.
47 1 Alexander Kamkin
48 20 Alexander Kamkin
Each template is a class that inherits a basic Template class that provides most of the core Ruby-TDL functionality. So, to write a template you need to subclass Template first:
49 1 Alexander Kamkin
50 11 Andrei Tatarnikov
<pre><code class="ruby">require_relative "_path-to-the-rubymt-library_/mtruby"
51 1 Alexander Kamkin
52 11 Andrei Tatarnikov
class MyTemplate < Template</code></pre>
53 1 Alexander Kamkin
54 20 Alexander Kamkin
While processing a template Ruby-TDL calls its %pre%, %run% and %post% methods, loosely meaning the pre-conditions, the main body and the post-conditions. The %pre% method is mostly useful for setup common to many templates, the %post% method will be more important once sequential testing is introduced. Most of the template code is supposed to be in the %run% method. Thus, a template needs to override one or more of these methods, most commonly %run%.
55 1 Alexander Kamkin
56 3 Artemiy Utekhin
To get %pre% and %post% over with, the most common usage of these is to make a special non-executable class and then subclass it with the actual templates:
57 1 Alexander Kamkin
58 10 Andrei Tatarnikov
<pre><code class="ruby">require_relative "_path-to-the-rubymt-library_/mtruby"
59 1 Alexander Kamkin
60 3 Artemiy Utekhin
class MyPrepost < Template
61
  def initialize
62
    super
63
    @is_executable = no
64
  end
65 1 Alexander Kamkin
66 3 Artemiy Utekhin
  def pre
67
    # Your ''startup'' code goes here
68
  end
69 1 Alexander Kamkin
70 3 Artemiy Utekhin
  def post
71
    # Your ''cleanup'' code goes here
72
  end
73 9 Andrei Tatarnikov
end</code></pre>
74 1 Alexander Kamkin
75 11 Andrei Tatarnikov
<pre><code class="ruby">require_relative "_path-to-the-rubymt-library_/mtruby"
76 1 Alexander Kamkin
77 3 Artemiy Utekhin
class MyTemplate < MyPrepost
78
  def initialize
79
    super
80
    @is_executable = yes
81
  end
82
  
83
  def run
84
    # Your template code goes here
85
  end
86 11 Andrei Tatarnikov
end</code></pre>
87 1 Alexander Kamkin
88 3 Artemiy Utekhin
These methods essentially contain the instructions. The general instruction format is slightly more intimidating than the native assembler and looks like this:
89 1 Alexander Kamkin
90 16 Andrei Tatarnikov
<pre><code class="ruby">instruction_name addr_mode1(:arg1_1 => value, :arg1_2 => value, ...), addr_mode2(:arg2_1 => value, ...), ...</code></pre>
91 1 Alexander Kamkin
92 3 Artemiy Utekhin
So, for instance, if the simulator has an ADD(MEM(i), MEM(i)|IMM(i)) instruction, it would look like:
93 1 Alexander Kamkin
94 16 Andrei Tatarnikov
<pre><code class="ruby">add mem(:i => 42), imm(:i => 128)</code></pre>
95 1 Alexander Kamkin
96 3 Artemiy Utekhin
Thankfully, there are shortcuts. If there''s only one argument expected in the addressing mode, you can simply write its value and never have to worry about the argument name. And, by convention, the immediate values are always denoted in the simulator as the IMM addressing mode, so the template parser automatically accepts numbers and strings as such. Thus, in this case, the instruction can be simplified to:
97 1 Alexander Kamkin
98 16 Andrei Tatarnikov
<pre><code class="ruby">add mem(42), 128</code></pre>
99 3 Artemiy Utekhin
100 8 Artemiy Utekhin
As a matter of fact, if you''re sure about the order of addressing mode arguments, you can omit the names altogether and simply provide the values:
101
102 16 Andrei Tatarnikov
<pre><code class="ruby">instruction_name addr_mode1(value1, value2, ...) ...</code></pre>
103 8 Artemiy Utekhin
104 3 Artemiy Utekhin
If the name of the instruction conflicts with an already existing Ruby method, the instruction will be available with an %op_% prefix before its name.
105
106
h3. Test situations
107
108
_This section is to be taken with a grain of salt because the logic and the interface behind the situations is not yet finalized and mostly missing from the templates and shouldn''t be used yet_
109
110
_Big TODO: define what is a test situation_
111
112
To denote a test situation, add a Ruby block that describes situations to an instruction, this will loosely look like this (likely similar to the way the addressing modes are denoted):
113
114 17 Andrei Tatarnikov
<pre><code class="ruby">sub mem(42), mem(21) do overflow(:op1 => 123, :op2 => 456) end</code></pre>
115 3 Artemiy Utekhin
116
h3. Instruction blocks
117
118
Sometimes a certain test situation should influence more than just one instruction. In that case, you can pass the instructions in an atomic block that can optionally accept a Proc of situations as its argument (because Ruby doesn''t want to be nice and allow multiple blocks for a method, and passing a Hash of Proc can hardly be called comfortable).
119
120 12 Andrei Tatarnikov
<pre><code class="ruby">p = lambda { overflow(:op1 => 123, :op2 => 456) }
121 3 Artemiy Utekhin
122
atomic p {
123
  mov mem(25), mem(26)
124
  add mem(27), 28
125
  sub mem(29), 30
126 12 Andrei Tatarnikov
}</code></pre>
127 3 Artemiy Utekhin
128 25 Andrei Tatarnikov
h3. Groups and random selections _(N.B. REMOVED in r1923. The implementation does not work in the current build and, therefore, was removed. The described features must be reviewed and reimplemented if required.)_
129 3 Artemiy Utekhin
130 24 Andrei Tatarnikov
From source code comments:
131
132
<pre>
133
# VERY UNTESTED leftovers from the previous version ("V2", this is V3)
134
# Should work with the applied fixes but I''d be very careful to use these
135
136
# As things stand this is just a little discrete probability utility that
137
# may or may not find its way into the potential ruby part of the test engine
138
</pre>
139
140 3 Artemiy Utekhin
There are certain ways to group together or randomize addressing modes and instructions.
141
142
To group several addressing modes together (this only works if they have similar arguments) create a mode group like this:
143
144 17 Andrei Tatarnikov
<pre><code class="ruby">mode_group "my_group" [:mem, :imm]</code></pre>
145 3 Artemiy Utekhin
146
You can also set weights to each of the modes in the group like this:
147
148 17 Andrei Tatarnikov
<pre><code class="ruby">mode_group "my_group" {:mem => 1.5, :imm => 2.5}</code></pre>
149 3 Artemiy Utekhin
150
The name of the group is converted into a method in the Template class. To select a random mode from a group, use %sample% on this generated method:
151
152 17 Andrei Tatarnikov
<pre><code class="ruby">add mem(42), my_group.sample(21)</code></pre>
153 3 Artemiy Utekhin
154
_TODO: sampling already parametrized modes_
155
156
The first method of grouping instructions works in a similar manner with the same restrictions on arguments:
157
158 17 Andrei Tatarnikov
<pre><code class="ruby">group "i_group" [:add, :sub]</code></pre>
159 3 Artemiy Utekhin
160 17 Andrei Tatarnikov
<pre><code class="ruby">group "i_group" {:add => 0.3, :sub => 0.7]</code></pre>
161 3 Artemiy Utekhin
162 17 Andrei Tatarnikov
<pre><code class="ruby">i_group.sample mem(42), 21</code></pre>
163 3 Artemiy Utekhin
164
You can also run all of the instructions in a group at once by using the %all% method:
165
166 17 Andrei Tatarnikov
<pre><code class="ruby">i_group.all mem(42), 21</code></pre>
167 3 Artemiy Utekhin
168
The second one allows you to create a normal block of instructions, setting their arguments separately. 
169
170 17 Andrei Tatarnikov
<pre><code class="ruby">block_group "b_group" do
171 3 Artemiy Utekhin
  mov mem(25), mem(26)
172
  add mem(27), 28
173
  sub mem(29), 30
174 17 Andrei Tatarnikov
end</code></pre>
175 3 Artemiy Utekhin
176
In this case to set weights you should call a %prob% method before every instruction:
177
178 17 Andrei Tatarnikov
<pre><code class="ruby">block_group "b_group" do
179 3 Artemiy Utekhin
  prob 0.1
180
  mov mem(25), mem(26)
181
  prob 0.7
182
  add mem(27), 28
183
  prob 0.4
184
  sub mem(29), 30
185 17 Andrei Tatarnikov
end</code></pre>
186 3 Artemiy Utekhin
187
The usage is almost identical, but without providing the arguments as they are already set:
188
189 18 Andrei Tatarnikov
<pre><code class="ruby">b_group.sample
190
b_group.all</code></pre>
191 3 Artemiy Utekhin
192
_Not sure how does it work inside atomics when the group is defined outside, needs more consideration_
193
194
_TODO: Permutations_
195
196
Any normal Ruby code is allowed inside the blocks as well as the %run%-type methods, letting you write more complex or inter-dependent templates.
197 8 Artemiy Utekhin
198
h3. TODO: Labels
199
200
To set a label write:
201
202 18 Andrei Tatarnikov
<pre><code class="ruby">label :label_name</code></pre>
203 8 Artemiy Utekhin
204
To use a label in an instruction that accepts one (under the hood it''s just a simple immediate #IMM value - just not a pre-defined one until it''s actually defined):
205
206 18 Andrei Tatarnikov
<pre><code class="ruby">b greaterThan, :label_name</code></pre>
207 8 Artemiy Utekhin
208
h3. TODO: Debug
209
210
To get a value from registers use:
211
212 15 Andrei Tatarnikov
<pre><code class="ruby">get_reg_value("register_name", index)</code></pre>
213 8 Artemiy Utekhin
214
Right now the pre-processing and the execution of instructions are separated due to ambiguous logic regarding labels and various blocks and atomics. This may be changed later, so these special debugging blocks might become unnecessary. By default what''s written in the template is run during pre-processing so you have to use special blocks if you want to run some Ruby code during the execution stage, most likely some debugging.
215
216
To print some debug in the console during the execution of the instructions use the exec_debug block:
217
218 15 Andrei Tatarnikov
<pre><code class="ruby">exec_debug {
219 8 Artemiy Utekhin
  puts "R0: " + get_reg_value("GPR", 0).to_s + ", R1: " + get_reg_value("GPR", 1).to_s# + ", label code: " + self.send("cycle" + ind.to_s).to_s
220 15 Andrei Tatarnikov
}</code></pre>
221 8 Artemiy Utekhin
222
To save something that depends on the current state of the simulator to the resulting assembler code use exec_output that should return a string:
223
224 14 Andrei Tatarnikov
<pre><code class="ruby">exec_output {
225 8 Artemiy Utekhin
  "// The result should be " + self.get_reg_value("GPR", 0).to_s
226 13 Andrei Tatarnikov
}</code></pre>