Changeset 2148


Ignore:
Timestamp:
May 24, 2012, 4:37:48 PM (7 years ago)
Author:
ksherdy
Message:

Refactored pablo_template. Eliminated global variables. Modularized common code.

Location:
proto/parabix2
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • proto/parabix2/pablo_template.cpp

    r2144 r2148  
    1313#include "../lib/bitblock_iterator.hpp"
    1414#include "../lib/s2p.hpp"
    15 
    16 #define SEGMENT_BLOCKS 12
    17 #define BUFFER_SIZE (BLOCK_SIZE * SEGMENT_BLOCKS)
    18 #define OVERLAP_BUFSIZE (sizeof(BitBlock))
     15#include "../lib/perflib/perfsec.h"
    1916
    2017#include "xmldecl.h"
    2118#include "namechars.h"
    22 #include "../lib/perflib/perfsec.h"
    23 
    2419#include "TagMatcher.hpp"
    2520#include "LineColTracker.hpp"
     
    2924
    3025#ifdef BUFFER_PROFILING
    31         BOM_Table * parser_timer;
    32 
     26    BOM_Table * parser_timer;
    3327#elif CODE_CLOCKER
    34         //#define NUM_EVENTS 1
    35         //int Events[NUM_EVENTS] = {PAPI_TOT_CYC};
    36         //int Events[NUM_EVENTS] = {PAPI_L2_DCM};
    37         #define NUM_EVENTS 2
    38         int Events[NUM_EVENTS] = {PAPI_TOT_CYC, PAPI_BR_MSP};
    39         int cal_size = 20;
    40         CC * parser_timer = new CC(Events,NUM_EVENTS,cal_size);
     28    //#define NUM_EVENTS 1
     29    //int Events[NUM_EVENTS] = {PAPI_TOT_CYC};
     30    //int Events[NUM_EVENTS] = {PAPI_L2_DCM};
     31    #define NUM_EVENTS 2
     32    int Events[NUM_EVENTS] = {PAPI_TOT_CYC, PAPI_BR_MSP};
     33    int cal_size = 20;
     34    CC * parser_timer = new CC(Events,NUM_EVENTS,cal_size);
    4135#else
    42         void * parser_timer;
     36    void * parser_timer;
    4337#endif
    4438
    45 int block_base=0;
    46 int buffer_base=0;
    47 char * source;
    48 
    49 LineColTracker tracker;
    50 TagMatcher matcher;
    5139ErrorTracker error_tracker;
    5240BitBlock EOF_mask = simd<1>::constant<1>();
    5341
    54 static inline int NameStrt_check(int pos);
    55 static inline int Name_check(int pos);
    56 static inline int PIName_check(int pos);
    57 static inline int CD_check(int pos);
    58 static inline int GenRef_check(int pos);
    59 static inline int HexRef_check(int pos);
    60 static inline int DecRef_check(int pos);
    61 static inline int AttRef_check(int pos);
    62 
     42//////////////////////////////////////////////////////////////////////////////////////////
     43// Buffer Management
     44//////////////////////////////////////////////////////////////////////////////////////////
     45#define PADDING_BLOCKS 1
     46#define PADDING_SIZE (BLOCK_SIZE * PADDING_BLOCKS )
     47#define SEGMENT_BLOCKS 12
     48#define SEGMENT_SIZE (BLOCK_SIZE * SEGMENT_BLOCKS)
     49#define BUFFER_SIZE PADDING_SIZE + SEGMENT_SIZE
     50
     51//////////////////////////////////////////////////////////////////////////////////////////
     52// @ global depends on 'error_tracker' and 'EOF_mask' definitions.
     53//////////////////////////////////////////////////////////////////////////////////////////
    6354@global
    6455
    65 static inline void s2p_do_block(BytePack U8[], Basis_bits & basis_bits);
    66 static inline void s2p_do_final_block(BytePack U8[], Basis_bits & basis_bits, BitBlock EOF_mask);
    67 static inline void postprocess_do_block(Lex & lex, CtCDPI_Callouts & ctCDPI_Callouts, Ref_Callouts & ref_Callouts, Check_streams & check_streams, int chars_avail);
    68 
    69 void do_process(FILE *infile, FILE *outfile);
    70 
    71 static inline void validate_block(BitBlockForwardIterator & start, int block_base, int is_valid(int));
    72 static inline void validate_block(BitBlockForwardIterator & start, int block_base, int is_valid(int,int));
     56//////////////////////////////////////////////////////////////////////////////////////////
     57// Headers that depend @ global stream struct types.
     58//////////////////////////////////////////////////////////////////////////////////////////
     59#include "../lib/transpose.hpp"
     60#include "post_process.hpp"
     61
     62static void do_process(FILE *infile, FILE *outfile);
    7363
    7464int main(int argc, char * argv[]) {
     
    117107}
    118108
    119 /* s2p Definitions */
    120 static inline void s2p_do_block(BytePack U8[], Basis_bits & basis_bits) {
    121   s2p(U8[0], U8[1], U8[2], U8[3], U8[4], U8[5], U8[6], U8[7],
    122         basis_bits.bit_0, basis_bits.bit_1, basis_bits.bit_2, basis_bits.bit_3, basis_bits.bit_4, basis_bits.bit_5, basis_bits.bit_6, basis_bits.bit_7);
    123 }
    124 
    125 static inline void s2p_do_final_block(BytePack U8[], Basis_bits & basis_bits, BitBlock EOF_mask) {
    126   s2p_do_block(U8, basis_bits);
    127   basis_bits.bit_0 = simd_and(basis_bits.bit_0, EOF_mask);
    128   basis_bits.bit_1 = simd_and(basis_bits.bit_1, EOF_mask);
    129   basis_bits.bit_2 = simd_and(basis_bits.bit_2, EOF_mask);
    130   basis_bits.bit_3 = simd_and(basis_bits.bit_3, EOF_mask);
    131   basis_bits.bit_4 = simd_and(basis_bits.bit_4, EOF_mask);
    132   basis_bits.bit_5 = simd_and(basis_bits.bit_5, EOF_mask);
    133   basis_bits.bit_6 = simd_and(basis_bits.bit_6, EOF_mask);
    134   basis_bits.bit_7 = simd_and(basis_bits.bit_7, EOF_mask);
    135 }
    136 
    137 
    138 static inline int NameStrt_check(int pos) {
    139         if(XML_10_UTF8_NameStrt_bytes((unsigned char*)&source[pos]) == 0){
    140               return XMLTestSuiteError::NAME_START;
    141         }
    142         return 0;
    143 }
    144 
    145 static inline int Name_check(int pos) {
    146         if(XML_10_UTF8_NameChar_bytes((unsigned char*)&source[pos]) == 0){
    147                   return XMLTestSuiteError::NAME;
    148         }
    149         return 0;
    150 }
    151 
    152 static inline int PIName_check(int pos, int file_pos) {
    153         if (at_XxMmLll<ASCII>((unsigned char*)&source[pos]) && (source[pos+3]=='?' || source[pos+3]<= ' ')) {
    154               // "<?xml" legal at start of file.
    155               if (!((file_pos == 2) && at_XmlDecl_start<ASCII>((unsigned char*)&source[0]))) {
    156                   return XMLTestSuiteError::XMLPINAME;
    157               }
    158         }
    159         return 0;
    160 }
    161 
    162 static inline int CD_check(int pos) {
    163         if (!at_CDATA1<ASCII>((unsigned char*)&source[pos])){
    164                   return XMLTestSuiteError::CDATA;
    165         }
    166         return 0;
    167 }
    168 
    169 static inline int GenRef_check(int pos) {
    170         unsigned char* s = (unsigned char*)&source[pos];
    171         if (!(at_Ref_gt<ASCII>(s)||at_Ref_lt<ASCII>(s)||at_Ref_amp<ASCII>(s)||at_Ref_quot<ASCII>(s)||at_Ref_apos<ASCII>(s))){
    172               return XMLTestSuiteError::UNDEFREF;
    173         }
    174         return 0;
    175 }
    176 
    177 static inline int HexRef_check(int pos) {
    178         unsigned char* s = (unsigned char*)&source[pos];
    179         int ch_val = 0;
    180         while(at_HexDigit<ASCII>(s)){
    181           ch_val = HexVal<ASCII>(s[0]) + (ch_val<<4);
    182           if (ch_val> 0x10FFFF ){
    183                 return XMLTestSuiteError::CHARREF;
    184           }
    185           s++;
    186         }
    187         if ((ch_val == 0x0) || ((ch_val | 0x7FF) == 0xDFFF)|| ((ch_val | 0x1) == 0xFFFF)){
    188           return XMLTestSuiteError::CHARREF;
    189         }
    190         else if (((ch_val < 0x20) && (ch_val != 0x9) && (ch_val != 0xD) && (ch_val != 0xA))){
    191           return XMLTestSuiteError::XML10CHARREF;
    192         }
    193         return 0;
    194 }
    195 
    196 static inline int DecRef_check(int pos) {
    197         unsigned char* s = (unsigned char*)&source[pos];
    198         int ch_val = 0;
    199         while(at_HexDigit<ASCII>(s)){
    200           ch_val = DigitVal<ASCII>(s[0]) + ch_val*10;
    201           if (ch_val> 0x10FFFF ){
    202                         return XMLTestSuiteError::CHARREF;
    203           }
    204           s++;
    205         }
    206         if ((ch_val == 0x0) || ((ch_val | 0x7FF) == 0xDFFF)|| ((ch_val | 0x1) == 0xFFFF)){
    207                   return XMLTestSuiteError::CHARREF;
    208         }
    209         else if (((ch_val < 0x20) && (ch_val != 0x9) && (ch_val != 0xD) && (ch_val != 0xA))){
    210                   return XMLTestSuiteError::XML10CHARREF;
    211         }
    212         return 0;
    213 }
    214 
    215 static inline int AttRef_check(int pos) {
    216         unsigned char* s = (unsigned char*)&source[pos];
    217         int ch_val = 0;
    218         if(s[0]=='#'){
    219           s++;
    220           if(s[0]=='x' || s[0]=='X'){
    221             s++;
    222             while(at_HexDigit<ASCII>(s)){
    223               ch_val = HexVal<ASCII>(s[0]) + (ch_val<<4);
    224               s++;
    225             }
    226           }
    227           else{
    228             while(at_HexDigit<ASCII>(s)){
    229               ch_val = DigitVal<ASCII>(s[0]) + ch_val*10;
    230               s++;
    231             }
    232           }
    233           if (ch_val==60){
    234             return XMLTestSuiteError::ATTREF;
    235           }
    236         }
    237         else if(at_Ref_lt<ASCII>(s)){
    238           return XMLTestSuiteError::ATTREF;
    239         }
    240         return 0;
    241 }
    242 
    243 static inline void validate_block(BitBlockForwardIterator & start, int block_base, int is_valid(int)) {
    244 
    245         int pos, block_pos;
    246         BitBlockForwardIterator end;
    247         while(start != end) {
    248 
    249                 block_pos = block_base + *start;
    250                 int rv = is_valid(block_pos);
    251 
    252                 if (rv) {
    253                         int error_line, error_column;
    254                         tracker.get_Line_and_Column(block_pos, error_line, error_column);
    255                         ReportError(XMLTestSuiteError::get_msg(rv), error_line, error_column);
    256                         exit(-1);
    257                 }
    258                 start++;
    259         }
    260 }
    261 
    262 static inline void validate_block(BitBlockForwardIterator & start, int block_base, int buffer_base, int is_valid(int,int)) {
    263 
    264         int pos, block_pos, file_pos;
    265         BitBlockForwardIterator end;
    266         while(start != end) {
    267 
    268                 block_pos = block_base + *start;
    269                 file_pos = block_pos+buffer_base;
    270 
    271 
    272                 int rv = is_valid(block_pos, file_pos);
    273 
    274                 if (rv) {
    275                         int error_line, error_column;
    276                         tracker.get_Line_and_Column(block_pos, error_line, error_column);
    277                         ReportError(XMLTestSuiteError::get_msg(rv), error_line, error_column);
    278                         exit(-1);
    279                 }
    280                 start++;
    281         }
    282 }
    283 
    284 static inline void postprocess_do_block(Lex & lex, CtCDPI_Callouts & ctCDPI_Callouts, Ref_Callouts & ref_Callouts, Check_streams & check_streams, int chars_avail){
    285 BitBlockForwardIterator iter;
    286 
    287 tracker.StoreNewlines(lex.LF);
    288 
    289 if (bitblock::any(simd_or(check_streams.non_ascii_name_starts, check_streams.non_ascii_names))) {
    290         iter.init(&check_streams.non_ascii_name_starts);
    291         validate_block(iter, block_base, NameStrt_check);
    292         iter.init(&check_streams.non_ascii_names);
    293         validate_block(iter, block_base, Name_check);
    294 }
    295 if (bitblock::any(ctCDPI_Callouts.PI_name_starts)){
    296         iter.init(&(ctCDPI_Callouts.PI_name_starts));
    297         validate_block(iter, block_base, buffer_base, PIName_check);
    298 }
    299 if (bitblock::any(ctCDPI_Callouts.CD_starts)){
    300         iter.init(&ctCDPI_Callouts.CD_starts);
    301         validate_block(iter, block_base, CD_check);
    302 }
    303 if(bitblock::any(ref_Callouts.GenRef_starts)){
    304         iter.init(&ref_Callouts.GenRef_starts);
    305         validate_block(iter, block_base, GenRef_check);
    306 }
    307 if(bitblock::any(ref_Callouts.DecRef_starts)){
    308         iter.init(&ref_Callouts.DecRef_starts);
    309         validate_block(iter, block_base, DecRef_check);
    310 }
    311 if(bitblock::any(ref_Callouts.HexRef_starts)){
    312         iter.init(&ref_Callouts.HexRef_starts);
    313         validate_block(iter, block_base, HexRef_check);
    314 }
    315 if(bitblock::any(check_streams.att_refs)){
    316         iter.init(&check_streams.att_refs);
    317         validate_block(iter, block_base, AttRef_check);
    318 }
    319 
    320 if(error_tracker.Has_Noted_Error()){
    321         int error_line, error_column;
    322         tracker.get_Line_and_Column(error_tracker.Noted_Pos_In_Block(), error_line, error_column);
    323         ReportError(error_tracker.Noted_Error_Msg(), error_line, error_column);
    324         exit(-1);
    325 }
    326 
    327 matcher.store_streams(check_streams.tag_marks, check_streams.name_follows, check_streams.misc_mask, chars_avail);
    328 tracker.AdvanceBlock();
    329 
    330 }
    331 
    332109void do_process(FILE *infile, FILE *outfile) {
    333110
    334 @decl
    335 
    336   int buf_pos = 0;
     111  @decl
     112
     113  LineColTracker tracker;
     114  TagMatcher<SEGMENT_SIZE,PADDING_SIZE> matcher;
     115
     116  char * source;
     117  int block_base=0;
     118  int buffer_base=0;
     119  int buffer_pos = 0;
    337120  int block_pos = 0;
    338121  int chars_avail = 0;
    339122  int check_pos = 0;
    340123  int chars_read = 0;
    341   BytePack buf[(BUFFER_SIZE+BLOCK_SIZE+OVERLAP_BUFSIZE*2)/sizeof(BitBlock)];
    342 
    343   char * srcbuf = ((char *) buf) + OVERLAP_BUFSIZE;
    344   buffer_base = buf_pos;
     124
     125  //////////////////////////////////////////////////////////////////////////////////////////
     126  // Buffer Management
     127  //////////////////////////////////////////////////////////////////////////////////////////
     128  BitBlock buf[(BUFFER_SIZE)/sizeof(BitBlock)];
     129
     130  char * srcbuf = ((char *) buf);
     131  buffer_base = buffer_pos;
    345132  source = srcbuf;
    346133
    347   chars_read = fread((void *)srcbuf, 1, BUFFER_SIZE + OVERLAP_BUFSIZE, infile);
     134  chars_read = fread((void *)srcbuf, 1, SEGMENT_SIZE, infile);
    348135  chars_avail = chars_read;
    349   if (chars_avail > BUFFER_SIZE) chars_avail = BUFFER_SIZE;
    350 
    351   matcher.setSrc(srcbuf);
     136  if (chars_avail > SEGMENT_SIZE) chars_avail = SEGMENT_SIZE;
    352137
    353138  if(chars_read<4){
     
    366151
    367152    if (e->code_unit_size != SingleByte || (e->has_encoding_decl && (!at_UTF_8(e->encoding)))){
    368         fprintf(stderr,"Sorry, this xmlwf demo only works for UTF-8.\n");
    369         exit(-1);
     153        fprintf(stderr,"Sorry, this xmlwf demo only works for UTF-8.\n");
     154        exit(-1);
    370155    }
    371156  }
    372157  else {
    373     fprintf(stderr,"Sorry, this xmlwf demo does not process EBCDIC.\n");
    374         exit(-1);
     158        fprintf(stderr,"Sorry, this xmlwf demo does not process EBCDIC.\n");
     159        exit(-1);
    375160  }
    376161
    377162  if (e->content_start != 0) {
    378163        memmove(&srcbuf[0], &srcbuf[e->content_start], chars_read - e->content_start);
    379         buf_pos = e->content_start;
    380         if (chars_avail == BUFFER_SIZE) {
    381                 chars_read = chars_read - e->content_start +
    382                              fread(&srcbuf[chars_read-e->content_start], 1, e->content_start, infile);
     164    buffer_pos = e->content_start;
     165    if (chars_avail == SEGMENT_SIZE) {
     166        chars_read = chars_read - e->content_start + fread(&srcbuf[chars_read-e->content_start], 1, e->content_start, infile);
    383167                chars_avail = chars_read;
    384                 if (chars_avail > BUFFER_SIZE) chars_avail = BUFFER_SIZE;
     168        if (chars_avail > SEGMENT_SIZE) chars_avail = SEGMENT_SIZE;
    385169        }
    386170        else {
     
    390174  }
    391175
     176  matcher.setSrc(srcbuf);
     177
    392178@stream_stmts
    393179
    394 /* Full Buffers */
    395 
    396     while (chars_avail == BUFFER_SIZE) {
     180    /* Full Segments */
     181    while (chars_avail == SEGMENT_SIZE) {
    397182      PERF_SEC_START(parser_timer);
    398183      for (int blk = 0; blk < SEGMENT_BLOCKS; blk++) {
     
    400185          s2p_do_block((BytePack *) &srcbuf[block_base], basis_bits);
    401186          @block_stmts
    402           postprocess_do_block(lex, ctCDPI_Callouts, ref_Callouts, check_streams, chars_avail);
     187          tracker.StoreNewlines(lex.LF);
     188          postprocess_do_block(lex, ctCDPI_Callouts, ref_Callouts, check_streams, source, buffer_base, block_base, chars_avail, tracker);
     189          matcher.store_streams(check_streams.tag_marks, check_streams.name_follows, check_streams.misc_mask, chars_avail);
     190          tracker.AdvanceBlock();
    403191      }
    404192      matcher.StreamScan(chars_avail);
     
    407195
    408196      int bytes_left = chars_read - chars_avail;
    409       memmove(srcbuf, &srcbuf[BUFFER_SIZE], bytes_left);
    410       chars_read = fread(&srcbuf[bytes_left],1, BUFFER_SIZE + OVERLAP_BUFSIZE - bytes_left, infile) + bytes_left;
     197      memmove(srcbuf, &srcbuf[SEGMENT_SIZE], bytes_left);
     198      chars_read = fread(&srcbuf[bytes_left], 1, SEGMENT_SIZE - bytes_left, infile) + bytes_left;
    411199      chars_avail = chars_read;
    412       if (chars_avail > BUFFER_SIZE) chars_avail = BUFFER_SIZE;
    413       buf_pos += chars_avail;
    414       buffer_base = buf_pos;
    415     }
    416 /* Final Partial Buffer */
     200      if (chars_avail > SEGMENT_SIZE) chars_avail = SEGMENT_SIZE;
     201      buffer_pos += chars_avail;
     202      buffer_base = buffer_pos;
     203    }
     204
     205    /* Final Partial Segment */
    417206    PERF_SEC_START(parser_timer);
    418207
    419208    block_pos = 0;
    420209    int remaining = chars_avail;
    421 /* Full Blocks */
     210
     211    /* Full Blocks */
    422212    while (remaining >= BLOCK_SIZE) {
    423213          block_base = block_pos;
    424214          s2p_do_block((BytePack *) &srcbuf[block_pos], basis_bits);
    425215          @block_stmts
    426           postprocess_do_block(lex, ctCDPI_Callouts, ref_Callouts, check_streams, chars_avail);
    427           block_pos += BLOCK_SIZE;
     216          tracker.StoreNewlines(lex.LF);
     217          postprocess_do_block(lex, ctCDPI_Callouts, ref_Callouts, check_streams, source, buffer_base, block_base, chars_avail, tracker);
     218          matcher.store_streams(check_streams.tag_marks, check_streams.name_follows, check_streams.misc_mask, chars_avail);
     219          tracker.AdvanceBlock();
     220          block_pos += BLOCK_SIZE;
    428221          remaining -= BLOCK_SIZE;
    429222    }
    430223    block_base = block_pos;
     224
     225    /* Partial Block or Any Carry */
    431226    if (remaining > 0 || @any_carry) {
    432227          EOF_mask = bitblock::srl(simd<1>::constant<1>(), convert(BLOCK_SIZE-remaining));
    433228          s2p_do_final_block((BytePack *) &srcbuf[block_pos], basis_bits, EOF_mask);
    434229          @final_block_stmts
    435           postprocess_do_block(lex, ctCDPI_Callouts, ref_Callouts, check_streams, chars_avail);
    436     }
    437     buf_pos += chars_avail;
    438     buffer_base = buf_pos;
     230          tracker.StoreNewlines(lex.LF);
     231          postprocess_do_block(lex, ctCDPI_Callouts, ref_Callouts, check_streams, source, buffer_base, block_base, chars_avail, tracker);
     232          matcher.store_streams(check_streams.tag_marks, check_streams.name_follows, check_streams.misc_mask, chars_avail);
     233          tracker.AdvanceBlock();
     234
     235    }
     236
     237    buffer_pos += chars_avail;
     238    buffer_base = buffer_pos;
    439239
    440240    matcher.StreamScan(chars_avail);
Note: See TracChangeset for help on using the changeset viewer.