author  Ryan C. Gordon <icculus@icculus.org> 
Sun, 14 Dec 2008 16:27:40 0500  
changeset 522  3c08f4d2aa2e 
parent 521  57e1945104cb 
child 523  699696afd731 
permissions  rwrr 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

1 
/** 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

2 
* MojoShader; generate shader programs from bytecode of compiled 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

3 
* Direct3D shaders. 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

4 
* 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

5 
* Please see the file LICENSE.txt in the source's root directory. 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

6 
* 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

7 
* This file written by Ryan C. Gordon. 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

8 
*/ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

9 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

10 
#define __MOJOSHADER_INTERNAL__ 1 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

11 
#include "mojoshader_internal.h" 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

12 

497
5b2465d40b57
Disabled tokenizer debug output for now.
Ryan C. Gordon <icculus@icculus.org>
parents:
495
diff
changeset

13 
#define DEBUG_TOKENIZER 0 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

14 

498  15 
// !!! FIXME: no #define support yet. 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

16 

515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

17 
typedef struct TokenizerContext 
514
ba913834b491
The parse_args_DCL fiasco continues.
Ryan C. Gordon <icculus@icculus.org>
parents:
513
diff
changeset

18 
{ 
ba913834b491
The parse_args_DCL fiasco continues.
Ryan C. Gordon <icculus@icculus.org>
parents:
513
diff
changeset

19 
const char *source; 
ba913834b491
The parse_args_DCL fiasco continues.
Ryan C. Gordon <icculus@icculus.org>
parents:
513
diff
changeset

20 
int on_endline; 
ba913834b491
The parse_args_DCL fiasco continues.
Ryan C. Gordon <icculus@icculus.org>
parents:
513
diff
changeset

21 
unsigned int linenum; 
ba913834b491
The parse_args_DCL fiasco continues.
Ryan C. Gordon <icculus@icculus.org>
parents:
513
diff
changeset

22 
char prevchar; 
ba913834b491
The parse_args_DCL fiasco continues.
Ryan C. Gordon <icculus@icculus.org>
parents:
513
diff
changeset

23 
char token[64]; 
ba913834b491
The parse_args_DCL fiasco continues.
Ryan C. Gordon <icculus@icculus.org>
parents:
513
diff
changeset

24 
char pushedback; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

25 
} TokenizerContext; 
514
ba913834b491
The parse_args_DCL fiasco continues.
Ryan C. Gordon <icculus@icculus.org>
parents:
513
diff
changeset

26 

ba913834b491
The parse_args_DCL fiasco continues.
Ryan C. Gordon <icculus@icculus.org>
parents:
513
diff
changeset

27 

465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

28 
typedef struct Context Context; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

29 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

30 
// Context...this is state that changes as we assemble a shader... 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

31 
struct Context 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

32 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

33 
MOJOSHADER_malloc malloc; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

34 
MOJOSHADER_free free; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

35 
void *malloc_data; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

36 
const char *failstr; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

37 
TokenizerContext tctx; 
486
45efac751027
Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset

38 
int started_parsing; 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

39 
MOJOSHADER_shaderType shader_type; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

40 
uint8 major_ver; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

41 
uint8 minor_ver; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

42 
uint32 tokenbuf[16]; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

43 
int tokenbufpos; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

44 
DestArgInfo dest_arg; 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

45 
uint32 *output; 
486
45efac751027
Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset

46 
uint32 *token_to_line; 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

47 
size_t output_len; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

48 
size_t output_allocation; 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

49 
}; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

50 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

51 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

52 
// Convenience functions for allocators... 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

53 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

54 
static inline int out_of_memory(Context *ctx) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

55 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

56 
if (ctx>failstr == NULL) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

57 
ctx>failstr = out_of_mem_str; // fail() would call malloc(). 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

58 
return FAIL; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

59 
} // out_of_memory 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

60 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

61 
static inline void *Malloc(Context *ctx, const size_t len) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

62 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

63 
void *retval = ctx>malloc((int) len, ctx>malloc_data); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

64 
if (retval == NULL) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

65 
out_of_memory(ctx); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

66 
return retval; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

67 
} // Malloc 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

68 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

69 
static inline void Free(Context *ctx, void *ptr) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

70 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

71 
if (ptr != NULL) // check for NULL in case of dumb free() impl. 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

72 
ctx>free(ptr, ctx>malloc_data); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

73 
} // Free 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

74 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

75 
static int failf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

76 
static int failf(Context *ctx, const char *fmt, ...) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

77 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

78 
if (ctx>failstr == NULL) // don't change existing error. 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

79 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

80 
char scratch = 0; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

81 
va_list ap; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

82 
va_start(ap, fmt); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

83 
const int len = vsnprintf(&scratch, sizeof (scratch), fmt, ap); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

84 
va_end(ap); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

85 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

86 
char *failstr = (char *) Malloc(ctx, len + 1); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

87 
if (failstr != NULL) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

88 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

89 
va_start(ap, fmt); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

90 
vsnprintf(failstr, len + 1, fmt, ap); // rebuild it. 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

91 
va_end(ap); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

92 
ctx>failstr = failstr; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

93 
} // if 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

94 
} // if 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

95 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

96 
return FAIL; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

97 
} // failf 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

98 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

99 
static inline int fail(Context *ctx, const char *reason) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

100 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

101 
return failf(ctx, "%s", reason); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

102 
} // fail 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

103 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

104 
static inline int isfail(const Context *ctx) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

105 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

106 
return (ctx>failstr != NULL); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

107 
} // isfail 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

108 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

109 

515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

110 
static inline int tokeq(const TokenizerContext *tctx, const char *token) 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

111 
{ 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

112 
return (strcasecmp(tctx>token, token) == 0); 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

113 
} // tokeq 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

114 

58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

115 

465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

116 
// Shader model version magic... 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

117 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

118 
static inline uint32 ver_ui32(const uint8 major, const uint8 minor) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

119 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

120 
return ( (((uint32) major) << 16)  (((minor) == 0xFF) ? 0 : (minor)) ); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

121 
} // version_ui32 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

122 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

123 
static inline int shader_version_atleast(const Context *ctx, const uint8 maj, 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

124 
const uint8 min) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

125 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

126 
return (ver_ui32(ctx>major_ver, ctx>minor_ver) >= ver_ui32(maj, min)); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

127 
} // shader_version_atleast 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

128 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

129 
static inline int shader_is_pixel(const Context *ctx) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

130 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

131 
return (ctx>shader_type == MOJOSHADER_TYPE_PIXEL); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

132 
} // shader_is_pixel 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

133 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

134 
static inline int shader_is_vertex(const Context *ctx) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

135 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

136 
return (ctx>shader_type == MOJOSHADER_TYPE_VERTEX); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

137 
} // shader_is_vertex 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

138 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

139 

482
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

140 
static int ui32fromstr(const char *str, uint32 *ui32) 
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

141 
{ 
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

142 
//*ui32 = (uint32) atoi(minstr); 
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

143 
char *endptr = NULL; 
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

144 
const long val = strtol(str, &endptr, 10); 
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

145 
*ui32 = (uint32) val; 
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

146 
return ((val >= 0) && (*str != '\0') && (*endptr == '\0')); 
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

147 
} // ui32fromstr 
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

148 

465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

149 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

150 
static void output_token_noswap(Context *ctx, const uint32 token) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

151 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

152 
if (isfail(ctx)) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

153 
return; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

154 

470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

155 
if (ctx>output_len >= ctx>output_allocation) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

156 
{ 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

157 
const size_t output_alloc_bump = 1024; // that's tokens, not bytes. 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

158 
const size_t newsize = ctx>output_allocation + output_alloc_bump; 
486
45efac751027
Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset

159 
void *ptr; 
45efac751027
Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset

160 

45efac751027
Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset

161 
ptr = Malloc(ctx, newsize * sizeof (uint32)); 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

162 
if (ptr == NULL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

163 
return; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

164 
if (ctx>output_len > 0) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

165 
memcpy(ptr, ctx>output, ctx>output_len * sizeof (uint32)); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

166 
Free(ctx, ctx>output); 
486
45efac751027
Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset

167 
ctx>output = (uint32 *) ptr; 
45efac751027
Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset

168 

45efac751027
Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset

169 
ptr = Malloc(ctx, newsize * sizeof (uint32)); 
45efac751027
Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset

170 
if (ptr == NULL) 
45efac751027
Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset

171 
return; 
45efac751027
Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset

172 
if (ctx>output_len > 0) 
45efac751027
Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset

173 
memcpy(ptr, ctx>token_to_line, ctx>output_len * sizeof (uint32)); 
45efac751027
Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset

174 
Free(ctx, ctx>token_to_line); 
45efac751027
Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset

175 
ctx>token_to_line = (uint32 *) ptr; 
45efac751027
Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset

176 

470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

177 
ctx>output_allocation = newsize; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

178 
} // if 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

179 

486
45efac751027
Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset

180 
ctx>output[ctx>output_len] = token; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

181 
ctx>token_to_line[ctx>output_len] = ctx>tctx.linenum; 
486
45efac751027
Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents:
485
diff
changeset

182 
ctx>output_len++; 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

183 
} // output_token_noswap 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

184 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

185 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

186 
static inline void output_token(Context *ctx, const uint32 token) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

187 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

188 
output_token_noswap(ctx, SWAP32(token)); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

189 
} // output_token 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

190 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

191 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

192 
static void output_comment_bytes(Context *ctx, const uint8 *buf, size_t len) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

193 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

194 
if (len > (0xFFFF * 4)) // length is stored as token count, in 16 bits. 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

195 
fail(ctx, "Comment field is too big"); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

196 
else if (!isfail(ctx)) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

197 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

198 
const uint32 tokencount = (len / 4) + ((len % 4) ? 1 : 0); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

199 
output_token(ctx, 0xFFFE  (tokencount << 16)); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

200 
while (len >= 4) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

201 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

202 
output_token_noswap(ctx, *((const uint32 *) buf)); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

203 
len = 4; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

204 
buf += 4; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

205 
} // while 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

206 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

207 
if (len > 0) // handle spillover... 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

208 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

209 
union { uint8 ui8[4]; uint32 ui32; } overflow; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

210 
overflow.ui32 = 0; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

211 
memcpy(overflow.ui8, buf, len); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

212 
output_token_noswap(ctx, overflow.ui32); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

213 
} // if 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

214 
} // else if 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

215 
} // output_comment_bytes 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

216 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

217 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

218 
static inline void output_comment_string(Context *ctx, const char *str) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

219 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

220 
output_comment_bytes(ctx, (const uint8 *) str, strlen(str)); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

221 
} // output_comment_string 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

222 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

223 

515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

224 
static int tokenize_ctx(Context *ctx, TokenizerContext *tctx) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

225 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

226 
int idx = 0; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

227 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

228 
if (isfail(ctx)) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

229 
return FAIL; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

230 

515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

231 
if (tctx>pushedback) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

232 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

233 
tctx>pushedback = 0; 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

234 
return NOFAIL; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

235 
} // if 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

236 

515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

237 
if (tctx>on_endline) 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

238 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

239 
tctx>on_endline = 0; 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

240 
tctx>linenum++; // passed a newline, update. 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

241 
} // if 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

242 

465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

243 
while (1) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

244 
{ 
477  245 
// !!! FIXME: carefully crafted (but legal) comments can trigger this. 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

246 
if (idx >= sizeof (tctx>token)) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

247 
return fail(ctx, "buffer overflow"); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

248 

515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

249 
char ch = *tctx>source; 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

250 
if (ch == '\t') 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

251 
ch = ' '; // collapse tabs into single spaces. 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

252 
else if (ch == '\r') 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

253 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

254 
if (tctx>source[1] == '\n') 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

255 
continue; // ignore '\r' if this is "\r\n" ... 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

256 
ch = '\n'; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

257 
} // else if 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

258 

478
af4bfa5d615c
Some basic tokenizer logic fixes.
Ryan C. Gordon <icculus@icculus.org>
parents:
477
diff
changeset

259 
if ((ch >= '0') && (ch <= '9')) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

260 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

261 
// starting a number, but rest of current token was not number. 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

262 
if ((idx > 0) && ((tctx>prevchar < '0')  (tctx>prevchar > '9'))) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

263 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

264 
tctx>token[idx++] = '\0'; 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

265 
return NOFAIL; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

266 
} // if 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

267 
} // if 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

268 
else 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

269 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

270 
// starting a nonnumber, but rest of current token was numbers. 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

271 
if ((idx > 0) && ((tctx>prevchar >= '0') && (tctx>prevchar <= '9'))) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

272 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

273 
tctx>token[idx++] = '\0'; 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

274 
return NOFAIL; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

275 
} // if 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

276 
} // else 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

277 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

278 
switch (ch) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

279 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

280 
case '/': 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

281 
case ';': // !!! FIXME: comment, right? 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

282 
if (idx != 0) // finish off existing token. 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

283 
tctx>token[idx] = '\0'; 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

284 
else 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

285 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

286 
tctx>token[idx++] = ch; 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

287 
tctx>source++; 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

288 
if ((ch == '/') && (*tctx>source == '/')) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

289 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

290 
tctx>token[idx++] = '/'; 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

291 
tctx>source++; 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

292 
} // if 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

293 
tctx>token[idx++] = '\0'; 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

294 
} // else 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

295 
return NOFAIL; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

296 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

297 
case ' ': 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

298 
if (tctx>prevchar == ' ') 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

299 
break; // multiple whitespace collapses into one. 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

300 
// intentional fallthrough... 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

301 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

302 
case '_': 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

303 
case '[': 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

304 
case ']': 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

305 
case '(': 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

306 
case ')': 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

307 
case '!': 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

308 
case '+': 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

309 
case '': 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

310 
case ',': 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

