-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmerge.cpp
More file actions
1061 lines (752 loc) · 29.8 KB
/
merge.cpp
File metadata and controls
1061 lines (752 loc) · 29.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
merge.cpp : takes source (.m) file, definitions (.md) file as input and
outputs linked (.ml) file
program constructed from source file is called primary
program constructed from import files (which the source file imports) are called secondary
*/
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sys/stat.h>
#include <iostream>
#include <boost/static_assert.hpp>
#include <string>
#include <stdint.h>
#include<assert.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include "linker.hpp"
#include "vm/program.hpp"
#include "db/tuple.hpp"
#include "vm/instr.hpp"
#include "db/database.hpp"
#include "utils/types.hpp"
#include "vm/state.hpp"
#include "version.hpp"
// reads bytes from primary (source) file
#define READ_CODE(TO, SIZE) do { \
infile_m.read((char *)(TO), SIZE); \
position += (SIZE); \
} while(false)
// reads byte from secondary (import) files
#define READ_CODE_TMP(TO, SIZE) do { \
temp_in.read((char *)(TO), SIZE); \
} while(false)
// seeks byte position in primary file
#define SEEK_CODE(SIZE) do { \
infile_m.seekg(SIZE, ios_base::cur); \
position += (SIZE); \
} while(false)
// writes byte code to output (.ml) file
#define WRITE_CODE(FROM, SIZE) do { \
outfile_ml.write((char *)(FROM), SIZE); \
position_wr += (SIZE); \
} while(false)
// copies from primary to output file
#define COPY_TO_OUTPUT(FROM,TO) do { \
infile_m.seekg(FROM); \
buffer = new char[TO - FROM]; \
infile_m.read(buffer,(TO - FROM)); \
infile_m.seekg(TO); \
WRITE_CODE(buffer,(TO - FROM)); \
} while(false)
#define VERSION_AT_LEAST(MAJ, MIN) (maj > (MAJ) || (maj == (MAJ) && min >= (MIN)))
// predicate descriptor size
#define PRED_DESC_SZ 104
using namespace vm;
using namespace process;
using namespace std;
using namespace db;
using namespace vm::instr;
using namespace process;
using namespace utils;
static char *bytefile1;
static char *m_file;
static char *md_file;
static char *ml_file;
static bool print = false;
static program* primary;
static program* secondary;
//list of import files
std::vector<string> meld_file;
//list of imported programs
std::vector<vm::program*> secondary_list;
std::ostream cout_null(0);
code_size_t const_code_size;
std::string
field_type_string_(field_type type)
{
switch(type) {
case FIELD_INT: return "int";
case FIELD_FLOAT: return "float";
case FIELD_NODE: return "node";
case FIELD_LIST_INT: return "int list";
case FIELD_LIST_FLOAT: return "float list";
case FIELD_LIST_NODE: return "node list";
case FIELD_STRING: return "string";
default:
return "Unrecognized field type";
}
assert(false);
return "";
}
static void help()
{
cerr << "merge: execute linker program" << endl;
cerr << "./merge -fm <.m input file> -fd <.md input file> -fl <.ml output file>" << endl;
exit(EXIT_SUCCESS);
}
static void read_args(int argc, char **argv)
{
argv++;
--argc;
while (argc > 0 && (argv[0][0] == '-')) {
switch(argv[0][1]) {
case 'f' :{
switch(argv[0][2]){
case 'm' : bytefile1 = argv[1];
m_file = argv[1];
break;
case 'd' : md_file = argv[1];
break;
case 'l' : ml_file = argv[1];
break;
default : break;
}
argc--;
argv++;
}
break;
case 'c': {
cout<<"printing code : \n";
print = true;
argc--;
argv++;
}
break;
default:
help();
}
/* advance */
argc--; argv++;
}
}
// table lookup
char* get_filename(char *func_name,struct func_table* table,int size){
int i;
for(i = 0; i < size;i++){
if(!strcmp(func_name,table[i].func_name))
return table[i].file_name;
}
return NULL;
}
//modify predicate and rule code for given program
void
modify_code(vm::program* prog){
// allowing changes to bytecode
prog->set_modify(true);
cout << "Modifying code for... " << prog->get_name() << endl;
// modify rule code
for(size_t i(0); i < prog->num_rules();i++){
cout<<"rule : "<<i<<endl;
cout<<prog->get_rule(i)->get_string()<<endl;
prog->get_rule(i)->print(cout_null,prog);
}
//modify predicate code
for(size_t i(0) ; i < prog->num_predicates(); i++){
cout<<"predicate : "<<i<<endl;
cout<< prog->get_predicate(i)->get_name()<<endl;
prog->print_predicate_code(cout_null,prog->get_predicate(i));
}
// disallow changes to bytecode
prog->set_modify(false);
}
// check if primary predicate is imported
bool
is_imported(size_t id){
string name = primary->get_predicate(id)->get_name();
for(size_t i(0); i < primary->num_imported_predicates(); i++){
if(!name.compare(primary->get_imported_predicate(i)->get_as()))
return true;
}
return false;
}
// get modified id of primary imported predicate
predicate_id
get_imported_predicate_id(predicate_id id){
string name = primary->get_predicate(id)->get_name();
string name_;
bool import = false;
for(size_t i(0); i < primary->num_imported_predicates(); i++){
if(!name.compare(primary->get_imported_predicate(i)->get_as())){
import = true;
name_ = primary->get_imported_predicate(i)->get_imp();
}
}
if(import){
for(size_t j(0); j < secondary_list.size(); j++){
vm::predicate* pred;
if((pred = secondary_list[j]->get_predicate_by_name(name_)) != NULL)
return pred->get_linker_id();
}
}
return id;
}
// main linker function
void linker(){
// .m file
std::ifstream infile_m(m_file,std::ifstream::binary);
// .md file
std::ifstream infile_md;
infile_md.open(md_file);
// .ml file
std::ofstream outfile_ml(ml_file,std::ofstream::binary);
infile_m.seekg(0,infile_m.end);
long size_tot = infile_m.tellg();
infile_m.seekg(0);
cout <<"total file size : "<<size_tot<<" bytes"<<endl;
position = 0;
position_wr = 0;
position_prev = 0;
cout << "merging .m files......"<<endl;
if(!infile_md.is_open()){
cout<<"Error : cannot open *.md file\n";
return;
}
// process definitions file
// read num of lines in .md file , equal to num of functions defined
num_of_lines = count(istreambuf_iterator<char>(infile_md),
istreambuf_iterator<char>(),'\n');
cout << "num of external functions : "<<num_of_lines<<endl;
// create table of func_names and file_names
struct func_table table[num_of_lines];
// reset file pointer
infile_md.seekg(0);
int n = 0;
// read external function info
while(n < num_of_lines){
char buf[MAX_CHARS_PER_LINE];
infile_md.getline(buf,MAX_CHARS_PER_LINE);
const char* token[MAX_TOKENS_PER_LINE] = {};
token[0] = strtok(buf,DELIMITER);
if(token[0]){
//copy func name
memcpy(table[n].func_name,token[0],strlen(token[0])+1);
//ignore argument
token[1] = strtok(0,DELIMITER);
// copy file path
token[2] = strtok(0,DELIMITER);
memcpy(table[n].file_name,token[2],strlen(token[2])+1);
cout << "func_name : "<<table[n].func_name<<endl;
cout << "func_arg : "<<token[1]<<endl;
cout << "file_name : "<<table[n].file_name<<endl;
}
n++;
}
if(!infile_m.is_open()){
cout<<"Error : cannot open *.m file\n";
}
//Done processing definitions file
// read magic number
uint32_t magic1, magic2;
READ_CODE(&magic1, sizeof(magic1));
READ_CODE(&magic2, sizeof(magic2));
// read version
uint32_t maj,min;
READ_CODE(&maj, sizeof(uint32_t));
READ_CODE(&min, sizeof(uint32_t));
COPY_TO_OUTPUT(position_prev,position);
// read number of predicates
utils::byte buf[PRED_DESC_SZ];
READ_CODE(buf, sizeof(byte));
const size_t num_predicates_orig = (size_t)buf[0];
size_t num_imported_predicates = 0;
size_t num_rules_new = 0;
size_t num_common_predicates;
// count predicates,rules from import file
for(uint32_t i(0); i < secondary_list.size(); i++){
num_imported_predicates = num_imported_predicates + secondary_list[i]->num_predicates();
// ignoring init rule from import secondary files
num_rules_new = num_rules_new + secondary_list[i]->num_rules() - 1;
}
//init,set_priority,setcolor,.....setcolor2 are 8 common predicates in every
//.m file
num_common_predicates = (COMMON_PREDICATES) * secondary_list.size();
const size_t num_predicates_new = num_predicates_orig + num_imported_predicates - num_common_predicates - primary->num_imported_predicates();
buf[0] = (byte)(num_predicates_new);
WRITE_CODE(buf,sizeof(byte));
cout << "Original predicates : " << num_predicates_orig << endl;
cout << "New predicates : " << num_predicates_new << endl;
// reset to last byte read
position_prev = position;
// num nodes
uint_val num_nodes;
READ_CODE(&num_nodes, sizeof(uint_val));
// get file with max nodes
int max = num_nodes;
int index = -1;
for(size_t i(0); i < secondary_list.size(); i++){
if(secondary_list[i]->get_num_nodes() > max){
max = secondary_list[i]->get_num_nodes();
index = i;
}
}
if(index != -1){
size_t num_nodes_new = secondary_list[index]->get_num_nodes();
WRITE_CODE(&num_nodes_new,sizeof(uint_val));
uint_val new_size = num_nodes_new * database::node_size;
WRITE_CODE(secondary_list[index]->get_buffer(),new_size);
}
else{
size_t num_nodes_new = primary->get_num_nodes();
WRITE_CODE(&num_nodes_new,sizeof(uint_val));
uint_val new_size = num_nodes_new * database::node_size;
WRITE_CODE(primary->get_buffer(),new_size);
}
// skip database info in primary file
SEEK_CODE(num_nodes * database::node_size);
// reset to last byte read
position_prev = position;
uint32_t number_imported_predicates;
// read imported/exported predicates
if(VERSION_AT_LEAST(0, 9)) {
READ_CODE(&number_imported_predicates,
sizeof(number_imported_predicates));
for(uint32_t i(0); i < number_imported_predicates; ++i) {
uint32_t size;
READ_CODE(&size, sizeof(size));
char buf_imp[size + 1];
READ_CODE(buf_imp, size);
buf_imp[size] = '\0';
READ_CODE(&size, sizeof(size));
char buf_as[size + 1];
READ_CODE(buf_as, size);
buf_as[size] = '\0';
READ_CODE(&size, sizeof(size));
char buf_file[size + 1];
READ_CODE(buf_file, size);
buf_file[size] = '\0';
cout << "import " << buf_imp << " as " << buf_as << " from " <<
buf_file << endl;
}
uint32_t number_exported_predicates;
READ_CODE(&number_exported_predicates,
sizeof(number_exported_predicates));
for(uint32_t i(0); i < number_exported_predicates; ++i) {
uint32_t str_size;
READ_CODE(&str_size, sizeof(str_size));
char buf[str_size + 1];
READ_CODE(buf, str_size);
buf[str_size] = '\0';
cout << "export " << buf << endl;
}
}
// get number of args needed
byte n_args;
READ_CODE(&n_args, sizeof(byte));
// copy from position_prev to position
COPY_TO_OUTPUT(position_prev,position);
position_prev = position;
// get rule information
uint_val n_rules_orig,n_rules_new;
READ_CODE(&n_rules_orig, sizeof(uint_val));
cout << endl << " Adding Rules ..." << endl;
// add rules info from import file, include init from imported file
n_rules_new = n_rules_orig + num_rules_new;
WRITE_CODE(&n_rules_new, sizeof(uint_val));
cout << "No of source rules : " << n_rules_orig << endl;
cout << "No of import rules : " << num_rules_new << endl;
position_prev = position;
//add rules from primary and secondary files
size_t rule_offset = 0;
// add primary rules
for(size_t i(0); i < n_rules_orig; ++i) {
// read rule string length
uint_val rule_len;
READ_CODE(&rule_len, sizeof(uint_val));
assert(rule_len > 0);
char str[rule_len + 1];
READ_CODE(str, sizeof(char) * rule_len);
str[rule_len] = '\0';
primary->get_rule(i)->set_linker_id(rule_offset);
rule_offset++;
}
COPY_TO_OUTPUT(position_prev,position);
position_prev = position;
// add remaining rules from secondary programs, skipping init
for(size_t j(0); j < secondary_list.size();j++){
for(size_t i(1); i < secondary_list[j]->num_rules(); ++i) {
// read rule string length
uint_val rule_len;
rule_len = strlen(secondary_list[j]->get_rule(i)->get_string().c_str());
WRITE_CODE(&rule_len, sizeof(uint_val));
assert(rule_len > 0);
// get string and append
const char *str = secondary_list[j]->get_rule(i)->get_string().c_str();
WRITE_CODE(str,rule_len);
secondary_list[j]->get_rule(i)->set_linker_id(rule_offset);
rule_offset++;
}
}
// read string constants
int_val num_strings;
READ_CODE(&num_strings, sizeof(int_val));
for(int i(0); i < num_strings; ++i) {
int_val length;
READ_CODE(&length, sizeof(int_val));
char str[length + 1];
READ_CODE(str, sizeof(char) * length);
str[length] = '\0';
}
// read constants code
uint_val num_constants;
READ_CODE(&num_constants, sizeof(uint_val));
for(uint_val i(0); i < num_constants; ++i) {
byte b;
READ_CODE(&b, sizeof(byte));
}
// read constants code
READ_CODE(&const_code_size, sizeof(code_size_t));
byte_code const_code = new byte_code_el[const_code_size];
READ_CODE(const_code, const_code_size);
delete const_code;
COPY_TO_OUTPUT(position_prev,position);
position_prev = position;
// process functions
if(VERSION_AT_LEAST(0, 6)) {
// get function code
uint_val n_functions,n_functions_new;
READ_CODE(&n_functions, sizeof(uint_val));
position_prev = position;
n_functions_new = 0;
for(size_t i(0); i < secondary_list.size(); i++){
n_functions_new = n_functions_new + secondary_list[i]->get_num_functions();
}
cout << "n_functions : " << n_functions << endl;
cout << "n_functions_new : " << n_functions_new << endl;
uint_val n_functions_total = n_functions + n_functions_new;
WRITE_CODE(&n_functions_total,sizeof(uint_val));
for(size_t i(0); i < n_functions; ++i) {
code_size_t fun_size;
READ_CODE(&fun_size, sizeof(code_size_t));
byte_code fun_code(new byte_code_el[fun_size]);
READ_CODE(fun_code, fun_size);
delete fun_code;
}
COPY_TO_OUTPUT(position_prev,position);
position_prev = position;
//add secondary file functions
for(size_t i(0); i < secondary_list.size(); i++){
for(size_t j(0); j < secondary_list[i]->get_num_functions(); j++){
code_size_t fun_size = secondary_list[i]->get_function(j)->get_bytecode_size();
WRITE_CODE(&fun_size, sizeof(code_size_t));
WRITE_CODE(secondary_list[i]->get_function(j)->get_bytecode(),fun_size);
}
}
if(maj > 0 || min >= 7) {
// get external functions definitions
uint_val n_externs,n_externs_new;
READ_CODE(&n_externs, sizeof(uint_val));
position_prev = position;
n_externs_new = 0;
for(size_t i(0); i < secondary_list.size(); i++){
n_externs_new = n_externs_new + secondary_list[i]->get_num_ext_functions();
}
cout << "n_externs : " << n_externs << endl;
cout << "n_externs_new : " << n_externs_new << endl;
uint_val n_externs_total = n_externs + n_externs_new;
WRITE_CODE(&n_externs_total, sizeof(uint_val));
// primary
for(size_t i(0); i < n_externs; ++i) {
uint_val extern_id;
READ_CODE(&extern_id, sizeof(uint_val));
char extern_name[256];
READ_CODE(extern_name, sizeof(extern_name));
COPY_TO_OUTPUT(position_prev,position);
char skip_filename[1024];
char *file_name = get_filename(extern_name,table,num_of_lines);
READ_CODE(skip_filename, sizeof(skip_filename));
cout<<"skip_filename : "<<skip_filename<<endl;
if(file_name){
cout<<"external function : "<<extern_name<<" found !\n";
cout << "filename : "<<file_name<<endl;
memcpy(skip_filename,file_name,strlen(file_name));
}
else{
cout<<"external function : "<<extern_name<<" not defined in .md file\n";
memcpy(skip_filename,dummy_path,sizeof(dummy_path));
}
WRITE_CODE(skip_filename,sizeof(skip_filename));
ptr_val skip_ptr;
READ_CODE(&skip_ptr, sizeof(skip_ptr));
WRITE_CODE(&skip_ptr,sizeof(skip_ptr));
uint_val num_args;
READ_CODE(&num_args, sizeof(num_args));
WRITE_CODE(&num_args,sizeof(num_args));
cout << "Id : " << extern_id << " Name :" << extern_name << endl;
cout<<"Num args : "<<num_args<<endl;
for(uint_val j(0); j != num_args + 1; ++j) {
byte b;
READ_CODE(&b, sizeof(byte));
WRITE_CODE(&b,sizeof(byte));
field_type type = (field_type)b;
cout << field_type_string_(type) << ", ";
}
cout << endl;
position_prev = position;
}
//secondary
size_t new_extern_id = n_externs;
for(size_t k(0) ; k < secondary_list.size() ; k++){
cout << "linking external functions" << endl;
cout << "Opening ..." << secondary_list[k]->get_name().c_str() << endl;
//open secondary program
std::ifstream temp_in(secondary_list[k]->get_name().c_str(),std::ifstream::binary);
//seek position to external function definition start
temp_in.seekg(secondary_list[k]->get_ext_function_position(), ios_base::beg);
for(size_t i(0); i < secondary_list[k]->get_num_ext_functions(); ++i) {
uint_val extern_id;
READ_CODE_TMP(&extern_id, sizeof(uint_val));
cout << "old external id : " << extern_id << endl;
cout << "new external id : " << new_extern_id << endl;
WRITE_CODE(&new_extern_id,sizeof(uint_val));
char extern_name[256];
READ_CODE_TMP(extern_name, sizeof(extern_name));
WRITE_CODE(extern_name, sizeof(extern_name));
char skip_filename[1024];
char *file_name = get_filename(extern_name,table,num_of_lines);
READ_CODE_TMP(skip_filename, sizeof(skip_filename));
cout<<"skip_filename : "<<skip_filename<<endl;
if(file_name){
cout<<"external function : "<<extern_name<<" found !\n";
cout << "filename : "<<file_name<<endl;
memcpy(skip_filename,file_name,strlen(file_name));
}
else{
cout<<"external function : "<<extern_name<<" not defined in .md file\n";
memcpy(skip_filename,dummy_path,sizeof(dummy_path));
}
WRITE_CODE(skip_filename,sizeof(skip_filename));
ptr_val skip_ptr;
READ_CODE_TMP(&skip_ptr, sizeof(skip_ptr));
WRITE_CODE(&skip_ptr,sizeof(skip_ptr));
uint_val num_args;
READ_CODE_TMP(&num_args, sizeof(num_args));
WRITE_CODE(&num_args,sizeof(num_args));
cout << "Id : " << new_extern_id << " Name :" << extern_name << endl;
cout<<"Num args : "<<num_args<<endl;
new_extern_id++;
for(uint_val j(0); j != num_args + 1; ++j) {
byte b;
READ_CODE_TMP(&b, sizeof(byte));
WRITE_CODE(&b,sizeof(byte));
field_type type = (field_type)b;
cout << field_type_string_(type) << ", ";
}
cout << endl;
}
//close secondary program file
temp_in.close();
}
}
}
COPY_TO_OUTPUT(position_prev,position);
position_prev = position;
//process predicate buffer
size_t offset = 0;
// read source predicate information, avoid imported predicates
for(size_t i(0); i < num_predicates_orig; ++i) {
READ_CODE(buf, PRED_DESC_SZ);
if(!is_imported(i)){
COPY_TO_OUTPUT(position_prev,position);
primary->get_predicate(i)->set_linker_id(offset);
offset++;
}
else
primary->get_predicate(i)->set_linker_id(DEFAULT_ID);
position_prev = position;
}
// offset used to number secondary predicates
assert(offset == (primary->num_predicates()-primary->num_imported_predicates()));
// add import predicate information
for(size_t j(0); j < secondary_list.size();j++){
//ignore common predicates
for(size_t i(COMMON_PREDICATES) ; i < secondary_list[j]->num_predicates() ; i++) {
string filename = secondary_list[j]->get_name();
// rename predicate, write buffer
secondary_list[j]->get_predicate(i)->rename(filename);
WRITE_CODE(secondary_list[j]->get_predicate(i)->get_desc_buffer(),PRED_DESC_SZ);
//link predicate id
secondary_list[j]->get_predicate(i)->set_linker_id(offset);
offset++;
}
}
//link primary predicates which are imported
for(size_t i(0); i < num_predicates_orig; ++i) {
if(is_imported(i)){
size_t id = get_imported_predicate_id(i);
primary->get_predicate(i)->set_linker_id(id);
}
}
// modify rule and predicate code for primary and secondary programs
modify_code(primary);
for(size_t i(0); i < secondary_list.size(); i++){
modify_code(secondary_list[i]);
}
// get global priority information
byte global_info;
READ_CODE(&global_info, sizeof(byte));
switch(global_info) {
case 0x01: { // priority by predicate
cerr << "Not supported anymore" << endl;
assert(false);
}
break;
case 0x02: { // normal priority
byte type(0x0);
byte asc_desc;
//dummy hold values
field_type priority_type;
int32_t int_value;
double float_value;
READ_CODE(&type, sizeof(byte));
switch(type) {
case 0x01: priority_type = FIELD_FLOAT; break;
case 0x02: priority_type = FIELD_INT; break;
default: assert(false);
}
READ_CODE(&asc_desc, sizeof(byte));
if(priority_type == FIELD_FLOAT)
READ_CODE(&float_value, sizeof(float_val));
else
READ_CODE(&int_value, sizeof(int_val));
}
break;
case 0x03: { // data file
}
break;
default:
break;
}
COPY_TO_OUTPUT(position_prev,position);
position_prev = position;
//process predicate code
// read predicate code, avoid imported predicates
for(size_t i(0); i < primary->num_predicates(); ++i) {
const size_t size = primary->get_predicate_bytecode_size(i);
byte_code code = new byte_code_el[size];
READ_CODE(code, size);
if(!is_imported(i))
WRITE_CODE(primary->get_predicate_bytecode(i),size);
position_prev = position;
delete code;
}
// write import predicate code
for(size_t j(0); j < secondary_list.size();j++){
for(size_t i(COMMON_PREDICATES); i < secondary_list[0]->num_predicates(); i++){
const size_t size = secondary_list[j]->get_predicate_bytecode_size(i);
WRITE_CODE(secondary_list[j]->get_predicate_bytecode(i),size);
}
}
// process rules code
uint_val num_rules_code_orig,num_rules_code_new;
READ_CODE(&num_rules_code_orig, sizeof(uint_val));
num_rules_code_new = num_rules_code_orig + num_rules_new;
WRITE_CODE(&num_rules_code_new,sizeof(uint_val));
position_prev = position;
// primary rules
for(size_t i(0); i < primary->num_rules(); ++i) {
code_size_t code_size;
byte_code code;
code_size = primary->get_rule(i)->get_codesize();
code = primary->get_rule(i)->get_bytecode();
WRITE_CODE(&code_size, sizeof(code_size_t));
WRITE_CODE(code, code_size);
byte is_persistent = primary->get_rule(i)->as_persistent();
WRITE_CODE(&is_persistent, sizeof(byte));
uint_val num_preds = primary->get_rule(i)->num_predicates();
WRITE_CODE(&num_preds, sizeof(uint_val));
assert(num_preds < 10);
for(size_t j(0); j < num_preds; ++j) {
predicate_id id = primary->get_rule(i)->get_predicate_number(j)->get_linker_id();
WRITE_CODE(&id, sizeof(predicate_id));
}
}
// secondary rules
for(size_t k(0); k < secondary_list.size();k++){
for(size_t i(1); i < secondary_list[k]->num_rules(); ++i) {
code_size_t code_size;
byte_code code;
code_size = secondary_list[k]->get_rule(i)->get_codesize();
code = secondary_list[k]->get_rule(i)->get_bytecode();
WRITE_CODE(&code_size, sizeof(code_size_t));
WRITE_CODE(code, code_size);
byte is_persistent = secondary_list[k]->get_rule(i)->as_persistent();
WRITE_CODE(&is_persistent, sizeof(byte));
uint_val num_preds = secondary_list[k]->get_rule(i)->num_predicates();
WRITE_CODE(&num_preds, sizeof(uint_val));
assert(num_preds < 10);
for(size_t j(0); j < num_preds; ++j) {
predicate_id id = secondary_list[k]->get_rule(i)->get_predicate_number(j)->get_linker_id();
WRITE_CODE(&id, sizeof(predicate_id));
}
}
}
delete[] buffer;
infile_m.close();
infile_md.close();
outfile_ml.close();
}
//checks if import is valid
bool
checkExport(string predicate){
for(size_t i(0); i < secondary->num_exported_predicates();i++){
if(!predicate.compare(secondary->get_exported_predicate(i))){
cout << "import " << predicate << " valid\n";
return true;
}
}
return false;
}
int