source: proto/s2k/trunk/framework/input/test/s2k/proto/grep/grep_flow.s2k @ 4049

Last change on this file since 4049 was 4049, checked in by ksherdy, 5 years ago

Minor updates to s2k source file.

File size: 5.9 KB
Line 
1/*
2 * grep exact-string search C++ in \s2k{}.
3 *
4 * Description:
5 *
6 * The \s2k{} grep exact-string search demo is organized into the following
7 * pipeline stages.
8 * 
9 * Stage 1 - Read`
10 * Stage 2 - Transpose~
11 * Stage 3 - ClassifyBytes~
12 * Stage 4 - Match~
13 * Stage 5 - MatchLines~
14 * Stage 6 - Write`
15 *
16 * Trailing tildes characters `~' indicate pipeline stages written in \s2k{}.
17 * Trailing back ticks `'', indicate stages defined within the
18 * \s2k{} C++ templates file, 'grep.template'.
19 *
20 * The \s2k{} program defines the following program elements.
21 *
22 * 1. Stream structures Lex and Output.
23 *
24 * 2. Stream functions ClassifyBytes, Match, and MatchLines.
25 * ClassifyBytes defines \s2k{} equations for each of the character
26 * classes encountered in the fixed string `apple' and line feeds.
27 * In this example, the generation of character class bit stream
28 * equations is automated using the character class compiler tool.
29 * Match defines \s2k{} equations to match the fixed string `apple'
30 * and line feeds.
31 *
32 * 3. Stream `graph' of stream filters.
33 *
34 *
35 * The \s2k{} compiler translates \s2k{} to \b2k{} to C++ code as follows.
36 *
37 * 1. \s2k{} stream structures to \b2k{} bitblock structure
38 *    to bitblock C/C++ structures, i.e., simple data containers.
39 *
40 * 2. \s2k{} stream filters to \b2k{} bitblock kernels to
41 *    C/C++ structures, i.e., objects with methods.
42 *    C/C++ structures are generated with `do_block', `do_final_blocks', `clear'
43 *    member functions, as well as member variables to capture
44 *    the propagation of state information across segment and register
45 *    boundaries for block-by-block stream computations.
46 *
47 * 3. \s2k{} `graph' method to sequence of stream function `do_block',
48 *    `do_final_block', and `clear' function calls.
49 */
50
51/*
52struct Input {
53  stream<8> data;
54};
55*/
56
57/*
58struct Basis_bits{
59  stream bit_0;
60  stream bit_1;
61  stream bit_2;
62  stream bit_3;
63  stream bit_4;
64  stream bit_5;
65  stream bit_6;
66  stream bit_7;
67};
68*/
69
70struct Lex{
71  stream a;
72  stream p;
73  stream l;
74  stream e;
75  stream LF;
76};
77
78struct Output{
79  stream match_follows;
80  stream lines;
81  stream line_starts;
82  stream line_ends;
83};
84
85/*
86Runtime.h
87
88  // Associates a C array with an \s2k{} stream variable.
89  bind<char>(buffer, stream<8> s, int size);
90 
91  // Reads a C array into an \s2k{} variable.
92  read<>(s);
93 
94  // Writes an \s2k{} variable to a C array.
95  write<>(s);
96
97};
98
99*/
100
101filter Source() {
102
103}
104
105filter ClassifyBytes(in struct Basis_bits basis_bits, out struct Lex lex) {
106  stream temp1 = (basis_bits.bit_1 & (~ basis_bits.bit_0));
107  stream temp2 = (basis_bits.bit_2 & (~ basis_bits.bit_3));
108  stream temp3 = (temp1 & temp2);
109  stream temp4 = (basis_bits.bit_4 | basis_bits.bit_5);
110  stream temp5 = (basis_bits.bit_7 & (~ basis_bits.bit_6));
111  stream temp6 = (temp5 & (~ temp4));
112  lex.a = (temp3 & temp6);
113  stream temp7 = (basis_bits.bit_2 & basis_bits.bit_3);
114  stream temp8 = (temp1 & temp7);
115  stream temp9 = (basis_bits.bit_6 | basis_bits.bit_7);
116  stream temp10 = (temp4 | temp9);
117  lex.p = (temp8 & (~ temp10));
118  stream temp11 = (basis_bits.bit_4 & basis_bits.bit_5);
119  stream temp12 = (temp11 & (~ temp9));
120  lex.l = (temp3 & temp12);
121  stream temp13 = (basis_bits.bit_5 & (~ basis_bits.bit_4));
122  stream temp14 = (temp13 & temp5);
123  lex.e = (temp3 & temp14);
124  stream temp15 = (basis_bits.bit_0 | basis_bits.bit_1);
125  stream temp16 = (basis_bits.bit_2 | basis_bits.bit_3);
126  stream temp17 = (temp15 | temp16);
127  stream temp18 = (basis_bits.bit_4 & (~ basis_bits.bit_5));
128  stream temp19 = (basis_bits.bit_6 & (~ basis_bits.bit_7));
129  stream temp20 = (temp18 & temp19);
130  lex.LF = (temp20 & (~ temp17));
131}
132
133filter Match(in struct Lex lex, out struct Output output) {
134  stream cursor = flow.Advance(lex.a);
135  cursor = flow.Advance((cursor & lex.p));
136  cursor = flow.Advance((cursor & lex.p));
137  cursor = flow.Advance((cursor & lex.l));
138  cursor = flow.Advance((cursor & lex.e));
139  output.match_follows = cursor;
140}
141
142filter MatchLines(in struct Lex lex, out struct Output output) {
143  stream all_line_starts = (flow.ScanToFirst((~ lex.LF)) | (flow.Advance(lex.LF) & (~ lex.LF)));
144  stream all_line_ends = lex.LF;
145  stream last_line_start = flow.ScanToFirst(all_line_starts);
146  stream cursor = last_line_start;
147  while (flow.inFile(cursor)) {
148      if ((cursor & all_line_starts)) {
149          last_line_start = cursor;
150      }
151      if ((cursor & output.match_follows)) {
152          cursor = flow.ScanTo(cursor, lex.LF);
153          output.lines |= flow.InclusiveSpan(last_line_start, cursor);
154      }
155      cursor = flow.AdvanceThenScanTo(cursor, (all_line_starts | output.match_follows));
156  }
157  output.line_starts = (output.lines & all_line_starts);
158  output.line_ends = flow.ScanTo(output.line_starts, (output.lines & all_line_ends));
159}
160filter Read(out struct Output output) {
161  //io.bind<char>(buffer, s76543210, 1024);
162  //io.bind<BitBlock>(starts, output.starts, 1024/BITBLOCK_SIZE);
163  //io.bind<BitBlock>(ends, output.ends, 1024/BITBLOCK_SIZE);
164  //io.bind<BitBlock>(markers, output.markers, 1024/BITBLOCK_SIZE);
165
166  //io.read_buffer<char>(s76543210);
167}
168
169// post-processing
170filter Write(out struct Output output) {
171  //io.write<BitBlock>(output.line_starts);
172  //io.write<BitBlock>(output.line_ends);
173  //io.write<BitBlock>(output.match_follows);
174}
175
176graph pipeline() { /* generates struct pipeline { void open(){} void process(){} void close(){} }; */
177
178    // char * byte_data;
179    struct Basis_bits basis_bits;
180    struct Lex lex;
181    struct Output output;
182   
183//  filter Read read; /* Begins with a filter that contains bind calls, io.read<>. */
184    filter Transpose transpose;
185    filter ClassifyBytes classifyBytes;
186    filter Match match;
187    filter MatchLines matchLines;
188//  filter Write write; /* Ends with a filter that contains write calls, io.write<>. */
189
190    // read();
191    transpose(byte_data, basis_bits);
192    classifyBytes(basis_bits, lex);
193    match(lex, output);
194    matchLines(lex, output);
195    // write(output);
196   
197}
198
Note: See TracBrowser for help on using the repository browser.