Commit 85e3a35d authored by Julian Kranz's avatar Julian Kranz

Endianness

parent 0c1fb475
...@@ -219,8 +219,12 @@ static inline int_t consume(state_t s, char size) { ...@@ -219,8 +219,12 @@ static inline int_t consume(state_t s, char size) {
longjmp(s->err_tgt, 1); longjmp(s->err_tgt, 1);
}; };
int_t result = 0; int_t result = 0;
char size_left = size; if(!s->buf_be) {
while(size_left) { while(size)
result |= *(s->ip++) << (--size * 8);
return result;
}
while(size) {
char be_buf_left = -((size_t) s->buf_be) & (s->token_size - 1); char be_buf_left = -((size_t) s->buf_be) & (s->token_size - 1);
if(!be_buf_left) { if(!be_buf_left) {
s->buf_be -= s->token_size; s->buf_be -= s->token_size;
...@@ -229,8 +233,8 @@ static inline int_t consume(state_t s, char size) { ...@@ -229,8 +233,8 @@ static inline int_t consume(state_t s, char size) {
s->buf_be[i] = s->le ? s->ip[s->token_size - i - 1] : s->ip[i]; s->buf_be[i] = s->le ? s->ip[s->token_size - i - 1] : s->ip[i];
be_buf_left += s->token_size; be_buf_left += s->token_size;
} }
for(; be_buf_left && size_left; be_buf_left--) { for(; be_buf_left && size; be_buf_left--) {
result |= *(s->buf_be++) << (--size_left * 8); result |= *(s->buf_be++) << (--size * 8);
s->ip++; s->ip++;
} }
} }
...@@ -238,6 +242,10 @@ static inline int_t consume(state_t s, char size) { ...@@ -238,6 +242,10 @@ static inline int_t consume(state_t s, char size) {
} }
static inline void unconsume(state_t s, char size) { static inline void unconsume(state_t s, char size) {
if(!s->buf_be) {
s->ip -= size;
return;
}
char be_buf_consumed = ((size_t) s->buf_be) & (s->token_size - 1); char be_buf_consumed = ((size_t) s->buf_be) & (s->token_size - 1);
if(size < be_buf_consumed) { if(size < be_buf_consumed) {
s->buf_be -= size; s->buf_be -= size;
...@@ -261,8 +269,34 @@ static inline void unconsume(state_t s, char size) { ...@@ -261,8 +269,34 @@ static inline void unconsume(state_t s, char size) {
void void
@endianness@ @endianness@
(state_t s, int_t kind, int_t size) { (state_t s, int_t le, int_t size) {
if (size != 1 && size != 2 && size != 4 && size != 8) {
s->err_str = "GDSL runtime: endianness(); invalid token size";
longjmp(s->err_tgt, 100);
};
char be_buf_consumed = ((size_t) s->buf_be) & (s->token_size - 1);
if (be_buf_consumed) {
s->err_str = "GDSL runtime: endianness(); unable to change endianness settings within a token";
longjmp(s->err_tgt, 101);
};
if(size == 1 || !le) {
if(s->buf_be) {
s->buf_be = (unsigned char*)(((size_t)s->buf_be | (s->token_size - 1)) - s->token_size + 1);
free(s->buf_be);
s->buf_be = 0;
}
} else {
if(!s->buf_be)
s->buf_be = (unsigned char*)malloc(size);
else {
s->buf_be -= s->token_size;
if(size > s->token_size)
s->buf_be = (unsigned char*)realloc(s->buf_be, size);
}
s->buf_be += size;
}
s->token_size = size;
s->le = (char)le;
} }
static int_t vec_to_signed(state_t s, vec_t v) { static int_t vec_to_signed(state_t s, vec_t v) {
...@@ -377,8 +411,11 @@ void ...@@ -377,8 +411,11 @@ void
free (heap); free (heap);
heap = prev; heap = prev;
} }
s->buf_be = (unsigned char*)(((size_t)s->buf_be | (s->token_size - 1)) - s->token_size + 1); if(s->buf_be) {
free(s->buf_be); char be_buf_left = -((size_t) s->buf_be) & (s->token_size - 1);
s->buf_be += be_buf_left - s->token_size;
free(s->buf_be);
}
free(s); free(s);
} }
...@@ -407,8 +444,8 @@ state_t ...@@ -407,8 +444,8 @@ state_t
/* /*
* Todo: Handle alignment error * Todo: Handle alignment error
*/ */
s->buf_be = (unsigned char*)malloc(s->token_size); @endianness@
s->buf_be += s->token_size; (s, 0, 1);
return s; return s;
} }
......
...@@ -21,7 +21,7 @@ val config-default-opnd-sz-32 = '10' ...@@ -21,7 +21,7 @@ val config-default-opnd-sz-32 = '10'
val test-opt opt config = if (zx (opt and config)) > 0 then '1' else '0' val test-opt opt config = if (zx (opt and config)) > 0 then '1' else '0'
val decode config = do val decode config = do
set-endianness LITTLE_ENDIAN 1; set-endianness LITTLE_ENDIAN 2;
update @{tab=void}; update @{tab=void};
main config main config
end end
......
...@@ -85,6 +85,8 @@ int main(int argc, char** argv) { ...@@ -85,6 +85,8 @@ int main(int argc, char** argv) {
char *buffer = (char*) malloc(buffer_size); char *buffer = (char*) malloc(buffer_size);
size_t buffer_length = fread(buffer, 1, buffer_size, f); size_t buffer_length = fread(buffer, 1, buffer_size, f);
fclose(f);
state_t state = gdsl_init(); state_t state = gdsl_init();
gdsl_set_code(state, buffer, buffer_length, 0); gdsl_set_code(state, buffer, buffer_length, 0);
...@@ -151,6 +153,7 @@ int main(int argc, char** argv) { ...@@ -151,6 +153,7 @@ int main(int argc, char** argv) {
long time = end.tv_sec * NANOS + end.tv_nsec - start.tv_nsec - start.tv_sec * NANOS; long time = end.tv_sec * NANOS + end.tv_nsec - start.tv_nsec - start.tv_sec * NANOS;
gdsl_destroy(state); gdsl_destroy(state);
free(buffer);
fprintf(stderr, "---------------------------\n"); fprintf(stderr, "---------------------------\n");
fprintf(stderr, "Statistics\n"); fprintf(stderr, "Statistics\n");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment