comparison gen_arm.c @ 563:c8fefa140c80

Moved some generic stuff from backend.h gen_arm.h and gen_arm.c into gen.h and gen.c. Added a couple fields to cpu_options so that gen_mem_fun can be made guest CPU generic
author Michael Pavone <pavone@retrodev.com>
date Mon, 24 Feb 2014 09:55:24 -0800
parents 474270dbff15
children bff307e03a94
comparison
equal deleted inserted replaced
558:dc9f178085a0 563:c8fefa140c80
55 55
56 #define IMMED 0x2000000u 56 #define IMMED 0x2000000u
57 #define REG 0u 57 #define REG 0u
58 58
59 59
60
61 #define RESERVE_INSTRUCTIONS 4 //1 ldr + 1bx + 1 constant
62 #define CODE_ALLOC_SIZE (1024*1024)
63
64 uint32_t make_immed(uint32_t val) 60 uint32_t make_immed(uint32_t val)
65 { 61 {
66 uint32_t rot_amount = 0; 62 uint32_t rot_amount = 0;
67 for (; rot_amount < 0x20; rot_amount += 2) 63 for (; rot_amount < 0x20; rot_amount += 2)
68 { 64 {
72 } 68 }
73 } 69 }
74 return INVALID_IMMED; 70 return INVALID_IMMED;
75 } 71 }
76 72
77 void init_code_info(code_info *code)
78 {
79 size_t size = CODE_ALLOC_SIZE;
80 code->cur = alloc_code(&size);
81 if (!code->cur) {
82 fputs("Failed to allocate memory for generated code\n", stderr);
83 exit(1);
84 }
85 code->last = code->cur + size/sizeof(uint32_t) - RESERVE_INSTRUCTIONS;
86 }
87
88 void check_alloc_code(code_info *code) 73 void check_alloc_code(code_info *code)
89 { 74 {
90 if (code->cur == code->last) { 75 if (code->cur == code->last) {
91 size_t size = CODE_ALLOC_SIZE; 76 size_t size = CODE_ALLOC_SIZE;
92 uint32_t *next_code = alloc_code(&size); 77 uint32_t *next_code = alloc_code(&size);
93 if (!next_code) { 78 if (!next_code) {
94 fputs("Failed to allocate memory for generated code\n", stderr); 79 fputs("Failed to allocate memory for generated code\n", stderr);
95 exit(1); 80 exit(1);
96 } 81 }
97 if (next_code = code->last + RESERVE_INSTRUCTIONS) { 82 if (next_code = code->last + RESERVE_WORDS) {
98 //new chunk is contiguous with the current one 83 //new chunk is contiguous with the current one
99 code->last = next_code + size/sizeof(uint32_t) - RESERVE_INSTRUCTIONS; 84 code->last = next_code + size/sizeof(code_word) - RESERVE_WORDS;
100 } else { 85 } else {
101 uint32_t * from = code->cur + 2; 86 uint32_t * from = code->cur + 2;
102 if (next_code - from < 0x400000 || from - next_code <= 0x400000) { 87 if (next_code - from < 0x400000 || from - next_code <= 0x400000) {
103 *from = CC_AL | OP_B | ((next_code - from) & 0xFFFFFF); 88 *from = CC_AL | OP_B | ((next_code - from) & 0xFFFFFF);
104 } else { 89 } else {
113 //Load target into r0 98 //Load target into r0
114 *(from++) = CC_AL | OP_MOV | IMMED | NO_COND | immed; 99 *(from++) = CC_AL | OP_MOV | IMMED | NO_COND | immed;
115 } 100 }
116 //branch to address in r0 101 //branch to address in r0
117 *from = CC_AL | OP_BX; 102 *from = CC_AL | OP_BX;
118 code->last = next_code + size/sizeof(uint32_t) - RESERVE_INSTRUCTIONS; 103 code->last = next_code + size/sizeof(code_word) - RESERVE_WORDS;
119 //pop r0 104 //pop r0
120 *(next_code++) = CC_AL | POP; 105 *(next_code++) = CC_AL | POP;
121 code->cur = next_code; 106 code->cur = next_code;
122 } 107 }
123 } 108 }