summaryrefslogtreecommitdiffstats
path: root/src/asmformatter.cpp
blob: fb77789bef5084a811777a6057b491a3eb202a8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/***************************************************************************
 *   Copyright (C) 2003-2005 by David Saxton                               *
 *   david@bluehaze.org                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "asmformatter.h"
#include "core/ktlconfig.h"
#include "picinfo12bit.h"
#include "picinfo14bit.h"
#include "picinfo16bit.h"

static QString extractComment( const QString & line )
{
	int pos = line.find( ';' );
	
	if ( pos == -1 )
		return "";
	
	return line.right( line.length() - pos );
}


//BEGIN class AsmFormatter
AsmFormatter::AsmFormatter()
{
}


AsmFormatter::~AsmFormatter()
{
}


QString AsmFormatter::tidyAsm( QStringList lines )
{
	// Update our indentation values from config
	m_indentAsmName = KTLConfig::indentAsmName();
	m_indentAsmData = KTLConfig::indentAsmData();
	m_indentEqu = KTLConfig::indentEqu();
	m_indentEquValue = KTLConfig::indentEquValue();
	m_indentComment = m_indentEquComment = KTLConfig::indentComment();
	
	QStringList::iterator end = lines.end();
	for ( QStringList::iterator slit = lines.begin(); slit != end; ++slit )
	{
		switch ( lineType(*slit) )
		{
			case Other:
				break;
				
			case Equ:
				*slit = tidyEqu( *slit );
				break;
				
			case Instruction:
				*slit = tidyInstruction( *slit );
				break;
		}
	}
	
	QString code;
	
	for ( QStringList::iterator slit = lines.begin(); slit != end; ++slit )
		code.append( *slit + '\n' );
	
	return code;
}


void AsmFormatter::pad( QString & text, int length )
{
	int padLength = length - text.length();
	if ( padLength <= 0 )
		return;
	
	QString pad;
	pad.fill( ' ', padLength );
	text += pad;
}


QString AsmFormatter::tidyInstruction( const QString & oldLine )
{
	InstructionParts parts( oldLine );
	QString line;
	
	if ( !parts.label().isEmpty() )
		line = parts.label() + ' ';
	pad( line, m_indentAsmName );
	
	if ( !parts.operand().isEmpty() )
		line += parts.operand() + ' ';
	pad( line, m_indentAsmData );
	
	if ( !parts.operandData().isEmpty() )
		line += parts.operandData();
	pad( line, m_indentComment );
	
	if ( parts.comment().isEmpty() )
	{
		// Remove any whitespace at the end if we're not padding out a comment
		while ( !line.isEmpty() && line[ line.length() - 1 ].isSpace() )
			line.remove( line.length() -1, 1 );
	}
	else
		line += parts.comment();
	
	return line;
}


QString AsmFormatter::tidyEqu( const QString & oldLine )
{
	QString comment = extractComment( oldLine );
	QString code = oldLine;
	code.remove( comment );
	code = code.simplifyWhiteSpace();

    QStringList parts = QStringList::split( ' ', code );

	QString pad0, pad1, pad2;
	pad0.fill( ' ', m_indentEqu - (*parts.at(0)).length() );
	pad1.fill( ' ', m_indentEquValue - m_indentEqu - (*parts.at(1)).length() );
	pad2.fill( ' ', m_indentEquComment - m_indentEquValue - m_indentEqu - (*parts.at(2)).length() );

	code = *parts.at(0) + pad0;
	code += *parts.at(1) + pad1;
	code += *parts.at(2);
	if ( !comment.isEmpty() )
	{
		code += pad2;
		code += comment;
	}
	
	return code;
}


AsmFormatter::LineType AsmFormatter::lineType( QString line )
{
	line = line.simplifyWhiteSpace();
	
	line.remove( extractComment( line ) );
	
	QStringList parts = QStringList::split( ' ', line );
	QStringList::iterator end = parts.end();
	for ( QStringList::iterator it = parts.begin(); it != end; ++it )
	{
		if ( (*it).lower() == "equ" )
			return Equ;
	}
	
	InstructionParts instructionParts( line );
	if ( !instructionParts.operand().isEmpty() )
		return Instruction;
	
	return Other;
}
//END class AsmFormatter



//BEGIN class InstructionParts
InstructionParts::InstructionParts( QString line )
{
	m_comment = extractComment( line );
	line.remove( m_comment );
	
	line = line.simplifyWhiteSpace();
	QStringList parts = QStringList::split( ' ', line );
	
	bool foundOperand = false;
	QStringList::iterator end = parts.end();
	for ( QStringList::iterator it = parts.begin(); it != end; ++it )
	{
		if ( foundOperand )
		{
			// Already found the operand, so anything else must be the operand
			// data.
			
			if ( m_operandData.isEmpty() )
				m_operandData = *it;
			else
				m_operandData += ' ' + *it;
			
			continue;
		}
		
		if ( PicAsm12bit::self()->operandList().contains( (*it).upper() )
				   || PicAsm14bit::self()->operandList().contains( (*it).upper() )
				   || PicAsm16bit::self()->operandList().contains( (*it).upper() ) )
		{
			m_operand = *it;
			foundOperand = true;
		}
		else
		{
			// Must be a label
			m_label = *it;
		}
	}
}
//END class InstructionParts