correct graph coloring and variable liveness analysis

This commit is contained in:
Tan, Kian-ting 2025-09-02 22:36:05 +08:00
parent 8ebd708b11
commit a827b9138d
3 changed files with 3 additions and 77 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
a.s
a.out
.gitignore

View file

@ -1,18 +0,0 @@
# TComp
A practice of Essential of Complication in Julia
## Dependencies
- julia
- [Match.jl](github.com/JuliaServices/Match.jl)
## instruction
`./src/TComp.jl [.tc file]`
the output assembly code is `./a.c` in AT&T assembly langauge.
to make it executable, please use `gcc`: `gcc ./a.c -o output.out`
the example `.tc` files is in `./test`
## Known issues
- all connected variable are pathized (I don't understand the tricky method to elimitated the path number)

View file

@ -1,59 +0,0 @@
graph = [['a', 'b'], ['b', 'c'], ['e', 'd'], ['e', 'a'], ['a', 'c'], ['b','e'], ['e','c']]
function vertexColoring(graph)
notDefined = -1
function getColor(v, color)
if !(v in keys(color))
return -1
else
return color[v]
end
end
vertices = Set(vcat(graph...))
verticesList = collect(vertices)
verticesMapping = map(x -> [x, Set()], verticesList)
adjacentNodes = Dict(verticesMapping)
for link in graph
a = link[1]
b = link[2]
push!(adjacentNodes[a], b)
push!(adjacentNodes[b], a)
end
sort!(verticesList, by=x -> length(adjacentNodes[x]), rev=true)
color = Dict()
println(verticesList)
for i in verticesList
i_adjacents = adjacentNodes[i]
println(i_adjacents)
i_adjacents_color_set = Set(map(x -> getColor(x, color), collect(i_adjacents)))
i_adjacents_color_list = sort(collect(i_adjacents_color_set))
if i_adjacents_color_list == [notDefined]
color[i] = 0
else
tmpId = 0
for i in i_adjacents_color_list
if tmpId == i
tmpId += 1
end
end
color[i] = tmpId
end
end
return color
end
println(vertexColoring(graph))