support 5.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/* Copyright 2013 Castle Technology Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "dis2.h"

/* Minimal sprintf() implementation and other support functions */

size_t strlen(const char *c)
{
	size_t count=0;
	while(*c++)
		count++;
	return count;
}

char *strcat(char *str1,const char *str2)
{
	char *ret = str1;
	while(*str1)
		str1++;
	while((*str1++ = *str2++) != 0) {};
	return ret;
}

36 37 38 39 40 41 42 43 44 45 46 47
int strcmp(const char *str1, const char *str2)
{
	char c;
	int diff;
	do
	{
		c = *str1++;
		diff = c - *str2++;
	} while (c && !diff);
	return diff;
}

48 49 50 51 52 53 54 55 56 57 58 59 60
void *memcpy(void *ptr1,const void *ptr2,size_t n)
{
	while(n--)
	{
		((char *) ptr1)[n] = ((char *) ptr2)[n];
	}
	return ptr1;
}

int sprintf(char *out,const char *format,...)
{
	va_list a;
	va_start(a,format);
61
	int ret = vsnprintf(out,SIZE_MAX,format,a);
62 63 64 65 66 67 68 69 70 71
	va_end(a);
	return ret;
}

extern int _sprintf(char *out,const char *format,...);

int _sprintf(char *out,const char *format,...)
{
	va_list a;
	va_start(a,format);
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	int ret = vsnprintf(out,SIZE_MAX,format,a);
	va_end(a);
	return ret;
}

int snprintf(char *out,size_t max,const char *format,...)
{
	va_list a;
	va_start(a,format);
	int ret = vsnprintf(out,max,format,a);
	va_end(a);
	return ret;
}

extern int _snprintf(char *out,size_t max,const char *format,...);

int _snprintf(char *out,size_t max,const char *format,...)
{
	va_list a;
	va_start(a,format);
	int ret = vsnprintf(out,max,format,a);
93 94 95 96 97 98
	va_end(a);
	return ret;
}

int vsprintf(char *out,const char *format,va_list a)
{
99 100 101 102 103 104 105 106
	return vsnprintf(out,SIZE_MAX,format,a);
}

#define OUTC(C) do { char chr = C; if (count < usable_max) out[count] = chr; count++; } while(0)

int vsnprintf(char *out,size_t max,const char *format,va_list a)
{
	size_t count=0;
107
	int c;
108
	size_t usable_max = (out ? max : 0);
109 110 111 112 113
	while((c = *format++) != 0)
	{
		if(c == '%')
		{
			/* We'll only deal with the following format specifiers:
114
			   %[-<width>]s   (control-terminated - to cope with messages)
115 116 117 118 119 120 121 122 123
			   %c
			   %<width>[ll]X
			   %<width>[ll]x
			   %d
			   %u
			*/
			int width=0;
			bool islong=false;
			c = *format++;
124 125
			if (c == '-')
				c = *format++;
126 127 128 129 130 131 132 133 134 135
			while((c >= '0') && (c <= '9'))
			{
				width = width*10 + c - '0';
				c = *format++;
			}
			switch(c)
			{
			case 's':
				{
				const char *s = va_arg(a,const char *);
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
				if(width)
				{
					/* Assume left-justified */
					do
					{
						OUTC(((*s >= ' ') ? *s++ : ' '));
					}
					while(--width);
				}
				else
				{
					while(*s >= ' ')
					{
						OUTC(*s++);
						width--;
					}
				}
153 154 155
				}
				break;
			case 'c':
156
				OUTC(va_arg(a,int));
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
				break;
			case 'l':
				islong = true;
				format++;
				c = *format++;
				/* Fall through... */
			case 'X':
			case 'x':
				{
				uint64_t h;
				if(islong)
					h = va_arg(a,uint64_t);
				else
					h = va_arg(a,unsigned int);
				int i = 15;
				if(!width)
					width = 1;
				do
				{
176
					unsigned int n = (unsigned int) (h>>60);
177 178 179
					if(n || (i<width))
					{
						width = 16;
180
						char c2;
181
						if(n <= 9)
182
							c2 = n+'0';
183
						else if(c == 'X')
184
							c2 = n+'A'-10;
185
						else
186 187
							c2 = n+'a'-10;
						OUTC(c2);
188 189 190 191 192 193 194 195 196 197 198 199
					}
					h=h<<4;
				} while(--i >= 0);
				}
				break;
			case 'd':
			case 'u':
				{
				unsigned int i = va_arg(a,unsigned int);
				if((i & 0x80000000) && (c == 'd'))
				{
					i = -i;
200
					OUTC('-');
201 202 203 204 205 206 207 208 209 210 211 212
				}
				while(i)
				{
					unsigned int j=1;
					int k=0;
					while((j < 0x10000000) && (j*10 <= i))
					{
						j *= 10;
						k++;
					}
					while(width > k)
					{
213
						OUTC('0');
214 215 216 217 218 219 220 221
						width--;
					}
					c = '0';
					while(i >= j)
					{
						i -= j;
						c++;
					}
222
					OUTC(c);
223 224 225
					width = k-1;
				}
				while(width-- >= 0)
226
					OUTC('0');
227 228 229 230 231 232
				}
				break;
			}
		}
		else
		{
233
			OUTC(c);
234 235
		}
	}
236 237 238 239
	if (out && max)
	{
		out[(count < max) ? count : (max-1)] = 0;
	}
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
	return count;
}

/* Main function called by assembler code */
static const char *conds_cscc[16] = {"EQ","NE","CS","CC","MI","PL","VS","VC","HI","LS","GE","LT","GT","LE","","NV"};

extern char *arm_engine_fromasm(char *buffer,uint32_t instr,uint32_t addr,char **regnames,size_t bufsize);

char *arm_engine_fromasm(char *buffer,uint32_t instr,uint32_t addr,char **regnames,size_t bufsize)
{
	dis_options options;
	for(int i=0;i<16;i++)
		options.regs[i] = regnames[i];
	options.cond = conds_cscc;
	options.reggroups = 0xffff;
	options.warnversions = ~0 - (1<<FPA);
	options.ual = false;
	options.vfpual = true;
	options.dci = false;
	options.bashex = true;
	options.nonstandard_undefined = true;
	options.allhex = false;
	options.swihash = false;
	options.positionind = false;
	options.cols[0] = 8;
	options.cols[1] = 27;
	options.swimode = SWIMODE_NAME;
	options.lfmsfmmode = LFMSFM_FORM1;
	options.comment = ';';

	return arm_engine(instr,addr,&options,buffer,bufsize);
}