1 #include <stdint.h> 2 #include <inttypes.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include <stdio.h> 6 #include <assert.h> 7 #include "opcodes.h" 8 9 /* The abstracted result of an CU42 insn */ 10 typedef struct { 11 uint64_t addr1; // target 12 uint64_t len1; 13 uint64_t addr2; // source 14 uint64_t len2; 15 uint32_t cc; 16 } cu42_t; 17 18 /* Define various input buffers. */ 19 20 /* U+0000 to U+d7ff: Result is 2 bytes for each uint32_t 21 U+dc00 to U+ffff: Result is 2 bytes for each uint32_t */ 22 uint32_t pattern2[] = { 23 0x0000, 0xd7ff, /* corner cases */ 24 0xdc00, 0xffff, /* corner cases */ 25 0xabba, 0xf00d, 0xd00f, 0x1234 /* misc */ 26 }; 27 28 /* U+00010000 to U+0010ffff: Result is 4 bytes for each uint32_t */ 29 uint32_t pattern4[] = { 30 0x00010000, 0x0010ffff, /* corner cases */ 31 0x00010123, 0x00023456, 0x000789ab, 0x00100000 /* misc */ 32 }; 33 34 /* Invalid UTF-32 character */ 35 uint32_t invalid[] = { 36 0x0000d800, 0x0000dbff, /* corner cases */ 37 0x00110000, 0xffffffff, /* corner cases */ 38 0x0000daad, 0x0000d901, 0x0000d8ff, /* misc */ 39 0x00110011, 0x01000000, 0x10000000, 0xdeadbeef /* misc */ 40 }; 41 42 /* Mixed bytes */ 43 uint32_t mixed[] = { 44 0x00000078 /* 2 bytes */, 45 0x0000d000 /* 2 bytes */, 46 0x00033333 /* 4 bytes */, 47 0x00040404 /* 4 bytes */, 48 0x0000abcd /* 2 bytes */, 49 }; 50 51 /* This is the buffer for the converted bytes. */ 52 uint16_t buff[1000]; /* Large so we con'don't have to worry about it */ 53 54 void write_and_check(uint32_t *, unsigned, unsigned); 55 56 57 static cu42_t 58 do_cu42(uint16_t *dst, uint64_t dst_len, uint32_t *src, uint64_t src_len) 59 { 60 int cc = 42; 61 cu42_t regs; 62 63 /* build up the register pairs */ 64 register uint32_t *source asm("4") = src; 65 register uint64_t source_len asm("5") = src_len; 66 register uint16_t *dest asm("2") = dst; 67 register uint64_t dest_len asm("3") = dst_len; 68 69 asm volatile( 70 CU42(2,4) 71 "ipm %2\n\t" 72 "srl %2,28\n\t" 73 : "+d"(dest), "+d"(source), "=d"(cc), 74 "+d"(source_len), "+d"(dest_len) 75 : 76 : "memory", "cc"); 77 78 /* Capture register contents at end of cu42 */ 79 regs.addr1 = (uint64_t)dest; 80 regs.len1 = dest_len; 81 regs.addr2 = (uint64_t)source; 82 regs.len2 = source_len; 83 regs.cc = cc; 84 85 return regs; 86 } 87 88 void 89 run_test(uint16_t *dst, uint64_t dst_len, uint32_t *src, uint64_t src_len) 90 { 91 int i; 92 cu42_t result; 93 94 result = do_cu42(dst, dst_len, src, src_len); 95 96 // Write out the converted values, if any 97 printf("UTF16: "); 98 if (dst_len - result.len1 == 0) 99 printf(" <none>"); 100 else 101 assert((dst_len - result.len1) % 2 == 0); 102 for (i = 0; i < (dst_len - result.len1) / 2; ++i) { 103 printf(" %04x", dst[i]); 104 } 105 printf("\n"); 106 107 printf(" cc = %d\n", result.cc); 108 if (dst != NULL) 109 printf(" dst address difference: %"PRId64, result.addr1 - (uint64_t)dst); 110 printf(" dst len: %"PRId64"\n", result.len1); 111 112 if (src != NULL) 113 printf(" src address difference: %"PRId64, result.addr2 - (uint64_t)src); 114 printf(" src len: %"PRId64"\n", result.len2); 115 } 116 117 int main() 118 { 119 int i; 120 121 /* Length == 0, no memory should be read or written */ 122 printf("\n------------- test1 ----------------\n"); 123 run_test(NULL, 0, NULL, 0); 124 125 /* Test exhaustion of source length (source bytes are valid) */ 126 printf("\n------------- test2.1 ----------------\n"); 127 128 /* No character will be written to BUFF, i.e. loop in jitted code 129 is not iterated */ 130 run_test(buff, sizeof buff, NULL, 0); 131 run_test(buff, sizeof buff, NULL, 1); 132 run_test(buff, sizeof buff, NULL, 2); 133 run_test(buff, sizeof buff, NULL, 3); 134 run_test(buff, sizeof buff, pattern2, 0); 135 run_test(buff, sizeof buff, pattern2, 1); 136 run_test(buff, sizeof buff, pattern2, 2); 137 run_test(buff, sizeof buff, pattern2, 3); 138 139 printf("\n------------- test2.2 ----------------\n"); 140 /* At least one character will be written to BUFF, i.e. loop in jitted 141 code is iterated */ 142 run_test(buff, sizeof buff, pattern2, 4); /* 1 utf32 -> 1 utf16 */ 143 run_test(buff, sizeof buff, pattern2, 10); /* 2 utf32 -> 2 utf16 */ 144 run_test(buff, sizeof buff, pattern4, 5); /* 1 utf32 -> 2 utf16 */ 145 run_test(buff, sizeof buff, pattern4, 11); /* 2 utf32 -> 4 utf16 */ 146 run_test(buff, sizeof buff, pattern4, 18); /* 4 utf32 -> 8 utf16 */ 147 148 /* Test exhaustion of destination length (source bytes are valid) */ 149 printf("\n------------- test3.1 ----------------\n"); 150 151 /* No character will be written to BUFF, i.e. loop in jitted code 152 is not iterated */ 153 154 /* Want to write at least 1 UTF-16 */ 155 run_test(NULL, 0, pattern2, sizeof pattern2); 156 157 /* Want to write at least 1 UTF-16 */ 158 run_test(NULL, 0, pattern2, sizeof pattern2); 159 run_test(NULL, 1, pattern2, sizeof pattern2); 160 161 /* Want to write at least 2 UTF-16 */ 162 run_test(NULL, 0, pattern4, sizeof pattern4); 163 run_test(NULL, 1, pattern4, sizeof pattern4); 164 run_test(NULL, 2, pattern4, sizeof pattern4); 165 run_test(NULL, 3, pattern4, sizeof pattern4); 166 167 /* When both operands are exhausted, cc=0 takes precedence. 168 (test1 tests this for len == 0) */ 169 printf("\n------------- test4 ----------------\n"); 170 run_test(buff, 4, pattern2, 8); 171 172 /* Input contains invalid characters */ 173 174 // As conversion stops upon encountering an invalid character, we 175 // need to test each invalid character separately, to make sure it 176 // is recognized as invalid. 177 178 printf("\n------------- test5 ----------------\n"); 179 for (i = 0; i < sizeof invalid / 4; ++i) { 180 run_test(buff, sizeof buff, invalid + i, 4); 181 } 182 run_test(buff, 0, invalid, sizeof invalid); // cc = 2 183 run_test(buff, 100, invalid, sizeof invalid); 184 185 /* Convert all pattern buffers */ 186 printf("\n------------- test6 ----------------\n"); 187 run_test(buff, sizeof buff, pattern2, sizeof pattern2); 188 run_test(buff, sizeof buff, pattern4, sizeof pattern4); 189 run_test(buff, sizeof buff, mixed, sizeof mixed); 190 191 /* Make sure we only write the exact number of bytes (and not more) */ 192 193 /* Write 2 bytes */ 194 printf("\n------------- test7.1 ----------------\n"); 195 write_and_check(pattern2 + 3, 4, 2); 196 197 /* Write 4 bytes */ 198 printf("\n------------- test7.2 ----------------\n"); 199 write_and_check(pattern4 + 5, 4, 4); 200 201 return 0; 202 } 203 204 205 void 206 write_and_check_aux(uint32_t *input, unsigned num_input_bytes, 207 unsigned num_expected_output_bytes, 208 unsigned fill_byte) 209 { 210 int num_errors, i; 211 212 /* Fill output buffer with FILL_BYTE */ 213 memset(buff, fill_byte, sizeof buff); 214 215 /* Execute cu42 */ 216 run_test(buff, sizeof buff, input, num_input_bytes); 217 218 /* Make sure the rest of the buffer is unmodified. */ 219 num_errors = 0; 220 for (i = num_expected_output_bytes; i < sizeof buff; ++i) 221 if (((unsigned char *)buff)[i] != fill_byte) ++num_errors; 222 if (num_errors) 223 fprintf(stderr, "*** wrote more than %d bytes\n", 224 num_expected_output_bytes); 225 } 226 227 void 228 write_and_check(uint32_t *input, unsigned num_input_bytes, 229 unsigned num_expected_output_bytes) 230 { 231 write_and_check_aux(input, num_input_bytes, num_expected_output_bytes, 0x0); 232 233 /* Run again with different fill pattern to make sure we did not write 234 an extra 0x0 byte */ 235 write_and_check_aux(input, num_input_bytes, num_expected_output_bytes, 0xFF); 236 } 237