Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize/cleanup generate_uudmap.c #22651

Open
wants to merge 4 commits into
base: blead
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile.SH
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ globals$(OBJ_EXT): $(generated_headers)

uudmap.h mg_data.h: bitcount.h

generate_uudmap$(OBJ_EXT): mg_raw.h
generate_uudmap$(OBJ_EXT): generate_uudmap.c mg_raw.h

!NO!SUBS!

Expand Down
202 changes: 128 additions & 74 deletions generate_uudmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,49 @@
"hello world" won't port easily to it. */
#include <errno.h>

#define xstrputc(_ch) *p++ = _ch
#define xstrputs(_str) do {p2 = p; p += sizeof(_str)-1; \
memcpy((void *)p2, (void *)_str, sizeof(_str)-1);} while(0)

static const char * progname;

static void
output_zeros(FILE *out, unsigned int count) {
char buf [sizeof(" 0, \n")+80]; /* tiny oversize */
char * start = buf;
char * p = start;
char * p2;

if(count) {
const unsigned int max0 = sizeof("0, ")-1;
const unsigned int maxln = 80-sizeof(" \n")-1;

xstrputs(" ");
while(count) {
xstrputc('0');
count--;
if(count) {
if(p < start+maxln) {
xstrputs(", ");
continue;
}
else
xstrputs(",\n ");
}
p2 = p;
p = start;
fwrite(start, sizeof(char), p2-start, out);
}
}
}

struct mg_data_raw_t {
unsigned char type;
const char *value;
const char *comment;
};

