Skip to content

Commit

Permalink
Extract analyzer func
Browse files Browse the repository at this point in the history
  • Loading branch information
DQNEO committed Jul 9, 2023
1 parent c139092 commit 1319917
Showing 1 changed file with 38 additions and 34 deletions.
72 changes: 38 additions & 34 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,40 +643,7 @@ func main() {
}

stmts, symbolsInLexicalOrder := ParseFiles(inFiles)

var textStmts []*Stmt
var dataStmts []*Stmt

var currentSection = ".text"
for _, s := range stmts {

if s.labelSymbol != "" {
definedSymbols[s.labelSymbol] = &symbolDefinition{
name: s.labelSymbol,
section: currentSection,
}
}

switch s.keySymbol {
case ".data":
currentSection = ".data"
continue
case ".text":
currentSection = ".text"
continue
case ".global":
globalSymbols[s.operands[0].(*symbolExpr).name] = true
continue
}

switch currentSection {
case ".data":
dataStmts = append(dataStmts, s)
case ".text":
textStmts = append(textStmts, s)
}
}

textStmts, dataStmts := analyzeStatements(stmts)
s_text.contents = encodeAllText(textStmts)
s_data.contents = encodeAllData(dataStmts)

Expand Down Expand Up @@ -721,6 +688,43 @@ func main() {
elfFile.writeTo(w)
}

func analyzeStatements(stmts []*Stmt) ([]*Stmt, []*Stmt) {

var textStmts []*Stmt
var dataStmts []*Stmt

var currentSection = ".text"
for _, s := range stmts {

if s.labelSymbol != "" {
definedSymbols[s.labelSymbol] = &symbolDefinition{
name: s.labelSymbol,
section: currentSection,
}
}

switch s.keySymbol {
case ".data":
currentSection = ".data"
continue
case ".text":
currentSection = ".text"
continue
case ".global":
globalSymbols[s.operands[0].(*symbolExpr).name] = true
continue
}

switch currentSection {
case ".data":
dataStmts = append(dataStmts, s)
case ".text":
textStmts = append(textStmts, s)
}
}
return textStmts, dataStmts
}

func buildRelaTextBody(symbolIndex map[string]int) []byte {
var contents []byte

Expand Down

0 comments on commit 1319917

Please sign in to comment.