311 
case '.': 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

312 
case '\n': 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

313 
if (idx != 0) // finish off existing token. 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

314 
tctx>token[idx] = '\0'; 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

315 
else // this is a token in itself. 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

316 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

317 
if (ch == '\n') 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

318 
tctx>on_endline = 1; 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

319 
tctx>source++; 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

320 
tctx>token[idx++] = ch; 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

321 
tctx>token[idx++] = '\0'; 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

322 
} // else 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

323 
return NOFAIL; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

324 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

325 
case '\0': 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

326 
tctx>token[idx] = '\0'; 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

327 
if (idx != 0) // had any chars? It's a token. 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

328 
return NOFAIL; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

329 
return END_OF_STREAM; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

330 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

331 
default: 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

332 
tctx>source++; 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

333 
tctx>token[idx++] = ch; 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

334 
break; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

335 
} // switch 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

336 

515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

337 
tctx>prevchar = ch; 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

338 
} // while 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

339 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

340 
return fail(ctx, "???"); // shouldn't hit this. 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

341 
} // tokenize_ctx 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

342 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

343 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

344 
static inline int tokenize(Context *ctx) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

345 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

346 
const int rc = tokenize_ctx(ctx, &ctx>tctx); 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

347 

465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

348 
#if DEBUG_TOKENIZER 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

349 
printf("TOKENIZE: %s '%s'\n", 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

350 
(rc == END_OF_STREAM) ? "END_OF_STREAM" : 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

351 
(rc == FAIL) ? "FAIL" : 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

352 
(rc == NOFAIL) ? "NOFAIL" : "???", 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

353 
(ctx>tctx.token[0] == '\n') ? "\\n" : ctx>tctx.token); 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

354 
#endif 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

355 

465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

356 
return rc; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

357 
} // tokenize 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

358 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

359 

515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

360 
static int pushback_ctx(Context *ctx, TokenizerContext *tctx) 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

361 
{ 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

362 
if (tctx>pushedback) 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

363 
return fail(ctx, "BUG: Double pushback in parser"); 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

364 
else 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

365 
tctx>pushedback = 1; 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

366 

58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

367 
return NOFAIL; 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

368 
} 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

369 

490
14b6586d9c43
Fixed parsing of DCL instructions.
Ryan C. Gordon <icculus@icculus.org>
parents:
489
diff
changeset

370 
static inline int pushback(Context *ctx) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

371 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

372 
const int rc = pushback_ctx(ctx, &ctx>tctx); 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

373 

465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

374 
#if DEBUG_TOKENIZER 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

375 
printf("PUSHBACK: %s\n", 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

376 
(rc == END_OF_STREAM) ? "END_OF_STREAM" : 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

377 
(rc == FAIL) ? "FAIL" : 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

378 
(rc == NOFAIL) ? "NOFAIL" : "???"); 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

379 
#endif 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

380 

515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

381 
return rc; 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

382 
} // pushback 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

383 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

384 

515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

385 
static int nexttoken_ctx(Context *ctx, TokenizerContext *tctx, 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

386 
const int ignoreeol, const int ignorewhitespace, 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

387 
const int eolok, const int eosok) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

388 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

389 
int rc = NOFAIL; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

390 

515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

391 
while ((rc = tokenize_ctx(ctx, tctx)) == NOFAIL) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

392 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

393 
if (tokeq(tctx, "\n")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

394 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

395 
if (ignoreeol) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

396 
continue; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

397 
else if (!eolok) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

398 
return fail(ctx, "Unexpected EOL"); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

399 
} // if 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

400 

515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

401 
else if (tokeq(tctx, " ")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

402 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

403 
if (ignorewhitespace) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

404 
continue; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

405 
} // else if 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

406 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

407 
// skip comments... 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

408 
else if (tokeq(tctx, "//")  tokeq(tctx, ";")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

409 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

410 
while ((rc = tokenize_ctx(ctx, tctx)) == NOFAIL) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

411 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

412 
if (tokeq(tctx, "\n")) 
479
f7beff48de5e
nexttoken() needs to report the ending newline when throwing away comments.
Ryan C. Gordon <icculus@icculus.org>
parents:
478
diff
changeset

413 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

414 
pushback_ctx(ctx, tctx); 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

415 
break; 
479
f7beff48de5e
nexttoken() needs to report the ending newline when throwing away comments.
Ryan C. Gordon <icculus@icculus.org>
parents:
478
diff
changeset

416 
} // if 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

417 
} // while 
479
f7beff48de5e
nexttoken() needs to report the ending newline when throwing away comments.
Ryan C. Gordon <icculus@icculus.org>
parents:
478
diff
changeset

418 
continue; // pick up from newline, go again. 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

419 
} // if 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

420 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

421 
break; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

422 
} // while 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

423 

515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

424 
if ((rc == END_OF_STREAM) && (!eosok)) 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

425 
return fail(ctx, "Unexpected EOF"); 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

426 

58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

427 
return rc; 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

428 
} // nexttoken_ctx 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

429 

58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

430 

58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

431 
static inline int nexttoken(Context *ctx, const int ignoreeol, 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

432 
const int ignorewhitespace, const int eolok, 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

433 
const int eosok) 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

434 
{ 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

435 
const int rc = nexttoken_ctx(ctx, &ctx>tctx, ignoreeol, 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

436 
ignorewhitespace, eolok, eosok); 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

437 

465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

438 
#if DEBUG_TOKENIZER 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

439 
printf("NEXTTOKEN: %s '%s'\n", 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

440 
(rc == END_OF_STREAM) ? "END_OF_STREAM" : 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

441 
(rc == FAIL) ? "FAIL" : 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

442 
(rc == NOFAIL) ? "NOFAIL" : "???", 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

443 
(ctx>tctx.token[0] == '\n') ? "\\n" : ctx>tctx.token); 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

444 
#endif 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

445 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

446 
return rc; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

447 
} // nexttoken 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

448 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

449 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

450 
static int require_endline(Context *ctx) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

451 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

452 
TokenizerContext *tctx = &ctx>tctx; 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

453 
const int rc = nexttoken(ctx, 0, 1, 1, 1); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

454 
if (rc == FAIL) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

455 
return FAIL; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

456 
else if (rc == END_OF_STREAM) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

457 
return NOFAIL; // we'll call this an EOL. 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

458 
else if (!tokeq(tctx, "\n")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

459 
return fail(ctx, "Endline expected"); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

460 
return NOFAIL; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

461 
} // require_endline 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

462 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

463 

470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

464 
static int require_comma(Context *ctx) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

465 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

466 
TokenizerContext *tctx = &ctx>tctx; 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

467 
const int rc = nexttoken(ctx, 0, 1, 0, 0); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

468 
if (rc == FAIL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

469 
return FAIL; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

470 
else if (!tokeq(tctx, ",")) 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

471 
return fail(ctx, "Comma expected"); 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

472 
return NOFAIL; 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

473 
} // require_comma 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