static struct mg_data_raw_t mg_data_raw[] = {
static const struct mg_data_raw_t mg_data_raw[] = {
#ifdef WIN32
# include "..\mg_raw.h"
#else
Expand All @@ -63,48 +99,61 @@ struct mg_data_t {
const char *comment;
};

static struct mg_data_t mg_data[256];

static void
format_mg_data(FILE *out, const void *thing, size_t count) {
format_mg_data(FILE *out, const void *thing, unsigned int count) {
const struct mg_data_t *p = (const struct mg_data_t *)thing;
unsigned int zero = 0;

while (1) {
if (p->value) {
fprintf(out, " %s\n %s", p->comment, p->value);
unsigned int zero2 = zero;
if (zero2) {
zero = 0;
output_zeros(out, zero2);
fputs(",\n", out);
}
fprintf(out, " %s\n %s,\n", p->comment, p->value);
} else {
fputs(" 0", out);
zero++;
}
++p;
if (!--count)
break;
fputs(",\n", out);
}
fputc('\n', out);
}

static void
format_char_block(FILE *out, const void *thing, size_t count) {
format_char_block(FILE *out, const void *thing, unsigned int count) {
char buf [(sizeof("-255,\n ")-1) * 256]; /* 2048, oversized vs ~900 */
char * start = buf;
char * p = start;
char * p2;
const char *block = (const char *)thing;

fputs(" ", out);
xstrputs(" ");
while (count--) {
fprintf(out, "%d", *block);
const char * fmt;
char c = *block;
block++;
if (count) {
fputs(", ", out);
if (!(count & 15)) {
fputs("\n ", out);
}
if (!(count & 15))
fmt = "%d,\n ";
else
fmt = "%d, ";
}
else
fmt = "%d";
p += sprintf(p, fmt, c);
}
fputc('\n', out);
xstrputc('\n');
fwrite(start, sizeof(char), p-start, out);
}

static void
output_to_file(const char *progname, const char *filename,
void (format_function)(FILE *out, const void *thing, size_t count),
const void *thing, size_t count,
output_to_file(const char *filename,
void (format_function)(FILE *out, const void *thing,
unsigned int count),
const void *thing, unsigned int count,
const char *header
) {
FILE *const out = fopen(filename, "w");
Expand All @@ -115,10 +164,11 @@ output_to_file(const char *progname, const char *filename,
exit(1);
}

fprintf(out, "/* %s:\n", filename);
fprintf(out, " * THIS FILE IS AUTO-GENERATED DURING THE BUILD by: %s\n",
progname);
fprintf(out, " *\n%s\n*/\n{\n", header);
fprintf(out,
"/* %s:\n"
" * THIS FILE IS AUTO-GENERATED DURING THE BUILD by: %s\n"
" *\n%s\n*/\n{\n",
filename, progname, header);
format_function(out, thing, count);
fputs("}\n", out);

Expand All @@ -129,71 +179,75 @@ output_to_file(const char *progname, const char *filename,
}
}


static const char PL_uuemap[]
= "`!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";

typedef unsigned char U8;

/* This will ensure it is initialized to all zeros. */
static char PL_uudmap[256];
static char PL_bitcount[256];

int main(int argc, char **argv) {
size_t i;
int bits;
struct mg_data_raw_t *p = mg_data_raw;
unsigned int i;

progname = argv[0];
if (argc < 4 || argv[1][0] == '\0' || argv[2][0] == '\0'
|| argv[3][0] == '\0') {
fprintf(stderr, "Usage: %s uudemap.h bitcount.h mg_data.h\n", argv[0]);
fprintf(stderr, "Usage: %s uudemap.h bitcount.h mg_data.h\n", progname);
return 1;
}

for (i = 0; i < sizeof(PL_uuemap) - 1; ++i)
PL_uudmap[(U8)PL_uuemap[i]] = (char)i;
/*
* Because ' ' and '`' map to the same value,
* we need to decode them both the same.
*/
PL_uudmap[(U8)' '] = 0;

output_to_file(argv[0], argv[1], &format_char_block,
(const void *)PL_uudmap, sizeof(PL_uudmap),
" * These values will populate PL_uumap[], as used by unpack('u')"
);

for (bits = 1; bits < 256; bits++) {
if (bits & 1) PL_bitcount[bits]++;
if (bits & 2) PL_bitcount[bits]++;
if (bits & 4) PL_bitcount[bits]++;
if (bits & 8) PL_bitcount[bits]++;
if (bits & 16) PL_bitcount[bits]++;
if (bits & 32) PL_bitcount[bits]++;
if (bits & 64) PL_bitcount[bits]++;
if (bits & 128) PL_bitcount[bits]++;
}
do {
char PL_uudmap[256] = {0};
for (i = 0; i < sizeof(PL_uuemap) - 1; ++i)
PL_uudmap[(U8)PL_uuemap[i]] = (char)i;
/*
* Because ' ' and '`' map to the same value,
* we need to decode them both the same.
*/
PL_uudmap[(U8)' '] = 0;
output_to_file(argv[1], &format_char_block,
(const void *)PL_uudmap, sizeof(PL_uudmap),
" * These values will populate PL_uumap[], as used by unpack('u')"
);
} while(0);

do {
char PL_bitcount[256] = {0};
int bits;
for (bits = 1; bits < 256; bits++) {
if (bits & 1) PL_bitcount[bits]++;
if (bits & 2) PL_bitcount[bits]++;
if (bits & 4) PL_bitcount[bits]++;
if (bits & 8) PL_bitcount[bits]++;
if (bits & 16) PL_bitcount[bits]++;
if (bits & 32) PL_bitcount[bits]++;
if (bits & 64) PL_bitcount[bits]++;
if (bits & 128) PL_bitcount[bits]++;
}

output_to_file(argv[0], argv[2], &format_char_block,
(const void *)PL_bitcount, sizeof(PL_bitcount),
" * These values will populate PL_bitcount[]:\n"
" * this is a count of bits for each U8 value 0..255"
);
output_to_file(argv[2], &format_char_block,
(const void *)PL_bitcount, sizeof(PL_bitcount),
" * These values will populate PL_bitcount[]:\n"
" * this is a count of bits for each U8 value 0..255"
);
} while(0);

do {
struct mg_data_t mg_data[256] = {{NULL,NULL}};
const struct mg_data_raw_t *p = mg_data_raw;
while (p->value) {
mg_data[p->type].value = p->value;
mg_data[p->type].comment = p->comment;
++p;
}

while (p->value) {
mg_data[p->type].value = p->value;
mg_data[p->type].comment = p->comment;
++p;
}

output_to_file(argv[0], argv[3], &format_mg_data,
(const void *)mg_data, sizeof(mg_data)/sizeof(mg_data[0]),
" * These values will populate PL_magic_data[]: this is an array of\n"
" * per-magic U8 values containing an index into PL_magic_vtables[]\n"
" * plus two flags:\n"
" * PERL_MAGIC_READONLY_ACCEPTABLE\n"
" * PERL_MAGIC_VALUE_MAGIC"
);
output_to_file(argv[3], &format_mg_data,
(const void *)mg_data, sizeof(mg_data)/sizeof(mg_data[0]),
" * These values will populate PL_magic_data[]: this is an array of\n"
" * per-magic U8 values containing an index into PL_magic_vtables[]\n"
" * plus two flags:\n"
" * PERL_MAGIC_READONLY_ACCEPTABLE\n"
" * PERL_MAGIC_VALUE_MAGIC"
);
} while (0);

return 0;
}
2 changes: 1 addition & 1 deletion win32/GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -1506,7 +1506,7 @@ $(UUDMAP_H) $(MG_DATA_H) : $(BITCOUNT_H)
$(BITCOUNT_H) : $(GENUUDMAP)
$(GENUUDMAP) $(GENERATED_HEADERS)

$(GENUUDMAP) : ..\mg_raw.h
$(GENUUDMAP) : ..\generate_uudmap.c ..\mg_raw.h
ifeq ($(CCTYPE),GCC)
$(LINK32) $(CFLAGS_O) -o..\generate_uudmap.exe ..\generate_uudmap.c \
$(BLINK_FLAGS) $(LIBFILES)
Expand Down
2 changes: 1 addition & 1 deletion win32/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ $(UUDMAP_H) $(MG_DATA_H) : $(BITCOUNT_H)
$(BITCOUNT_H) : $(GENUUDMAP)
$(GENUUDMAP) $(GENERATED_HEADERS)

$(GENUUDMAP_OBJ) : ..\mg_raw.h
$(GENUUDMAP_OBJ) : ..\generate_uudmap.c ..\mg_raw.h

$(GENUUDMAP) : $(GENUUDMAP_OBJ)
$(LINK32) -out:$@ @<<
Expand Down
Loading