1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright (c) 2017 Rob Clark <robdclark@gmail.com> 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21bf215546Sopenharmony_ci * SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#ifndef _ASM_H_ 25bf215546Sopenharmony_ci#define _ASM_H_ 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_ci#include <stdbool.h> 28bf215546Sopenharmony_ci#include <stdint.h> 29bf215546Sopenharmony_ci#include "afuc.h" 30bf215546Sopenharmony_ci 31bf215546Sopenharmony_ciextern int gpuver; 32bf215546Sopenharmony_ci 33bf215546Sopenharmony_ci/** 34bf215546Sopenharmony_ci * Intermediate representation for an instruction, before final encoding. 35bf215546Sopenharmony_ci * This mostly exists because we need to resolve label offset's in a 2nd 36bf215546Sopenharmony_ci * pass, but also so that parser.y doesn't really need to care so much 37bf215546Sopenharmony_ci * about the different encodings for 2src regs vs 1src+immed, or mnemonics 38bf215546Sopenharmony_ci */ 39bf215546Sopenharmony_cistruct asm_instruction { 40bf215546Sopenharmony_ci int tok; 41bf215546Sopenharmony_ci int dst; 42bf215546Sopenharmony_ci int src1; 43bf215546Sopenharmony_ci int src2; 44bf215546Sopenharmony_ci int immed; 45bf215546Sopenharmony_ci int shift; 46bf215546Sopenharmony_ci int bit; 47bf215546Sopenharmony_ci int xmov; 48bf215546Sopenharmony_ci uint32_t literal; 49bf215546Sopenharmony_ci const char *label; 50bf215546Sopenharmony_ci 51bf215546Sopenharmony_ci bool has_immed : 1; 52bf215546Sopenharmony_ci bool has_shift : 1; 53bf215546Sopenharmony_ci bool has_bit : 1; 54bf215546Sopenharmony_ci bool is_literal : 1; 55bf215546Sopenharmony_ci bool rep : 1; 56bf215546Sopenharmony_ci}; 57bf215546Sopenharmony_ci 58bf215546Sopenharmony_cistruct asm_label { 59bf215546Sopenharmony_ci unsigned offset; 60bf215546Sopenharmony_ci const char *label; 61bf215546Sopenharmony_ci}; 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_cistruct asm_instruction *next_instr(int tok); 64bf215546Sopenharmony_civoid decl_label(const char *str); 65bf215546Sopenharmony_ci 66bf215546Sopenharmony_cistatic inline uint32_t 67bf215546Sopenharmony_ciparse_reg(const char *str) 68bf215546Sopenharmony_ci{ 69bf215546Sopenharmony_ci char *retstr; 70bf215546Sopenharmony_ci long int ret; 71bf215546Sopenharmony_ci 72bf215546Sopenharmony_ci if (!strcmp(str, "$rem")) 73bf215546Sopenharmony_ci return REG_REM; 74bf215546Sopenharmony_ci else if (!strcmp(str, "$memdata")) 75bf215546Sopenharmony_ci return REG_MEMDATA; 76bf215546Sopenharmony_ci else if (!strcmp(str, "$addr")) 77bf215546Sopenharmony_ci return REG_ADDR; 78bf215546Sopenharmony_ci else if (!strcmp(str, "$regdata")) 79bf215546Sopenharmony_ci return REG_REGDATA; 80bf215546Sopenharmony_ci else if (!strcmp(str, "$usraddr")) 81bf215546Sopenharmony_ci return REG_USRADDR; 82bf215546Sopenharmony_ci else if (!strcmp(str, "$data")) 83bf215546Sopenharmony_ci return 0x1f; 84bf215546Sopenharmony_ci 85bf215546Sopenharmony_ci ret = strtol(str + 1, &retstr, 16); 86bf215546Sopenharmony_ci 87bf215546Sopenharmony_ci if (*retstr != '\0') { 88bf215546Sopenharmony_ci printf("invalid register: %s\n", str); 89bf215546Sopenharmony_ci exit(2); 90bf215546Sopenharmony_ci } 91bf215546Sopenharmony_ci 92bf215546Sopenharmony_ci return ret; 93bf215546Sopenharmony_ci} 94bf215546Sopenharmony_ci 95bf215546Sopenharmony_cistatic inline uint32_t 96bf215546Sopenharmony_ciparse_literal(const char *str) 97bf215546Sopenharmony_ci{ 98bf215546Sopenharmony_ci char *retstr; 99bf215546Sopenharmony_ci long int ret; 100bf215546Sopenharmony_ci 101bf215546Sopenharmony_ci ret = strtol(str + 1, &retstr, 16); 102bf215546Sopenharmony_ci 103bf215546Sopenharmony_ci if (*retstr != ']') { 104bf215546Sopenharmony_ci printf("invalid literal: %s\n", str); 105bf215546Sopenharmony_ci exit(2); 106bf215546Sopenharmony_ci } 107bf215546Sopenharmony_ci 108bf215546Sopenharmony_ci return ret; 109bf215546Sopenharmony_ci} 110bf215546Sopenharmony_ci 111bf215546Sopenharmony_cistatic inline uint32_t 112bf215546Sopenharmony_ciparse_bit(const char *str) 113bf215546Sopenharmony_ci{ 114bf215546Sopenharmony_ci return strtol(str + 1, NULL, 10); 115bf215546Sopenharmony_ci} 116bf215546Sopenharmony_ci 117bf215546Sopenharmony_ciunsigned parse_control_reg(const char *name); 118bf215546Sopenharmony_ci 119bf215546Sopenharmony_ci/* string trailing ':' off label: */ 120bf215546Sopenharmony_cistatic inline const char * 121bf215546Sopenharmony_ciparse_label_decl(const char *str) 122bf215546Sopenharmony_ci{ 123bf215546Sopenharmony_ci char *s = strdup(str); 124bf215546Sopenharmony_ci s[strlen(s) - 1] = '\0'; 125bf215546Sopenharmony_ci return s; 126bf215546Sopenharmony_ci} 127bf215546Sopenharmony_ci 128bf215546Sopenharmony_civoid yyset_in(FILE *_in_str); 129bf215546Sopenharmony_ci 130bf215546Sopenharmony_ci#endif /* _ASM_H_ */ 131