474 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

475 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

476 
static int parse_register_name(Context *ctx, RegisterType *rtype, int *rnum) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

477 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

478 
TokenizerContext *tctx = &ctx>tctx; 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

479 
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

480 
return FAIL; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

481 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

482 
int neednum = 1; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

483 
int regnum = 0; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

484 
RegisterType regtype = REG_TYPE_TEMP; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

485 
if (tokeq(tctx, "r")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

486 
regtype = REG_TYPE_TEMP; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

487 
else if (tokeq(tctx, "v")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

488 
regtype = REG_TYPE_INPUT; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

489 
else if (tokeq(tctx, "c")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

490 
regtype = REG_TYPE_CONST; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

491 
else if (tokeq(tctx, "i")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

492 
regtype = REG_TYPE_CONSTINT; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

493 
else if (tokeq(tctx, "b")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

494 
regtype = REG_TYPE_CONSTBOOL; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

495 
else if (tokeq(tctx, "oC")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

496 
regtype = REG_TYPE_COLOROUT; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

497 
else if (tokeq(tctx, "s")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

498 
regtype = REG_TYPE_SAMPLER; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

499 
else if (tokeq(tctx, "oD")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

500 
regtype = REG_TYPE_ATTROUT; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

501 
else if (tokeq(tctx, "l")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

502 
regtype = REG_TYPE_LABEL; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

503 
else if (tokeq(tctx, "p")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

504 
regtype = REG_TYPE_PREDICATE; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

505 
else if (tokeq(tctx, "oDepth")) 
513
abd9c85ba168
oDepth register doesn't have an index.
Ryan C. Gordon <icculus@icculus.org>
parents:
512
diff
changeset

506 
{ 
abd9c85ba168
oDepth register doesn't have an index.
Ryan C. Gordon <icculus@icculus.org>
parents:
512
diff
changeset

507 
regtype = REG_TYPE_DEPTHOUT; 
abd9c85ba168
oDepth register doesn't have an index.
Ryan C. Gordon <icculus@icculus.org>
parents:
512
diff
changeset

508 
neednum = 0; 
abd9c85ba168
oDepth register doesn't have an index.
Ryan C. Gordon <icculus@icculus.org>
parents:
512
diff
changeset

509 
} // else if 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

510 
else if (tokeq(tctx, "aL")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

511 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

512 
regtype = REG_TYPE_LOOP; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

513 
neednum = 0; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

514 
} // else if 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

515 
else if (tokeq(tctx, "o")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

516 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

517 
if (!shader_is_vertex(ctx)  !shader_version_atleast(ctx, 3, 0)) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

518 
return fail(ctx, "Output register not valid in this shader type"); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

519 
regtype = REG_TYPE_OUTPUT; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

520 
} // else if 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

521 
else if (tokeq(tctx, "oT")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

522 
{ 
502
3cb501248990
Fixed logic error in assembler, to decide if oTx registers are available.
Ryan C. Gordon <icculus@icculus.org>
parents:
500
diff
changeset

523 
if (shader_is_vertex(ctx) && shader_version_atleast(ctx, 3, 0)) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

524 
return fail(ctx, "Output register not valid in this shader type"); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

525 
regtype = REG_TYPE_OUTPUT; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

526 
} // else if 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

527 
else if (tokeq(tctx, "a")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

528 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

529 
if (!shader_is_vertex(ctx)) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

530 
return fail(ctx, "Address register only valid in vertex shaders."); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

531 
regtype = REG_TYPE_ADDRESS; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

532 
} // else if 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

533 
else if (tokeq(tctx, "t")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

534 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

535 
if (!shader_is_pixel(ctx)) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

536 
return fail(ctx, "Address register only valid in pixel shaders."); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

537 
regtype = REG_TYPE_ADDRESS; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

538 
} // else if 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

539 
else if (tokeq(tctx, "vPos")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

540 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

541 
regtype = REG_TYPE_MISCTYPE; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

542 
regnum = (int) MISCTYPE_TYPE_POSITION; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

543 
neednum = 0; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

544 
} // else if 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

545 
else if (tokeq(tctx, "vFace")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

546 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

547 
regtype = REG_TYPE_MISCTYPE; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

548 
regnum = (int) MISCTYPE_TYPE_FACE; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

549 
neednum = 0; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

550 
} // else if 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

551 
else if (tokeq(tctx, "oPos")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

552 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

553 
regtype = REG_TYPE_RASTOUT; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

554 
regnum = (int) RASTOUT_TYPE_POSITION; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

555 
neednum = 0; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

556 
} // else if 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

557 
else if (tokeq(tctx, "oFog")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

558 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

559 
regtype = REG_TYPE_RASTOUT; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

560 
regnum = (int) RASTOUT_TYPE_FOG; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

561 
neednum = 0; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

562 
} // else if 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

563 
else if (tokeq(tctx, "oPts")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

564 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

565 
regtype = REG_TYPE_RASTOUT; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

566 
regnum = (int) RASTOUT_TYPE_POINT_SIZE; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

567 
neednum = 0; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

568 
} // else if 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

569 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

570 
//case REG_TYPE_TEMPFLOAT16: // !!! FIXME: don't know this asm string 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

571 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

572 
else 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

573 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

574 
return fail(ctx, "expected register type"); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

575 
} // else 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

576 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

577 
if (neednum) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

578 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

579 
// Make a temp TokenizerContext, since we need to skip whitespace here, 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

580 
// but if the next nonwhitespace token isn't '[', we'll want to get 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

581 
// that whitespace back. 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

582 
TokenizerContext tmptctx; 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

583 
memcpy(&tmptctx, tctx, sizeof (TokenizerContext)); 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

584 
if (nexttoken_ctx(ctx, &tmptctx, 0, 1, 1, 1) == FAIL) 
482
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

585 
return FAIL; 
522
3c08f4d2aa2e
Fixed relative addressing parsing, again.
Ryan C. Gordon <icculus@icculus.org>
parents:
521
diff
changeset

586 
else if (tokeq(&tmptctx, "[")) 
482
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

587 
neednum = 0; 
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

588 
} // if 
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

589 

3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

590 
if (neednum) 
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

591 
{ 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

592 
if (nexttoken(ctx, 0, 0, 0, 0) == FAIL) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

593 
return FAIL; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

594 

482
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

595 
uint32 ui32 = 0; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

596 
if (!ui32fromstr(tctx>token, &ui32)) 
482
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

597 
return fail(ctx, "Invalid register index"); 
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

598 
regnum = (int) ui32; 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

599 
} // if 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

600 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

601 
// split up REG_TYPE_CONST 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

602 
if (regtype == REG_TYPE_CONST) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

603 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

604 
if (regnum < 2048) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

605 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

606 
regtype = REG_TYPE_CONST; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

607 
regnum = 0; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

608 
} // if 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

609 
else if (regnum < 4096) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

610 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

611 
regtype = REG_TYPE_CONST2; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

612 
regnum = 2048; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

613 
} // if 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

614 
else if (regnum < 6144) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

615 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

616 
regtype = REG_TYPE_CONST3; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

617 
regnum = 4096; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

618 
} // if 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

619 
else if (regnum < 8192) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

620 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

621 
regtype = REG_TYPE_CONST4; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

622 
regnum = 6144; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

623 
} // if 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

624 
else 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

625 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

626 
return fail(ctx, "Invalid const register index"); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

627 
} // else 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

628 
} // if 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

629 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

630 
*rtype = regtype; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

631 
*rnum = regnum; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

632 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

633 
return NOFAIL; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

634 
} // parse_register_name 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

635 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

636 

470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

637 
static int set_result_shift(Context *ctx, DestArgInfo *info, const int val) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

638 
{ 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

639 
if (info>result_shift != 0) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

640 
return fail(ctx, "Multiple result shift modifiers"); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

641 
info>result_shift = val; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

642 
return NOFAIL; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

643 
} // set_result_shift 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

644 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

645 

515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

646 
static int parse_destination_token(Context *ctx) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

647 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

648 
TokenizerContext *tctx = &ctx>tctx; 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

649 

58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

650 
DestArgInfo *info = &ctx>dest_arg; 
510
f4433db86f6e
Fixed wrong sizeof for a memset() call.
Ryan C. Gordon <icculus@icculus.org>
parents:
508
diff
changeset

651 
memset(info, '\0', sizeof (DestArgInfo)); 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

652 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

653 
// See if there are destination modifiers on the instruction itself... 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

654 
while (1) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

655 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

656 
if (nexttoken(ctx, 0, 0, 0, 0) == FAIL) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

657 
return FAIL; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

658 
else if (tokeq(tctx, " ")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

659 
break; // done with modifiers. 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

660 
else if (!tokeq(tctx, "_")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

661 
return fail(ctx, "Expected modifier or whitespace"); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

662 
else if (nexttoken(ctx, 0, 0, 0, 0) == FAIL) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

663 
return FAIL; 
511
67d7efa9206b
Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
510
diff
changeset

664 
// !!! FIXME: this can be cleaned up when tokenizer is fixed. 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

665 
else if (tokeq(tctx, "x")) 
511
67d7efa9206b
Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
510
diff
changeset

666 
{ 
67d7efa9206b
Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
510
diff
changeset

667 
if (nexttoken(ctx, 0, 0, 0, 0) == FAIL) 
67d7efa9206b
Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
510
diff
changeset

668 
return FAIL; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

669 
else if (tokeq(tctx, "2")) 
511
67d7efa9206b
Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
510
diff
changeset

670 
set_result_shift(ctx, info, 0x1); 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

671 
else if (tokeq(tctx, "4")) 
511
67d7efa9206b
Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
510
diff
changeset

672 
set_result_shift(ctx, info, 0x2); 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

673 
else if (tokeq(tctx, "8")) 
511
67d7efa9206b
Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
510
diff
changeset

674 
set_result_shift(ctx, info, 0x3); 
67d7efa9206b
Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
510
diff
changeset

675 
else 
67d7efa9206b
Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
510
diff
changeset

676 
return fail(ctx, "Expected modifier"); 
67d7efa9206b
Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
510
diff
changeset

677 
} // else if 
67d7efa9206b
Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
510
diff
changeset

678 
// !!! FIXME: this can be cleaned up when tokenizer is fixed. 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

679 
else if (tokeq(tctx, "d")) 
511
67d7efa9206b
Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
510
diff
changeset

680 
{ 
67d7efa9206b
Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
510
diff
changeset

681 
if (nexttoken(ctx, 0, 0, 0, 0) == FAIL) 
67d7efa9206b
Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
510
diff
changeset

682 
return FAIL; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

683 
else if (tokeq(tctx, "8")) 
511
67d7efa9206b
Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
510
diff
changeset

684 
set_result_shift(ctx, info, 0xD); 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

685 
else if (tokeq(tctx, "4")) 
511
67d7efa9206b
Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
510
diff
changeset

686 
set_result_shift(ctx, info, 0xE); 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

687 
else if (tokeq(tctx, "2")) 
511
67d7efa9206b
Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
510
diff
changeset

688 
set_result_shift(ctx, info, 0xF); 
67d7efa9206b
Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
510
diff
changeset

689 
else 
67d7efa9206b
Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
510
diff
changeset

690 
return fail(ctx, "Expected modifier"); 
67d7efa9206b
Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
510
diff
changeset

691 
} // else if 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

692 
else if (tokeq(tctx, "sat")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

693 
info>result_mod = MOD_SATURATE; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

694 
else if (tokeq(tctx, "pp")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

695 
info>result_mod = MOD_PP; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

696 
else if (tokeq(tctx, "centroid")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

697 
info>result_mod = MOD_CENTROID; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

698 
else 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

699 
return fail(ctx, "Expected modifier"); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

700 
} // while 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

701 

470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

702 
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

703 
return FAIL; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

704 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

705 
// !!! FIXME: predicates. 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

706 
if (tokeq(tctx, "(")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

707 
return fail(ctx, "Predicates unsupported at this time"); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

708 
pushback(ctx); // parse_register_name calls nexttoken(). 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

709 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

710 
if (parse_register_name(ctx, &info>regtype, &info>regnum) == FAIL) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

711 
return FAIL; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

712 

470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

713 
if (nexttoken(ctx, 0, 1, 1, 1) == FAIL) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

714 
return FAIL; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

715 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

716 
// !!! FIXME: can dest registers do relative addressing? 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

717 

521
57e1945104cb
Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents:
519
diff
changeset

718 
int implicit_writemask = 0; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

719 
if (!tokeq(tctx, ".")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

720 
{ 
521
57e1945104cb
Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents:
519
diff
changeset

721 
implicit_writemask = 1; 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

722 
info>writemask = 0xF; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

723 
info>writemask0 = info>writemask1 = info>writemask2 = info>writemask3 = 1; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

724 
pushback(ctx); // no explicit writemask; do full mask. 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

725 
} // if 
521
57e1945104cb
Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents:
519
diff
changeset

726 
// !!! FIXME: Cg generates code with oDepth.z ... this is a bug, I think. 
57e1945104cb
Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents:
519
diff
changeset

727 
//else if (scalar_register(ctx>shader_type, info>regtype, info>regnum)) 
57e1945104cb
Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents:
519
diff
changeset

728 
else if ( (scalar_register(ctx>shader_type, info>regtype, info>regnum)) && (info>regtype != REG_TYPE_DEPTHOUT) ) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

729 
return fail(ctx, "Writemask specified for scalar register"); 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

730 
else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

731 
return FAIL; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

732 
else if (tokeq(tctx, "")) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

733 
return fail(ctx, "Invalid writemask"); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

734 
else 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

735 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

736 
char *ptr = tctx>token; 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

737 
info>writemask0 = info>writemask1 = info>writemask2 = info>writemask3 = 0; 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

738 
if (*ptr == 'x') { info>writemask0 = 1; ptr++; } 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

739 
if (*ptr == 'y') { info>writemask1 = 1; ptr++; } 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

740 
if (*ptr == 'z') { info>writemask2 = 1; ptr++; } 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

741 
if (*ptr == 'w') { info>writemask3 = 1; ptr++; } 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

742 
if ((ptr == tctx>token) && (shader_is_pixel(ctx))) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

743 
{ 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

744 
if (*ptr == 'r') { info>writemask0 = 1; ptr++; } 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

745 
if (*ptr == 'g') { info>writemask1 = 1; ptr++; } 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

746 
if (*ptr == 'b') { info>writemask2 = 1; ptr++; } 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

747 
if (*ptr == 'a') { info>writemask3 = 1; ptr++; } 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

748 
} // if 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

749 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

750 
if (*ptr != '\0') 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

751 
return fail(ctx, "Invalid writemask"); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

752 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

753 
info>writemask = ( ((info>writemask0 & 0x1) << 0)  
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

754 
((info>writemask1 & 0x1) << 1)  
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

755 
((info>writemask2 & 0x1) << 2)  
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

756 
((info>writemask3 & 0x1) << 3) ); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

757 
} // else 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

758 

521
57e1945104cb
Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents:
519
diff
changeset

759 
// !!! FIXME: Cg generates code with oDepth.z ... this is a bug, I think. 
57e1945104cb
Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents:
519
diff
changeset

760 
if (info>regtype == REG_TYPE_DEPTHOUT) 
57e1945104cb
Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents:
519
diff
changeset

761 
{ 
57e1945104cb
Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents:
519
diff
changeset

762 
if ( (!implicit_writemask) && ((info>writemask0 + info>writemask1 + 
57e1945104cb
Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents:
519
diff
changeset

763 
info>writemask2 + info>writemask3) > 1) ) 
57e1945104cb
Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents:
519
diff
changeset

764 
return fail(ctx, "Writemask specified for scalar register"); 
57e1945104cb
Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents:
519
diff
changeset

765 
} // if 
57e1945104cb
Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents:
519
diff
changeset

766 

465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

767 
info>orig_writemask = info>writemask; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

768 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

769 
if (ctx>tokenbufpos >= STATICARRAYLEN(ctx>tokenbuf)) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

770 
return fail(ctx, "Too many tokens"); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

771 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

772 
ctx>tokenbuf[ctx>tokenbufpos++] = 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

773 
( ((((uint32) 1)) << 31)  
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

774 
((((uint32) info>regnum) & 0x7ff) << 0)  
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

775 
((((uint32) info>relative) & 0x1) << 13)  
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

776 
((((uint32) info>result_mod) & 0xF) << 20)  
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

777 
((((uint32) info>result_shift) & 0xF) << 24)  
500
38ce929323c2
Fixed writemask bits in assembled bytecode.
Ryan C. Gordon <icculus@icculus.org>
parents:
499
diff
changeset

778 
((((uint32) info>writemask) & 0xF) << 16)  
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

779 
((((uint32) info>regtype) & 0x7) << 28)  
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

780 
((((uint32) info>regtype) & 0x18) << 8) ); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

781 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

782 
return 1; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

783 
} // parse_destination_token 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

784 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

785 

472
e52d487e6d91
Bunch More Work on the assembler. Feature complete now?
Ryan C. Gordon <icculus@icculus.org>
parents:
470
diff
changeset

786 
static void set_source_mod(Context *ctx, const int negate, 
e52d487e6d91
Bunch More Work on the assembler. Feature complete now?
Ryan C. Gordon <icculus@icculus.org>
parents:
470
diff
changeset

787 
const SourceMod norm, const SourceMod negated, 
e52d487e6d91
Bunch More Work on the assembler. Feature complete now?
Ryan C. Gordon <icculus@icculus.org>
parents:
470
diff
changeset

788 
SourceMod *srcmod) 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

789 
{ 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

790 
if ( (*srcmod != SRCMOD_NONE)  (negate && (negated == SRCMOD_NONE)) ) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

791 
fail(ctx, "Incompatible source modifiers"); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

792 
else 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

793 
*srcmod = ((negate) ? negated : norm); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

794 
} // set_source_mod 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

795 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

796 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

797 
static int parse_source_token_maybe_relative(Context *ctx, const int relok) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

798 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

799 
TokenizerContext *tctx = &ctx>tctx; 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

800 
int retval = 1; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

801 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

802 
if (ctx>tokenbufpos >= STATICARRAYLEN(ctx>tokenbuf)) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

803 
return fail(ctx, "Too many tokens"); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

804 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

805 
// mark this now, so optional relative addressing token is placed second. 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

806 
uint32 *token = &ctx>tokenbuf[ctx>tokenbufpos++]; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

807 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

808 
SourceMod srcmod = SRCMOD_NONE; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

809 
int negate = 0; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

810 
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

811 
return FAIL; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

812 
else if (tokeq(tctx, "1")) 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

813 
{ 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

814 
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

815 
return FAIL; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

816 
else if (!tokeq(tctx, "")) 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

817 
return fail(ctx, "Unexpected value"); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

818 
else 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

819 
srcmod = SRCMOD_COMPLEMENT; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

820 
} // else 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

821 
else if (tokeq(tctx, "!")) 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

822 
srcmod = SRCMOD_NOT; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

823 
else if (tokeq(tctx, "")) 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

824 
negate = 1; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

825 
else 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

826 
pushback(ctx); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

827 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

828 
RegisterType regtype; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

829 
int regnum; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

830 
if (parse_register_name(ctx, ®type, ®num) == FAIL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

831 
return FAIL; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

832 
else if (nexttoken(ctx, 0, 1, 1, 1) == FAIL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

833 
return FAIL; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

834 
else if (!tokeq(tctx, "_")) 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

835 
pushback(ctx); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

836 
else if (nexttoken(ctx, 0, 0, 0, 0) == FAIL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

837 
return FAIL; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

838 
else if (tokeq(tctx, "bias")) 
472
e52d487e6d91
Bunch More Work on the assembler. Feature complete now?
Ryan C. Gordon <icculus@icculus.org>
parents:
470
diff
changeset

839 
set_source_mod(ctx, negate, SRCMOD_BIAS, SRCMOD_BIASNEGATE, &srcmod); 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

840 
else if (tokeq(tctx, "bx2")) 
472
e52d487e6d91
Bunch More Work on the assembler. Feature complete now?
Ryan C. Gordon <icculus@icculus.org>
parents:
470
diff
changeset

841 
set_source_mod(ctx, negate, SRCMOD_SIGN, SRCMOD_SIGNNEGATE, &srcmod); 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

842 
else if (tokeq(tctx, "x2")) 
472
e52d487e6d91
Bunch More Work on the assembler. Feature complete now?
Ryan C. Gordon <icculus@icculus.org>
parents:
470
diff
changeset

843 
set_source_mod(ctx, negate, SRCMOD_X2, SRCMOD_X2NEGATE, &srcmod); 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

844 
else if (tokeq(tctx, "dz")) 
472
e52d487e6d91
Bunch More Work on the assembler. Feature complete now?
Ryan C. Gordon <icculus@icculus.org>
parents:
470
diff
changeset

845 
set_source_mod(ctx, negate, SRCMOD_DZ, SRCMOD_NONE, &srcmod); 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

846 
else if (tokeq(tctx, "dw")) 
472
e52d487e6d91
Bunch More Work on the assembler. Feature complete now?
Ryan C. Gordon <icculus@icculus.org>
parents:
470
diff
changeset

847 
set_source_mod(ctx, negate, SRCMOD_DW, SRCMOD_NONE, &srcmod); 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

848 
else if (tokeq(tctx, "abs")) 
472
e52d487e6d91
Bunch More Work on the assembler. Feature complete now?
Ryan C. Gordon <icculus@icculus.org>
parents:
470
diff
changeset

849 
set_source_mod(ctx, negate, SRCMOD_ABS, SRCMOD_ABSNEGATE, &srcmod); 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

850 
else 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

851 
return fail(ctx, "Invalid source modifier"); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

852 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

853 
if (nexttoken(ctx, 0, 1, 1, 1) == FAIL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

854 
return FAIL; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

855 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

856 
uint32 relative = 0; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

857 
if (!tokeq(tctx, "[")) 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

858 
pushback(ctx); // not relative addressing? 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

859 
else if (!relok) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

860 
return fail(ctx, "Relative addressing not permitted here."); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

861 
else 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

862 
{ 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

863 
const int rc = parse_source_token_maybe_relative(ctx, 0); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

864 
if (rc == FAIL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

865 
return FAIL; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

866 
retval += rc; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

867 
relative = 1; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

868 
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

869 
return FAIL; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

870 
else if (!tokeq(tctx, "+")) 
482
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

871 
pushback(ctx); 
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

872 
else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL) 
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

873 
return FAIL; 
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

874 
else 
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

875 
{ 
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

876 
if (regnum != 0) // !!! FIXME: maybe c3[a0.x + 5] is legal and becomes c[a0.x + 8] ? 
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

877 
fail(ctx, "Relative addressing with explicit register number."); 
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

878 
uint32 ui32 = 0; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

879 
if (!ui32fromstr(tctx>token, &ui32)) 
482
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

880 
return fail(ctx, "Invalid relative addressing offset"); 
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

881 
regnum += (int) ui32; 
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

882 
} // else 
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

883 

3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

884 
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL) 
3f740f25bd7e
Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
481
diff
changeset

885 
return FAIL; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

886 
else if (!tokeq(tctx, "]")) 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

887 
return fail(ctx, "Expected ']'"); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

888 
} // else 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

889 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

890 
if (nexttoken(ctx, 0, 1, 1, 1) == FAIL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

891 
return FAIL; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

892 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

893 
uint32 swizzle = 0; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

894 
if (!tokeq(tctx, ".")) 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

895 
{ 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

896 
swizzle = 0xE4; // 0xE4 == 11100100 ... 0 1 2 3. No swizzle. 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

897 
pushback(ctx); // no explicit writemask; do full mask. 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

898 
} // if 
491
bcc3c215807a
Fixed wrong data from scalar_register().
Ryan C. Gordon <icculus@icculus.org>
parents:
490
diff
changeset

899 
else if (scalar_register(ctx>shader_type, regtype, regnum)) 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

900 
return fail(ctx, "Swizzle specified for scalar register"); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

901 
else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

902 
return FAIL; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

903 
else if (tokeq(tctx, "")) 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

904 
return fail(ctx, "Invalid swizzle"); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

905 
else 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

906 
{ 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

907 
// deal with shortened form (.x = .xxxx, etc). 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

908 
if (tctx>token[1] == '\0') 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

909 
tctx>token[1] = tctx>token[2] = tctx>token[3] = tctx>token[0]; 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

910 
else if (tctx>token[2] == '\0') 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

911 
tctx>token[2] = tctx>token[3] = tctx>token[1]; 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

912 
else if (tctx>token[3] == '\0') 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

913 
tctx>token[3] = tctx>token[2]; 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

914 
else if (tctx>token[4] != '\0') 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

915 
return fail(ctx, "Invalid swizzle"); 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

916 
tctx>token[4] = '\0'; 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

917 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

918 
uint32 val; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

919 
int saw_xyzw = 0; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

920 
int saw_rgba = 0; 
472
e52d487e6d91
Bunch More Work on the assembler. Feature complete now?
Ryan C. Gordon <icculus@icculus.org>
parents:
470
diff
changeset

921 
int i; 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

922 
for (i = 0; i < 4; i++) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

923 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

924 
const int component = (int) tctx>token[i]; 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

925 
switch (component) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

926 
{ 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

927 
case 'x': val = 0; saw_xyzw = 1; break; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

928 
case 'y': val = 1; saw_xyzw = 1; break; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

929 
case 'z': val = 2; saw_xyzw = 1; break; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

930 
case 'w': val = 3; saw_xyzw = 1; break; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

931 
case 'r': val = 0; saw_rgba = 1; break; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

932 
case 'g': val = 1; saw_rgba = 1; break; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

933 
case 'b': val = 2; saw_rgba = 1; break; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

934 
case 'a': val = 3; saw_rgba = 1; break; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

935 
default: return fail(ctx, "Invalid swizzle"); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

936 
} // switch 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

937 
swizzle = (val << (i * 2)); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

938 
} // for 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

939 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

940 
if (saw_xyzw && saw_rgba) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

941 
return fail(ctx, "Invalid swizzle"); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

942 
} // else 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

943 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

944 
*token = ( ((((uint32) 1)) << 31)  
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

945 
((((uint32) regnum) & 0x7ff) << 0)  
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

946 
((((uint32) relative) & 0x1) << 13)  
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

947 
((((uint32) swizzle) & 0xFF) << 16)  
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

948 
((((uint32) srcmod) & 0xF) << 24)  
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

949 
((((uint32) regtype) & 0x7) << 28)  
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

950 
((((uint32) regtype) & 0x18) << 8) ); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

951 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

952 
return retval; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

953 
} // parse_source_token_maybe_relative 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

954 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

955 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

956 
static inline int parse_source_token(Context *ctx) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

957 
{ 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

958 
return parse_source_token_maybe_relative(ctx, 1); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

959 
} // parse_source_token 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

960 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

961 

465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

962 
static int parse_args_NULL(Context *ctx) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

963 
{ 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

964 
return (isfail(ctx) ? FAIL : 1); 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

965 
} // parse_args_NULL 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

966 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

967 

470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

968 
static int parse_num(Context *ctx, const int floatok, uint32 *token) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

969 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

970 
TokenizerContext *tctx = &ctx>tctx; 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

971 
int32 negative = 1; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

972 
union { float f; int32 si32; uint32 ui32; } cvt; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

973 
cvt.si32 = 0; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

974 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

975 
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

976 
return FAIL; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

977 
else if (tokeq(tctx, "")) 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

978 
negative = 1; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

979 
else 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

980 
pushback(ctx); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

981 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

982 
uint32 val = 0; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

983 
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

984 
return FAIL; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

985 
else if (!ui32fromstr(tctx>token, &val)) 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

986 
return fail(ctx, "Expected number"); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

987 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

988 
uint32 fraction = 0; 
488
1a318d011c00
Handle endlines on DEF statements.
Ryan C. Gordon <icculus@icculus.org>
parents:
487
diff
changeset

989 
if (nexttoken(ctx, 0, 1, 1, 1) == FAIL) 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

990 
return FAIL; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

991 
else if (!tokeq(tctx, ".")) 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

992 
pushback(ctx); // whole number 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

993 
else if (!floatok) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

994 
return fail(ctx, "Expected whole number"); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

995 
else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

996 
return FAIL; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

997 
else if (!ui32fromstr(tctx>token, &fraction)) 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

998 
return fail(ctx, "Expected number"); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

999 

492
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1000 
uint32 exponent = 0; 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1001 
int negexp = 0; 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1002 
if (nexttoken(ctx, 0, 1, 1, 1) == FAIL) 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1003 
return FAIL; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

1004 
else if (!tokeq(tctx, "e")) 
492
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1005 
pushback(ctx); 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1006 
else if (!floatok) 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1007 
return fail(ctx, "Exponent on whole number"); // !!! FIXME: illegal? 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1008 
else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL) 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1009 
return FAIL; 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1010 
else 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1011 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

1012 
if (!tokeq(tctx, "")) 
492
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1013 
pushback(ctx); 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1014 
else 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1015 
negexp = 1; 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1016 

29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1017 
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL) 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1018 
return FAIL; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

1019 
else if (!ui32fromstr(tctx>token, &exponent)) 
492
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1020 
return fail(ctx, "Expected exponent"); 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1021 
} // else 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1022 

470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1023 
if (!floatok) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1024 
cvt.si32 = ((int32) val) * negative; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1025 
else 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1026 
{ 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1027 
// !!! FIXME: this is lame. 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1028 
char buf[128]; 
492
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1029 
snprintf(buf, sizeof (buf), "%s%u.%u", (negative < 0) ? "" : "", 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1030 
(uint) val, (uint) fraction); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1031 
sscanf(buf, "%f", &cvt.f); 
492
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1032 
cvt.f *= (float) negative; 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1033 

29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1034 
if (exponent) 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1035 
{ 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1036 
int i; 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1037 
if (negexp) 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1038 
{ 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1039 
for (i = 0; i > exponent; i) 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1040 
cvt.f /= 10.0f; 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1041 
} // if 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1042 
else 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1043 
{ 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1044 
for (i = 0; i < exponent; i++) 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1045 
cvt.f *= 10.0f; 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1046 
} // else 
29bfa3448549
Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents:
491
diff
changeset

1047 
} // if 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1048 
} // else 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1049 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1050 
*token = cvt.ui32; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1051 
return NOFAIL; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1052 
} // parse_num 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1053 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1054 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1055 
static int parse_args_DEFx(Context *ctx, const int isflt) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

1056 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

1057 
if (parse_destination_token(ctx) == FAIL) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

1058 
return FAIL; 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1059 
else if (require_comma(ctx) == FAIL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1060 
return FAIL; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1061 
else if (parse_num(ctx, isflt, &ctx>tokenbuf[ctx>tokenbufpos++]) == FAIL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1062 
return FAIL; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1063 
else if (require_comma(ctx) == FAIL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1064 
return FAIL; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1065 
else if (parse_num(ctx, isflt, &ctx>tokenbuf[ctx>tokenbufpos++]) == FAIL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1066 
return FAIL; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1067 
else if (require_comma(ctx) == FAIL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1068 
return FAIL; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1069 
else if (parse_num(ctx, isflt, &ctx>tokenbuf[ctx>tokenbufpos++]) == FAIL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1070 
return FAIL; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1071 
else if (require_comma(ctx) == FAIL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1072 
return FAIL; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1073 
else if (parse_num(ctx, isflt, &ctx>tokenbuf[ctx>tokenbufpos++]) == FAIL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1074 
return FAIL; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1075 
return 6; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1076 
} // parse_args_DEFx 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

1077 

470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1078 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1079 
static int parse_args_DEF(Context *ctx) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1080 
{ 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1081 
return parse_args_DEFx(ctx, 1); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1082 
} // parse_args_DEF 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

1083 

470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1084 

7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1085 
static int parse_args_DEFI(Context *ctx) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1086 
{ 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1087 
return parse_args_DEFx(ctx, 0); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1088 
} // parse_args_DEFI 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

1089 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

1090 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

1091 
static int parse_args_DEFB(Context *ctx) 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

1092 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

1093 
TokenizerContext *tctx = &ctx>tctx; 
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

1094 
if (parse_destination_token(ctx) == FAIL) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

1095 
return FAIL; 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1096 
else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1097 
return FAIL; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

1098 
else if (!tokeq(tctx, ",")) 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1099 
return fail(ctx, "Expected ','"); 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1100 
else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL) 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1101 
return FAIL; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

1102 
else if (tokeq(tctx, "true")) 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1103 
ctx>tokenbuf[ctx>tokenbufpos++] = 1; 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

1104 
else if (tokeq(tctx, "false")) 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1105 
ctx>tokenbuf[ctx>tokenbufpos++] = 0; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1106 
else 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1107 
return fail(ctx, "Expected 'true' or 'false'"); 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

1108 
return 3; 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

1109 
} // parse_args_DEFB 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

1110 

0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

1111 

504  1112 
static int parse_dcl_usage(Context *ctx, uint32 *val, int *issampler) 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

1113 
{ 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents:
514
diff
changeset

1114 
TokenizerContext *tctx = &ctx>tctx; 
470
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1115 
int i; 
7d84d3452125
Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents:
465
diff
changeset

1116 
static const char *samplerusagestrs[] = { "2d", "cube", "volume" }; 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

1117 
static const char *usagestrs[] = { 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

1118 
"position", "blendweight", "blendindices", "normal", "psize", 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

1119 
"texcoord", "tangent", "binormal", "tessfactor", "positiont", 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

1120 
"color", "fog", "depth", "sample" 
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

1121 
}; 
514
ba913834b491
The parse_args_DCL fiasco continues.
Ryan C. Gordon <icculus@icculus.org>
parents:
513
diff
changeset

1122 
static const char *ignorestrs[] = { "pp", "centroid", "saturate" }; 
465
0a75f98f785b
Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset

1123 

504  1124 
// !!! FIXME: we need to clean this out in the tokenizer. 
515
58c1a7d98176
First steps of reworking tokenizer.
Ryan C. Gordon <icculus@i 