-
Notifications
You must be signed in to change notification settings - Fork 1
/
exomizer.h
4999 lines (4302 loc) · 134 KB
/
exomizer.h
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
/*
Warning! This is a modified version of original sources!
To sum up:
- all include files and C sources were merged in a single file
- existing logs were removed (except error logs)
- main were removed and wrapper added
*/
#ifndef ALREADY_INCLUDED_CALLBACK
#define ALREADY_INCLUDED_CALLBACK
/*
* Copyright (c) 2005 Magnus Lind.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software, alter it and re-
* distribute it freely for any non-commercial, non-profit purpose subject to
* the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any distribution.
*
* 4. The names of this software and/or it's copyright holders may not be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
*/
typedef int cb_cmp(const void *a, const void *b);
typedef void cb_free(void *a);
typedef void cb_fprint(FILE *f, const void *a);
#endif
#ifndef INCLUDED_INT
#define INCLUDED_INT
/*
* Copyright (c) 2005 Magnus Lind.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software, alter it and re-
* distribute it freely for any non-commercial, non-profit purpose subject to
* the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any distribution.
*
* 4. The names of this software and/or it's copyright holders may not be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
*/
typedef signed char i8;
typedef signed short int i16;
typedef signed int i32;
typedef unsigned char u8;
typedef unsigned short int u16;
typedef unsigned int u32;
#endif
#ifndef ALREADY_INCLUDED_PROGRESS
#define ALREADY_INCLUDED_PROGRESS
/*
* Copyright (c) 2005 Magnus Lind.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software, alter it and re-
* distribute it freely for any non-commercial, non-profit purpose subject to
* the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any distribution.
*
* 4. The names of this software and/or it's copyright holders may not be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
*/
struct progress
{
char *msg;
float factor;
int offset;
int last;
};
void progress_init(struct progress p[1], char *msg, int start, int end);
void progress_bump(struct progress p[1], int pos);
void progress_free(struct progress p[1]);
#endif
#ifndef ALREADY_INCLUDED_CHUNKPOOL
#define ALREADY_INCLUDED_CHUNKPOOL
/*
* Copyright (c) 2003 -2005 Magnus Lind.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software, alter it and re-
* distribute it freely for any non-commercial, non-profit purpose subject to
* the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any distribution.
*
* 4. The names of this software and/or it's copyright holders may not be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
*/
#define CHUNKPOOL_CHUNKS_MAX 64
struct chunkpool {
int chunk_size;
int chunk;
int chunk_pos;
int chunk_max;
void *chunks[64];
};
void
chunkpool_init(struct chunkpool *ctx, int size);
void
chunkpool_free(struct chunkpool *ctx);
void chunkpool_free2(struct chunkpool *ctx, cb_free *f);
void *
chunkpool_malloc(struct chunkpool *ctx);
void *
chunkpool_calloc(struct chunkpool *ctx);
#endif
#ifndef ALREADY_INCLUDED_MATCH
#define ALREADY_INCLUDED_MATCH
/*
* Copyright (c) 2002 - 2005, 2013 Magnus Lind.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software, alter it and re-
* distribute it freely for any non-commercial, non-profit purpose subject to
* the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any distribution.
*
* 4. The names of this software and/or it's copyright holders may not be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
*/
struct match {
unsigned short int offset;
unsigned short int len;
struct match *next;
};
typedef struct match match[1];
typedef struct match *matchp;
typedef const struct match *const_matchp;
struct pre_calc {
struct match_node *single;
const struct match *cache;
};
struct match_ctx {
struct chunkpool m_pool[1];
struct pre_calc (*info)[1];
unsigned short int *rle;
unsigned short int *rle_r;
const unsigned char *buf;
int len;
int max_offset;
int max_len;
};
typedef struct match_ctx match_ctx[1];
typedef struct match_ctx *match_ctxp;
//void match_ctx_init(match_ctx ctx, /* IN/OUT */ struct membuf *inbuf, /* IN */ int max_len, /* IN */ int max_offset, /* IN */ int use_imprecise_rle); /* IN */
void match_ctx_free(match_ctx ctx); /* IN/OUT */
/* this needs to be called with the indexes in
* reverse order */
const_matchp matches_get(match_ctx ctx, /* IN/OUT */
int index); /* IN */
void match_delete(match_ctx ctx, /* IN/OUT */
matchp mp); /* IN */
struct matchp_cache_enum {
match_ctxp ctx;
const_matchp next;
match tmp1;
match tmp2;
int pos;
};
typedef struct matchp_cache_enum matchp_cache_enum[1];
typedef struct matchp_cache_enum *matchp_cache_enump;
void matchp_cache_get_enum(match_ctx ctx, /* IN */
matchp_cache_enum mpce); /* IN/OUT */
typedef const_matchp matchp_enum_get_next_f(void *matchp_enum); /* IN/OUT */
const_matchp matchp_cache_enum_get_next(void *matchp_cache_enum); /* IN */
#endif
#ifndef ALREADY_INCLUDED_OUTPUT
#define ALREADY_INCLUDED_OUTPUT
/*
* Copyright (c) 2002 - 2005 Magnus Lind.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software, alter it and re-
* distribute it freely for any non-commercial, non-profit purpose subject to
* the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any distribution.
*
* 4. The names of this software and/or it's copyright holders may not be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
*/
struct _output_ctx {
unsigned int bitbuf;
int pos;
int start;
struct membuf *buf;
};
typedef struct _output_ctx output_ctx[1];
typedef struct _output_ctx *output_ctxp;
void output_ctx_init(output_ctx ctx, struct membuf *out); /* IN/OUT */
unsigned int output_get_pos(output_ctx ctx); /* IN */
void output_byte(output_ctx ctx, /* IN/OUT */
unsigned char byte); /* IN */
void output_word(output_ctx ctx, /* IN/OUT */
unsigned short int word); /* IN */
void output_bits_flush(output_ctx ctx); /* IN/OUT */
void output_bits(output_ctx ctx, /* IN/OUT */
int count, /* IN */
int val); /* IN */
void output_gamma_code(output_ctx ctx, /* IN/OUT */
int code); /* IN */
#endif
#ifndef ALREADY_INCLUDED_SEARCH
#define ALREADY_INCLUDED_SEARCH
/*
* Copyright (c) 2002 - 2005 Magnus Lind.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software, alter it and re-
* distribute it freely for any non-commercial, non-profit purpose subject to
* the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any distribution.
*
* 4. The names of this software and/or it's copyright holders may not be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
*/
struct _search_node {
int index;
match match;
unsigned int total_offset;
float total_score;
struct _search_node *prev;
};
typedef struct _search_node search_node[1];
typedef struct _search_node *search_nodep;
typedef const struct _search_node *const_search_nodep;
struct _encode_match_data {
output_ctxp out;
void *priv;
};
typedef struct _encode_match_data encode_match_data[1];
typedef struct _encode_match_data *encode_match_datap;
/* example of what may be used for priv data
* field in the encode_match_data struct */
typedef
float encode_int_f(int val, void *priv, output_ctxp out); /* IN */
struct _encode_match_priv {
int lit_num;
int seq_num;
int rle_num;
float lit_bits;
float seq_bits;
float rle_bits;
encode_int_f *offset_f;
encode_int_f *len_f;
void *offset_f_priv;
void *len_f_priv;
output_ctxp out;
};
typedef struct _encode_match_priv encode_match_priv[1];
typedef struct _encode_match_priv *encode_match_privp;
/* end of example */
typedef
float encode_match_f(const_matchp mp, encode_match_data emd); /* IN */
void search_node_dump(search_nodep snp); /* IN */
void search_node_free(search_nodep snp); /* IN/OUT */
search_nodep search_buffer(match_ctx ctx, /* IN */
encode_match_f * f, /* IN */
encode_match_data emd,
int use_literal_sequences); /* IN */
struct _matchp_snp_enum {
const_search_nodep startp;
const_search_nodep currp;
};
typedef struct _matchp_snp_enum matchp_snp_enum[1];
typedef struct _matchp_snp_enum *matchp_snp_enump;
void matchp_snp_get_enum(const_search_nodep snp, /* IN */
matchp_snp_enum snpe); /* IN/OUT */
const_matchp matchp_snp_enum_get_next(void *matchp_snp_enum);
#endif
#ifndef ALREADY_INCLUDED_RADIX
#define ALREADY_INCLUDED_RADIX
/*
* Copyright (c) 2002, 2003 Magnus Lind.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software, alter it and re-
* distribute it freely for any non-commercial, non-profit purpose subject to
* the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any distribution.
*
* 4. The names of this software and/or it's copyright holders may not be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
*/
typedef struct _radix_node *radix_nodep;
struct _radix_root {
int depth;
radix_nodep root;
struct chunkpool mem[1];
};
typedef struct _radix_root radix_root[1];
typedef struct _radix_root *radix_rootp;
typedef void free_callback(void *data, void *priv);
/* *f will be called even for null pointers */
void radix_tree_free(radix_root rr, /* IN */
free_callback * f, /* IN */
void *priv); /* IN */
void radix_tree_init(radix_root rr); /* IN */
void radix_node_set(radix_root rr, /* IN */
unsigned int index, /* IN */
void *data); /* IN */
void *radix_node_get(radix_root rr, /* IN */
unsigned int index); /* IN */
#endif
#ifndef MEMBUF_IO_ALREADY_INCLUDED
#define MEMBUF_IO_ALREADY_INCLUDED
/*
* Copyright (c) 2005 Magnus Lind.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software, alter it and re-
* distribute it freely for any non-commercial, non-profit purpose subject to
* the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any distribution.
*
* 4. The names of this software and/or it's copyright holders may not be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
*/
void read_file(const char *name, struct membuf *buf);
void write_file(const char *name, struct membuf *buf);
#endif
#ifndef ALREADY_INCLUDED_MEMBUF
#define ALREADY_INCLUDED_MEMBUF
/*
* Copyright (c) 2002 - 2005 Magnus Lind.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software, alter it and re-
* distribute it freely for any non-commercial, non-profit purpose subject to
* the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any distribution.
*
* 4. The names of this software and/or it's copyright holders may not be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
*/
#define STATIC_MEMBUF_INIT {0, 0, 0}
struct membuf {
void *buf;
int len;
int size;
};
void membuf_init(struct membuf *sb);
void membuf_clear(struct membuf *sb);
void membuf_free(struct membuf *sb);
void membuf_new(struct membuf **sbp);
void membuf_delete(struct membuf **sbp);
/* Gets the length of data put into the membuf */
int membuf_memlen(const struct membuf *sb);
void membuf_truncate(struct membuf *sb, int len);
/* returns the new len or < 0 if failure */
int membuf_trim(struct membuf *sb, int pos);
void *membuf_memcpy(struct membuf *sb, int offset, const void *mem, int len);
void *membuf_append(struct membuf *sb, const void *mem, int len);
void *membuf_append_char(struct membuf *sb, char c);
void *membuf_insert(struct membuf *sb, int offset, const void *mem, int len);
void membuf_remove(struct membuf *sb, int offset, int len);
/* Grows the capacity if it's less than the given size */
void membuf_atleast(struct membuf *sb, int size);
/* Skrinks the capacity if it's greater than the given size */
void membuf_atmost(struct membuf *sb, int size);
/* Gets the current capacity of the membuf */
int membuf_get_size(const struct membuf *sb);
/* Gets a pointer to the internal buffer. Don't dereferece it beyond
* its size. */
void *membuf_get(const struct membuf *sb);
#endif
#ifndef INCLUDED_PARSE
#define INCLUDED_PARSE
/*
* Copyright (c) 2005 Magnus Lind.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software, alter it and re-
* distribute it freely for any non-commercial, non-profit purpose subject to
* the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any distribution.
*
* 4. The names of this software and/or it's copyright holders may not be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
*/
#define ATOM_TYPE_OP_ARG_NONE 0 /* uses u.op */
#define ATOM_TYPE_OP_ARG_U8 1 /* uses u.op */
#define ATOM_TYPE_OP_ARG_U16 2 /* uses u.op */
#define ATOM_TYPE_OP_ARG_I8 3 /* uses u.op */
#define ATOM_TYPE_OP_ARG_UI8 4 /* uses u.op */
#define ATOM_TYPE_EXPRS 12 /* uses u.exprs */
#define ATOM_TYPE_WORD_EXPRS 10 /* uses u.exprs */
#define ATOM_TYPE_BYTE_EXPRS 11 /* uses u.exprs */
#define ATOM_TYPE_RES 13 /* uses u.res */
#define ATOM_TYPE_BUFFER 14 /* uses u.buffer */
struct op
{
struct expr *arg;
u8 code;
};
struct res
{
struct expr *length;
struct expr *value;
};
struct buffer
{
const char *name;
i32 length;
i32 skip;
};
struct atom
{
u8 type;
union
{
struct op op;
struct vec *exprs;
struct buffer buffer;
struct res res;
} u;
};
extern int push_state_skip;
extern int push_state_macro;
extern int push_state_init;
extern int num_lines;
void parse_init(void);
void parse_free(void);
void set_initial_symbol(const char *symbol, i32 value);
void initial_symbol_dump(int level, const char *symbol);
struct membuf *new_initial_named_buffer(const char *name);
int assemble(struct membuf *source, struct membuf *dest);
/* start of internal functions */
struct atom *new_op(u8 op_code, u8 op_size, struct expr *arg);
struct atom *new_op0(u8 op_code);
struct atom *new_exprs(struct expr *arg);
struct atom *exprs_add(struct atom *atom, struct expr *arg);
struct atom *exprs_to_byte_exprs(struct atom *atom);
struct atom *exprs_to_word_exprs(struct atom *atom);
struct atom *new_res(struct expr *len, struct expr *value);
struct atom *new_incbin(const char *name,
struct expr *skip, struct expr *len);
struct expr *new_is_defined(const char *symbol);
struct expr *new_expr_inclen(const char *name);
struct expr *new_expr_incword(const char *name,
struct expr *skip);
void new_symbol_expr(const char *symbol, struct expr *arg);
void new_symbol_expr_guess(const char *symbol, struct expr *arg);
/* returns NULL if found, not otherwise, expp may be NULL. */
const char *find_symref(const char *symbol,
struct expr **expp);
int resolve_symbol(const char *symbol, int *has_valuep, i32 *valuep);
void symbol_dump_resolved(int level, const char *symbol);
void new_label(const char *label);
void set_org(struct expr *arg);
void push_if_state(struct expr *arg);
void push_macro_state(const char *name);
void macro_append(const char *text);
void asm_error(const char *msg);
void asm_echo(const char *msg, struct atom *atom);
void asm_include(const char *msg);
void output_atoms(struct membuf *out, struct vec *mem);
void asm_src_buffer_push(struct membuf *buf);
int assembleSinglePass(struct membuf *source, struct membuf *dest);
#endif
#ifndef ALREADY_INCLUDED_OPTIMAL
#define ALREADY_INCLUDED_OPTIMAL
/*
* Copyright (c) 2002 - 2005 Magnus Lind.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software, alter it and re-
* distribute it freely for any non-commercial, non-profit purpose subject to
* the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any distribution.
*
* 4. The names of this software and/or it's copyright holders may not be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
*/
float optimal_encode(const_matchp mp, /* IN */
encode_match_data emp); /* IN */
void optimal_init(encode_match_data emp); /* IN/OUT */
void optimal_free(encode_match_data emd); /* IN */
void optimal_optimize(encode_match_data emd, /* IN/OUT */
matchp_enum_get_next_f * f, /* IN */
void *priv); /* IN */
void optimal_encoding_import(encode_match_data emd, /* IN/OUT */
const char *encoding); /* IN */
const char *
optimal_encoding_export(encode_match_data emd); /* IN */
void optimal_dump(int level, encode_match_data emp); /* IN */
void optimal_out(output_ctx out, /* IN/OUT */
encode_match_data emd); /* IN */
#endif
#ifndef ALREADY_INCLUDED_VEC
#define ALREADY_INCLUDED_VEC
/*
* Copyright (c) 2003 - 2005 Magnus Lind.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software, alter it and re-
* distribute it freely for any non-commercial, non-profit purpose subject to
* the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any distribution.
*
* 4. The names of this software and/or it's copyright holders may not be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
*/
#define STATIC_VEC_INIT(EL_SIZE) {(EL_SIZE), STATIC_MEMBUF_INIT, 1}
struct vec {
size_t elsize;
struct membuf buf;
int flags;
};
struct vec_iterator {
const struct vec *vec;
int pos;
};
void vec_init(struct vec *p, size_t elsize);
void vec_clear(struct vec *p, cb_free * f);
void vec_free(struct vec *p, cb_free * f);
int vec_count(const struct vec *p);
void *vec_get(const struct vec *p, int index);
/**
* Returns a pointer to the set area or null if the index is out of
* bounds.
**/
void *vec_set(struct vec *p, int index, const void *in);
void *vec_insert(struct vec *p, int index, const void *in);
void vec_remove(struct vec *p, int index);
void *vec_push(struct vec *p, const void *in);
/**
* Gets the position where the key is stored in the vector. The vector
* needs to be sorted for this function to work. Returns the position,
* -1 on error or a negative number that can be converted to where
* it should have been if it had been inserted. insert_pos = -(val + 2)
**/
int vec_find(const struct vec *p, cb_cmp * f, const void *key);
/**
* Gets a pointer to the element that the key points to.
* Returns a pointer that may be null if not found.
**/
void *vec_find2(const struct vec *p, cb_cmp * f, const void *key);
/**
* Inserts the in element in its correct position in a sorted vector.
* returns 1 if insertion is successful, 0 if element is already
* present or -1 on error. If out is not NULL it will be
* dereferenced and set to the inserted or present element.
**/
int vec_insert_uniq(struct vec *p, cb_cmp * f, const void *in, void **out);
void vec_sort(struct vec *p, cb_cmp * f);
void vec_get_iterator(const struct vec *p, struct vec_iterator *i);
void *vec_iterator_next(struct vec_iterator *i);
int vec_equals(const struct vec *a, const struct vec *b, cb_cmp *equals);
void vec_fprint(FILE *, const struct vec *a, cb_fprint *fprint);
#endif
#ifndef ALREADY_INCLUDED_MAP
#define ALREADY_INCLUDED_MAP
/*
* Copyright (c) 2006 Magnus Lind.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software, alter it and re-
* distribute it freely for any non-commercial, non-profit purpose subject to
* the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any distribution.
*
* 4. The names of this software and/or it's copyright holders may not be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
*/
struct map_entry {
const char *key;
void *value;
};
#define STATIC_MAP_INIT {STATIC_VEC_INIT(sizeof(struct map_entry))}
struct map {
struct vec vec;
};
struct map_iterator {
struct vec_iterator vec;
};
void map_init(struct map *m);
void map_clear(struct map *m);
void map_free(struct map *m);
void *map_put(struct map *m, const char *key, void *value);
void *map_get(const struct map *m, const char *key);
int map_contains_key(const struct map *m, const char *key);
void map_put_all(struct map *m, const struct map *source);
int map_contains(const struct map *m1, const struct map *m2, cb_cmp *f);
/**
* If f is NULL, only the keys will be compared.
* returns -1 on error, 1 on equality and 0 otherwise,
**/
int map_equals(const struct map *m1, const struct map *m2, cb_cmp *f);
void map_get_iterator(const struct map *p, struct map_iterator *i);
const struct map_entry *map_iterator_next(struct map_iterator *i);
#endif
#ifndef NAMED_BUFFER_INCLUDED
#define NAMED_BUFFER_INCLUDED
/*
* Copyright (c) 2005 Magnus Lind.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software, alter it and re-
* distribute it freely for any non-commercial, non-profit purpose subject to
* the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any distribution.
*
* 4. The names of this software and/or it's copyright holders may not be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
*/
struct named_buffer {
struct map map;
struct chunkpool buf;
};
void named_buffer_init(struct named_buffer *nb);
void named_buffer_free(struct named_buffer *nb);
void named_buffer_clear(struct named_buffer *nb);
void named_buffer_copy(struct named_buffer *nb,
const struct named_buffer *source);
struct membuf *new_named_buffer(struct named_buffer *nb, const char *name);
struct membuf *get_named_buffer(struct named_buffer *nb, const char *name);
#endif
#ifndef ALREADY_INCLUDED_LOG
#define ALREADY_INCLUDED_LOG
/*
* Copyright (c) 2002, 2003 Magnus Lind.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software, alter it and re-
* distribute it freely for any non-commercial, non-profit purpose subject to
* the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any distribution.